.NET 6 Web API on Lambda with a Custom Runtime
Want to learn more about AWS Lambda and .NET? Check out my A Cloud Guru course on ASP.NET Web API and Lambda.
I have a few other posts on running .NET 6 on AWS Lambda.
Introduction
At the time of posting, AWS Lambda does not support .NET 6 with a managed runtime. But there are two other options, putting your code in a container image as I showed last month, or using the custom runtime, which is what this post will explain.
If you want to know a little more about managed and custom runtimes, see this earlier post.
The work here is entirely based on a sample by Norm Johanson - https://github.com/normj/LambdaNETCoreSamples/.
tl;dr just show me the commands!
Prerequisites
- You need an AWS account, you can get one for free.
- Install and configure the AWS CLI.
- The .NET 6 SDK.
Installing the tools
Install the Amazon Lambda tools with the following command -
dotnet tool install -g Amazon.Lambda.Tools
These will be used to deploy the application to AWS.
Create the S3 bucket
The deployment tool needs an S3 bucket to store its template. Create that using -
aws s3api create-bucket --bucket cloudformation-templates-2022
Note, you must use a unique name for the bucket, you can’t use the one shown here.
Check the region of the bucket by calling -
aws s3api get-bucket-location --bucket cloudformation-templates-2022
If you get back a null
, like below, it means the bucket is in us-east-1
, otherwise, a region will be listed -
{
"LocationConstraint": null
}
If you want to specify another location for the bucket when creating it, you can do so with -
aws s3api create-bucket --bucket cloudformation-templates-2022 --create-bucket-configuration LocationConstraint=REGION
Clone the code
Clone Norm’s repository, I use git from the command line -
git clone git@github.com:normj/LambdaNETCoreSamples.git
#or git clone https://github.com/normj/LambdaNETCoreSamples.git
Build and deploy
Change to the directory of the custom runtime example -
cd LambdaNETCoreSamples/CustomRuntimeNET6AspNetCore
Open the aws-lambda-tools-defaults.json
file, make the region specified match the one where the bucket is located (remember that if you got null, your bucket is in us-east-1
).
All that’s left is to deploy -
dotnet lambda deploy-serverless --stack-name CustomRuntimeNET6AspNet --s3-bucket cloudformation-templates-2022
If everything is working you will see something like -
Timestamp Logical Resource Id Status
-------------------- ---------------------------------------- ----------------------------------------
2/9/2022 3:30 PM AspNetCoreFunctionRole CREATE_IN_PROGRESS
2/9/2022 3:30 PM AspNetCoreFunctionRole CREATE_IN_PROGRESS
2/9/2022 3:30 PM AspNetCoreFunctionRole CREATE_COMPLETE
2/9/2022 3:30 PM AspNetCoreFunction CREATE_IN_PROGRESS
2/9/2022 3:30 PM AspNetCoreFunction CREATE_IN_PROGRESS
2/9/2022 3:30 PM AspNetCoreFunction CREATE_COMPLETE
2/9/2022 3:30 PM ServerlessHttpApi CREATE_IN_PROGRESS
snip..
Stack finished updating with status: CREATE_COMPLETE
Output Name Value
------------------------------ --------------------------------------------------
ApiURL https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/
The last line of the output is the URL of the API, it is publicly available.
Up and running
Use your favorite browser to test out the API.
Grab the ApiURL
from above (replacing “xxxxxxxxxx” with the real URL) and open -
https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/
you will see “Welcome to running ASP.NET Core on AWS Lambda”https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/api/values
- you will see “[“value1”,“value2”]”
Cold start
Cold start time is always a consideration with Lambdas, and especially with the custom runtime. If you are concerned about this, try using the trimming and ready to run features of .NET - see here and here for more.
Cleaning up
To delete all the resources you created, run -
dotnet lambda delete-serverless --stack-name CustomRuntimeNET6AspNet
Just the commands
If all you want are the commands to do everything quickly, here you are -
dotnet tool install -g Amazon.Lambda.Tools
aws s3api create-bucket --bucket cloudformation-templates-2022
git clone https://github.com/normj/LambdaNETCoreSamples.git
cd LambdaNETCoreSamples/CustomRuntimeNET6AspNetCore
dotnet lambda deploy-serverless --stack-name CustomRuntimeNET6AspNet --s3-bucket cloudformation-templates-2022
# clean up
# dotnet lambda delete-serverless --stack-name CustomRuntimeNET6AspNet