We will create a minimal API using
AWS Lambda and
.NET 6. It will be a simple API with a single MapGet method and a single controller which is a simple calculator class with four methods.
Creating a new project
We are going to use the
serverless.AspNetCoreMinimalAPI template. You can create a new project using the following command:
dotnet new serverless.AspNetCoreMinimalAPI --name MinimalLambda --output MinimalLambda
or using the Rider IDE.
Configuring the project
We need to configure
This time we will use
MinimalLambda. Then we can remove
Looking at the code
Program.cs file, we have a
CreateHostBuilder method. This method will create a new
var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); // Add AWS Lambda support. When application is run in Lambda Kestrel is swapped out as the web server with Amazon.Lambda.AspNetCoreServer. This // package will act as the webserver translating request and responses between the Lambda event source and ASP.NET Core. builder.Services.AddAWSLambdaHosting(LambdaEventSource.RestApi); var app = builder.Build(); app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.MapGet("/", () => "Welcome to running ASP.NET Core Minimal API on AWS Lambda"); app.Run();
LambdaEventSource.RestApi is the event source that will be used to handle the request. If we change it to
LambdaEventSource.HttpApi, we will be able to use the new HTTP API.
In the Controllers folder, we have a
CalculatorController.cs file. This file contains a
Calculator class with four methods. We can use these methods to perform simple calculations.
Deploying the project
Now that we have created the project, we can deploy it to AWS Lambda. Go to the project directory and run the following command:
dotnet lambda deploy-function
"function-url-enable": true in
aws-lambda-tools-defaults.json file, we can get the function URL after deploying the project.
Now we can use the function URL to access the API.
They can be accessed using the following URLs:
Postman, we can send a request to these URLs and get the result.
You have created a minimal API using
AWS Lambda and