Skip to content

Instantly share code, notes, and snippets.

@sunilkumarmedium
Created December 1, 2020 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunilkumarmedium/8fb60a1c5de81fc98e8aa11387170b55 to your computer and use it in GitHub Desktop.
Save sunilkumarmedium/8fb60a1c5de81fc98e8aa11387170b55 to your computer and use it in GitHub Desktop.
LambdaEntryPoint.cs
{
"Information": [
"This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
"To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",
"dotnet lambda help",
"All the command line options for the Lambda command can be specified in this file."
],
"profile": "default",
"region": "ap-south-1",
"configuration": "Release",
"framework": "netcoreapp3.1",
"stack-name": "CleanArchitectureAppWebApi",
"s3-bucket": "mytestbucketsunil",
"s3-prefix": "CleanArchitectureAppWebApi/",
"template": "serverless.template",
"template-parameters": "ShouldCreateBucket=false;BucketName="
}
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace CleanArchitectureApp.WebApi
{
/// <summary>
/// This class extends from APIGatewayProxyFunction which contains the method FunctionHandlerAsync which is the
/// actual Lambda function entry point. The Lambda handler field should be set to
///
/// CleanArchitectureApp.WebApi::CleanArchitectureApp.WebApi.LambdaEntryPoint::FunctionHandlerAsync
/// </summary>
public class LambdaEntryPoint :
// The base class must be set to match the AWS service invoking the Lambda function. If not Amazon.Lambda.AspNetCoreServer
// will fail to convert the incoming request correctly into a valid ASP.NET Core request.
//
// API Gateway REST API -> Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
// API Gateway HTTP API payload version 1.0 -> Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
// API Gateway HTTP API payload version 2.0 -> Amazon.Lambda.AspNetCoreServer.APIGatewayHttpApiV2ProxyFunction
// Application Load Balancer -> Amazon.Lambda.AspNetCoreServer.ApplicationLoadBalancerFunction
//
// Note: When using the AWS::Serverless::Function resource with an event type of "HttpApi" then payload version 2.0
// will be the default and you must make Amazon.Lambda.AspNetCoreServer.APIGatewayHttpApiV2ProxyFunction the base class.
Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
{
/// <summary>
/// The builder has configuration, logging and Amazon API Gateway already configured. The startup class
/// needs to be configured in this method using the UseStartup<>() method.
/// </summary>
/// <param name="builder"></param>
protected override void Init(IWebHostBuilder builder)
{
builder
.UseStartup<Startup>();
}
/// <summary>
/// Use this override to customize the services registered with the IHostBuilder.
///
/// It is recommended not to call ConfigureWebHostDefaults to configure the IWebHostBuilder inside this method.
/// Instead customize the IWebHostBuilder in the Init(IWebHostBuilder) overload.
/// </summary>
/// <param name="builder"></param>
protected override void Init(IHostBuilder builder)
{
}
}
}
{
"AWSTemplateFormatVersion": "2010-09-09",
"Transform": "AWS::Serverless-2016-10-31",
"Description": "An AWS Serverless Application that uses the ASP.NET Core framework running in Amazon Lambda.",
"Parameters": {
"ShouldCreateBucket": {
"Type": "String",
"AllowedValues": [
"true",
"false"
],
"Description": "If true then the S3 bucket that will be proxied will be created with the CloudFormation stack."
},
"BucketName": {
"Type": "String",
"Description": "Name of S3 bucket that will be proxied. If left blank a name will be generated.",
"MinLength": "0"
}
},
"Conditions": {
"CreateS3Bucket": {
"Fn::Equals": [
{
"Ref": "ShouldCreateBucket"
},
"true"
]
},
"BucketNameGenerated": {
"Fn::Equals": [
{
"Ref": "BucketName"
},
""
]
}
},
"Resources": {
"AspNetCoreFunction": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "CleanArchitectureApp.WebApi::CleanArchitectureApp.WebApi.LambdaEntryPoint::FunctionHandlerAsync",
"Runtime": "dotnetcore3.1",
"CodeUri": "",
"MemorySize": 256,
"Timeout": 30,
"Role": null,
"Policies": [
"AWSLambdaFullAccess", "AmazonSSMFullAccess","AWSLambdaVPCAccessExecutionRole"
],
"Environment": {
"Variables": {
"AppS3Bucket": {
"Fn::If": [
"CreateS3Bucket",
{
"Ref": "Bucket"
},
{
"Ref": "BucketName"
}
]
}
}
},
"Events": {
"ProxyResource": {
"Type": "Api",
"Properties": {
"Path": "/{proxy+}",
"Method": "ANY"
}
},
"RootResource": {
"Type": "Api",
"Properties": {
"Path": "/",
"Method": "ANY"
}
}
}
}
},
"Bucket": {
"Type": "AWS::S3::Bucket",
"Condition": "CreateS3Bucket",
"Properties": {
"BucketName": {
"Fn::If": [
"BucketNameGenerated",
{
"Ref": "AWS::NoValue"
},
{
"Ref": "BucketName"
}
]
}
}
}
},
"Outputs": {
"ApiURL": {
"Description": "API endpoint URL for Prod environment",
"Value": {
"Fn::Sub": "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
}
},
"S3ProxyBucket": {
"Value": {
"Fn::If": [
"CreateS3Bucket",
{
"Ref": "Bucket"
},
{
"Ref": "BucketName"
}
]
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment