.NET 6 on AWS Lambda, Quick Demos

This is a quick post with the steps I was using to deploy three simple .NET 6 Lambda functions during .NET Enterprise Developer Day and DeveloperWeek Europe this week.

I’m not explaining things in this post, but if you want to find out more about .NET on AWS Lambda, please check my other posts on the topic.

Install tooling, create a bucket

dotnet tool install -g Amazon.Lambda.Tools
dotnet new -i Amazon.Lambda.Templates
aws s3api create-bucket —bucket cloudformation-templates-2022  # you need to use a unique bucket name

Note, you need a unique name for the bucket.

Demo 1 - ASP.NET Core Web API, on ARM 64

From the command line run -

dotnet new serverless.AspNetCoreMinimalAPI —name AspNetCoreMinimalAPI
cd AspNetCoreMinimalAPI/src/AspNetCoreMinimalAPI

Open the project in your favorite IDE.

Open the Program.cs file.

Replace the line with app.MapGet(..) with the below -

var architecture = System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture;
var dotnetVersion = Environment.Version.ToString();
app.MapGet("/", () => $"Hello AWS Serverless. Architecture: {architecture}, .NET Version: {dotnetVersion}");

Open the serverless.template file.

Under “Properties” add the following -

"Architectures": ["arm64"],

Deploy the Lambda function using -

dotnet lambda deploy-serverless —stack-name AspNetCoreMinimalAPI  —s3-bucket cloudformation-templates-2022

When the deployment is complete you will get a URL, open that in your browser.

Demo 2 - ASP.NET Core Web API in a container running on Lambda

From the command line run -

dotnet new serverless.image.AspNetCoreWebAPI —name AspNetCoreWebAPIContainer
cd AspNetCoreWebAPIContainer/src/AspNetCoreWebAPIContainer 

Open the project in your favorite IDE.

Open the Startup.cs file.

Change the line that starts with await context.Response.WriteAsync.. to

await context.Response.WriteAsync("Hello from inside a .NET 6 Web API running in a container, in a Lambda!");

Back at the command line run -

dotnet lambda deploy-serverless —stack-name AspNetCoreWebAPIContainer —s3-bucket cloudformation-templates-2022

When the deployment is complete you will get a URL, open that in your browser.

Demo 3 - Lambda Function URL demo

From the command line run -

dotnet new lambda.EmptyFunction —name FunctionUrlExample
cd FunctionUrlExample\src\FunctionUrlExample
dotnet add package Amazon.Lambda.APIGatewayEvents

Open the project in your favorite IDE.

Create a new class called Person.

 
public class Person
{
    public string FirstName { get; init; }
    public string LastName { get; init; }
    public int Age { get; init; }
}

Add three using statements to the top of the Function.cs file -

 
using Amazon.Lambda.APIGatewayEvents;
using System.Text.Json;
using System.Text.Json.Serialization;

And change the FunctionHandler(..) method to this -

 
public string FunctionHandler(APIGatewayHttpApiV2ProxyRequest request, ILambdaContext context)
{
    var serializationOptions = new JsonSerializerOptions
    {
        PropertyNameCaseInsensitive = true,
        NumberHandling= JsonNumberHandling.AllowReadingFromString
    };

    Person person = JsonSerializer.Deserialize<Person>(request.Body, serializationOptions); 

    return $"Hello {person.FirstName} {person.LastName}, you are {person.Age} years old.";
}

Deploy the function

From the command line run -

dotnet lambda deploy-function FunctionUrlExample 

You will be asked - “Select IAM Role that to provide AWS credentials to your code:”, at the bottom of the list will be “*** Create new IAM Role ***”, enter that number.

You will then be asked - “Enter name of the new IAM Role:”, put in “FunctionUrlExampleRole”.

Then you will be asked to - “Select IAM Policy to attach to the new role and grant permissions”, select “AWSLambdaBasicExecutionRole”, for me it is number 6 on the list.

Configure the Function URL

From the command line, run -

aws lambda create-function-url-config --function-name FunctionUrlExample --auth-type NONE
aws lambda add-permission --function-name FunctionUrlExample --statement-id AuthNone --action lambda:InvokeFunctionUrl --principal * --function-url-auth-type NONE

Now the Lambda function can be accessed from the URL with no authentication needed.

Make a POST request to the URL

Using Fiddler, Postman, Rest Client for VS Code Curl, etc, make a POST request.

This is the sample for Rest Client VS Code -

POST https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.lambda-url.us-east-1.on.aws/ HTTP/1.1
content-type: application/json

{
    "firstname": "Alan",
    "lastname": "Adams",
    "age": "25"
}
comments powered by Disqus

Related