Developing .NET Core App run on AWS Lambda

Masayuki Hiyama
6 min readJul 10, 2020

--

About this post

This post explains the following ways:

  1. How to develop AWS Lambda function using .NET Core
  2. How to develop ASP.NET Core app running on Lambda
  3. How to enable your existed ASP.NET Core app to run on Lambda

1. How to develop Lambda function using .NET Core

In this section, I explain how to develop a Lambda function using .NET Core.

Installing project templates

Firstly, you install Amazon.Lambda.Templates package as a project template of .NET Core by the following command:

dotnet new -i Amazon.Lambda.Templates

This command enables you to use the following templates:

  • lambda.SQS (Lambda function triggered by SQS)
  • lambda.SNS (Lambda function triggered by SNS)
  • lambda.S3 (Lambda function triggered by S3 events)
  • lambda.Kinesis (Lambda function triggered by Kinesis Stream
  • lambda.KinesisFirehose (Lambda function triggered by Kinesis Firehose)
  • lambda.DynamoDB (Lambda function triggered by DynamoDB Stream)
  • lambda.SimpleApplicationLoadBalancerFunction (Lambda function triggered by Application Load Balancer)
  • serverless.AspNetCoreWebApp (ASP.NET Core Razor Pages app running on Lambda)
  • serverless.AspNetCoreWebAPI (ASP.NET Core Web API app running on Lambda)

You can confirm all templates you installed by the following command:

dotnet new lambda --list

Creating a project using the template

For example, You can create a Lambda function triggered by queue messages of SQS by the following command. This command will create a project (it contains test codes) using the template.

dotnet new lambda.SQS --name {Project name} --region {AWS region you want to use} --profile {AWS profile name}# Example: dotnet new lambda.SQS --name mysqsfunc --region ap-northeast-1 --profile default

I think the above command generated a Lambda function code (Function.cs) like the below code. (I removed comments of the function code for high visibility). If you didn’t modify any code after creating a new project (by default), The FunctionHandler method will be called when the Lambda function is executed.

Function.cs

“aws-lambda-tools-defaults.json” is the definition file of deploying the Lambda function. I recommend you to confirm that .NET Core version indicated at Project File(.csproj) and .NET Core version indicated at this file (“framework”, “function-runtime”) are same.

aws-lambda-tools-defaults.json

Deploying the project to AWS

You can deploy the project easily using Amazon.Lambda.Tools .NET Core Global Tool that is one of .NET Core tools. You can install this tool by the following command:

dotnet tool install -g Amazon.Lambda.Tools

After installing this tool, you can deploy your project to AWS by executing the following command at same directory with .NET Core project file (.csproj):

dotnet lambda deploy-function {Lambda function's name}# Example: dotnet lambda deploy-function samplesqsapp

When you create a new Lambda function using this tool, you will be asked which IAM role do you want to attach to the Lambda function. At that time, you need to select the IAM role you want to attach to the Lambda function by inputting a number, or “Create new IAM Role” to create a new IAM role for this Lambda function.

Select IAM Role that to provide AWS credentials to your code:1) RoleA2) RoleB...X) *** Create new IAM Role ***

When you can see the message “New Lambda function created”, deploying the Lambda function is already completed. However, it was just a deploying code to AWS as a Lambda function. You need to configure a trigger of the Lambda function by yourself.

2. How to develop ASP.NET Core app running on Lambda

In this section, I explain how to develop an ASP.NET Core app running on Lambda.

Creating a project using the template

You can easily create an ASP.NET Core app that can run as a Lambda function by installing Amazon.Lambda.Templates package and using the following templates:

  • serverless.AspNetCoreWebApp (ASP.NET Core Razor Pages app running on Lambda)
  • serverless.AspNetCoreWebAPI (ASP.NET Core Web API app running on Lambda)
dotnet new serverless.AspNetCoreWebApp --name {Project name} --region {AWS region you want to use} --profile {AWS profile name}# Example: new serverless.AspNetCoreWebApp --name myaspnetapp --region ap-northeast-1 --profile default

Deploying the project to AWS

You can deploy the project you created using Amazon.Lambda.Tools .NET Core Global Tool that you installed at the below. The following command will deploy your ASP.NET Core app to AWS as a Lambda function. Unlike “deploy-function” command, “deploy-serverless” command deploy your ASP.NET Core app using CloudFormation, its stack contains not only a Lambda function but also Amazon API Gateway resources (like a Stage), an IAM role, etc..

dotnet lambda deploy-serverless

You need to indicate a CloudFormation stack name and an S3 bucket name which is used for storing your application code. You can define those at aws-lambda-tools-defaults.json to skip those input.

3. How to enable your existed ASP.NET Core app to run on Lambda

The way I explained above(#2) is how to create a new ASP.NET Core app that can run as a Lambda function. In this section, I explain how to enable your EXISTED ASP.NET Core app to run on Lambda.

Installing a Nuget package

Firstly, you need to install a Nuget package Amazon.Lambda.AspNetCoreServer to your project that you want to deploy to AWS as a Lambda function.

dotnet add package Amazon.Lambda.AspNetCoreServer

Creating LambdaEntryPoint class

Next, please refer to the following code and create the LambdaEntryPoint class:

LambdaEntryPoint.cs

If you want to call ASP.NET Core app via API Gateway, you need to use APIGatewayProxyFunction abstraction class as the parent class of LambdaEntryPoint. If you want to call it via Application Load Balancer, you need to use ApplicationLoadBalancerFunction abstraction class. When

LambdaEntryPoint class inherits those classes, you need to develop Init method. So please make Init method to indicate Startup class (Startup.cs).

You will indicate FunctionHandlerAsync method of LambdaEntryPoint class as a handler of the Lambda function you will deploy. (You don’t need to develop FunctionHandlerAsync method by yourself because this method is already developed at AbstractAspNetCoreFunction class that is a parent of a parent of LambdaEntryPoint class)

Creating files for the definition of the deployment

Then, please create an “aws-lambda-tools-defaults.json” file and a “serverless.template” file for deploying your existed ASP.NET Core app to AWS.

aws-lambda-tools-defaults.json
serverless.template

“serverless.template” is a template of AWS Serverless Application Model (an extension of AWS CloudFormation). You can modify “serverless.template” according to this reference.

Deploying the project to AWS

Finally, as you did in the above section, you can deploy your existed ASP.NET Core app to AWS as a Lambda function by executing the following command.

dotnet lambda deploy-serverless

This deployment uses CloudFormation too, so Lambda function and other required resources like API Gateway will be deployed as CloudFormation stack.

Created CloudFormation stack sampleaspnetcoreappTimestamp Logical Resource Id Status— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —2020/07/06 14:00 sampleaspnetcoreapp CREATE_IN_PROGRESS2020/07/06 14:00 AspNetCoreFunctionRole CREATE_IN_PROGRESS2020/07/06 14:00 AspNetCoreFunctionRole CREATE_IN_PROGRESS2020/07/06 14:00 AspNetCoreFunctionRole CREATE_COMPLETE2020/07/06 14:01 AspNetCoreFunction CREATE_IN_PROGRESS2020/07/06 14:01 AspNetCoreFunction CREATE_IN_PROGRESS2020/07/06 14:01 AspNetCoreFunction CREATE_COMPLETE2020/07/06 14:01 ServerlessRestApi CREATE_IN_PROGRESS2020/07/06 14:01 ServerlessRestApi CREATE_IN_PROGRESS2020/07/06 14:01 ServerlessRestApi CREATE_COMPLETE2020/07/06 14:01 AspNetCoreFunctionProxyResourcePermissionProd CREATE_IN_PROGRESS2020/07/06 14:01 ServerlessRestApiDeploymentcfb7a37fc3 CREATE_IN_PROGRESS2020/07/06 14:01 AspNetCoreFunctionProxyResourcePermissionProd CREATE_IN_PROGRESS2020/07/06 14:01 AspNetCoreFunctionRootResourcePermissionProd CREATE_IN_PROGRESS2020/07/06 14:01 AspNetCoreFunctionRootResourcePermissionProd CREATE_IN_PROGRESS2020/07/06 14:01 ServerlessRestApiDeploymentcfb7a37fc3 CREATE_IN_PROGRESS2020/07/06 14:01 ServerlessRestApiDeploymentcfb7a37fc3 CREATE_COMPLETE2020/07/06 14:01 ServerlessRestApiProdStage CREATE_IN_PROGRESS2020/07/06 14:01 ServerlessRestApiProdStage CREATE_IN_PROGRESS2020/07/06 14:01 ServerlessRestApiProdStage CREATE_COMPLETE2020/07/06 14:01 AspNetCoreFunctionProxyResourcePermissionProd CREATE_COMPLETE2020/07/06 14:01 AspNetCoreFunctionRootResourcePermissionProd CREATE_COMPLETE2020/07/06 14:01 sampleaspnetcoreapp CREATE_COMPLETEStack finished updating with status: CREATE_COMPLETEOutput Name Value— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —ApiURL https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/Prod/

After completing the deployment, you can see the URL that enables us to access your web application.

--

--

Masayuki Hiyama

Solution Architect in Amazon Web Services Japan. My opinions are my own.