Handling HTTP Requests with .NET 7 Native AOT on AWS Lambda

Want to learn more about AWS Lambda and .NET? Check out my A Cloud Guru course on ASP.NET Web API and Lambda.

If you have tried out native AOT with .NET 7, you will probably have seen the Microsoft documentation page that states - “it is currently not supported with ASP.NET Core, but only console apps”. So no Web API for now!

But, there are a couple of ways to create .NET 7 native AOT applications that handle HTTP requests from anywhere in the world with the AWS Lambda service. Even though you are not using a familiar controller or endpoint, you can achieve the same result.

In this post, I’ll show the simplest approach that uses an out-of-the-box template, and in a later post, I will show how to use Function URLs with native AOT Lambda functions.

If you want to find out more about .NET native AOT with Lambda functions, check out my previous post where I compared Lambda cold start times for the .NET 6 managed runtime, with the native AOT .NET 7 custom runtime. .NET 7 with native AOT had significantly faster cold start times.

Get the tools

Install or update the latest tooling, this lets you deploy, and run Lambda functions.

dotnet tool install -g Amazon.Lambda.Tools
dotnet tool update -g Amazon.Lambda.Tools

Install or update the latest Lambda function templates.

dotnet new --install Amazon.Lambda.Templates

Install .NET 7.

Install and run Docker Desktop.

Create the S3 bucket

The deployment tool needs an S3 bucket to store its template. Follow the instructions here to create an S3 bucket.

Create a new project

From the command line run -

dotnet new serverless.NativeAOT -n ServerlessNativeAOT

Change to the ServerlessNativeAOT/src/ServerlessNativeAOT directory and open up that project in your favorite IDE.

The project you just created contains two function handlers, one for the GET verb and one for the PUT verb.

Open the Functions.cs file, it contains the code that will be executed when the GET and PUT requests invoke the Lambda functions.

The code for each is very simple, and you don’t need to change anything.

Deploy the Lambda function

From the command line run -

dotnet lambda deploy-serverless --stack-name ServerlessNativeAOT --s3-bucket YOUR-BUCKET-NAME

This will build and deploy the application. It will take a while, especially the first time you run it as it downloads the Docker image to perform the build in.

Once it deploys, you will see a message like this -

Output Name                    Value
------------------------------ --------------------------------------------------
ApiURL                         https://xxxxxxxx.execute-api.us-east-1.amazonaws.com/Prod/

Testing it out

To exercise the GET function, open a browser and navigate to the ApiURL value from the previous step.

To exercise the PUT function, use a tool like Fiddler, Postman, Rest Client, etc to send a PUT request to the ApiURL value from the previous step.

Here is an example of a PUT request you can use with Rest Client for VS Code -

PUT https://xxxxxxxx.execute-api.us-east-1.amazonaws.com/Prod/ HTTP/1.1
content-type: application/json

{
}
comments powered by Disqus

Related