Often touted as a solution for microservices, Minimal APIs are a new feature in ASP.NET Core that allows developers to easily create HTTP based APIs with minimal dependencies. On top of that, ASP.NET Core minimal APIs are simple, only requiring a csproj file and a Program.cs file.

Photo by Jakub Mičuch on Unsplash
The Solution
In this tutorial, well take a look at using ASP.NET Core minimal APIs as a way to build AWS Lambda functions. We’ll first develop the ASP.NET Core minimal API and then we’ll make a small change that turns the minimal API into a Lambda Function.
Remember, for any example solution from AWS with .NET, we focus on the code that exemplifies the problem we are trying to solve. We don’t include logging, input validation, exception handling, etc., and we embed the configuration data within classes instead of using environment variables, configuration files, key/value stores and the like. These items should not be skipped for proper solutions.
Prerequisites
To complete this tutorial, you will need the .NET CLI which is included in the .NET 6 SDK. In addition, you will need to download the AWS CLI and configure your environment.
Warning: some AWS services may have fees associated with them.
Our Dev Environment
This tutorial was developed using Ubuntu 20.04, AWS CLI v2, .NET 6 SDK and Visual Studio Code 1.66.2. Some commands/constructs may very across systems.
Developing the .NET Minimal API
The first thing we need to do is create an empty web project using the following command:
$ dotnet new web -n MinimalLambda
Let’s now go into the Program.cs file of the new web project and create an endpoint for the API. Add the following code just below the app variable declaration to create an endpoint that supports the GET method at the “/values” path. Essentially, this endpoint will read any values from the querystring parameters and surface them as an array of strings.
app.MapGet("/values", (HttpRequest request) => { | |
return request?.Query?.Select(s=> s.Value.ToString()).ToArray() ?? new string[0]; | |
} | |
); |
The Program.cs file should now look like so:
var builder = WebApplication.CreateBuilder(args); | |
var app = builder.Build(); | |
app.MapGet("/values", (HttpRequest request) => { | |
return request?.Query?.Select(s=> s.Value.ToString()).ToArray() ?? new string[0]; | |
} | |
); | |
app.Run(); |
Let’s test the minimal API.
First, we start the app with the following command:
$ dotnet run ––project ./MinimalApi
*Note, here we have configured the app to run on port 5001. Your app port may very.
Now that the app is running, Let’s test the endpoint by browsing to: https://localhost:5001/values?key1=value1&key2=value2
You should see the following response:
[
"value1",
"value2"
]
OK, now we have a functioning minimal API, but how do we create a Lambda function from this minimal API?
Creating the Lambda Function
Step one in creating the Lambda function is to pull in a Nuget package that is developed by AWS. This package allows Kestrel, the cross-platform web server for ASP.NET Core, to run the application via the .NET CLI. When the app is executed as a Lambda function in AWS, Amazon.Lambda.AspNetCoreServer.Hosting steps in to handle the execution of the application, relieving Kestrel of its duties.
Amazon.Lambda.AspNetCoreServer.Hosting can be added to the project with the following command:
$ dotnet add package Amazon.Lambda.AspNetCoreServer.Hosting
With the Nuget in place, let’s now make one small modification to our app. Let’s make a call to AddAWSLambdaHosting just below our builder variable declaration to complete the Program.cs file.
var builder = WebApplication.CreateBuilder(args); | |
builder.Services.AddAWSLambdaHosting(LambdaEventSource.HttpApi); | |
var app = builder.Build(); | |
app.MapGet("/values", (HttpRequest request) => { | |
return request?.Query?.Select(s=> s.Value.ToString()).ToArray() ?? new string[0]; | |
} | |
); | |
app.Run(); |
That’s it. Your ASP.NET Core Minimal API based Lambda function is ready to be packaged and uploaded.
Once packaged and uploaded, navigate to the “values” endpoint by using your Lambda function’s URL:
https://(lambda-url-fqdn)/values?key1=value1&key2=value2
And, just like in your development environment, you should see the following response:
[
"value1",
"value2"
]
Packaging and Uploading the Lambda Function
Checkout this article on packaging and uploading an AWS Lambda function that takes you through, step-by-step, on how to create a Lambda function, create an HTTPS endpoint for the Lambda Function and then packaging and uploading the Lambda Function.
Summary
We have concluded this tutorial where you have learned how to build a .NET Lambda function using an ASP.NET Core Minimal API.
Challenges
Now that you have a working solution, let’s take things a little further…
- Change the URL by removing all of the querystring parameters to see how the response of the endpoint changes.
- Create another endpoint that surfaces the keys of the querystring parameters.
Want to know more about the tech in this article? Checkout these resources:
.NET CLI, AWS CLI, Configuring the AWS CLI, ASP.NET Core Minimal APIs, .NET 6 runtime for AWS Lambda.