.NET 6 Lambdas on ARM64 - Part 2, Serverless

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

Download full source code.

This is part two of a short series of posts on deploying .NET 6 Lambdas on ARM64 processors.

In part 1, I covered simple Lambda functions (templates that start with lambda.), and in this post, I’ll talk about serverless functions (templates that start with serverless.).

Please read part 1 to get some background, find out about the two types of templates, and download the required tools/templates.

I have a few other posts on running .NET 6 on AWS Lambda.

Moving to ARM64

If you are using the lambda. templates, a change is needed to aws-lambda-tools-defaults.json, this was covered in the previous post.

If you are using the serverless. templates, a change is needed to the serverless.templates file, this is covered below.

Creating the application

Create a simple Lambda function using the serverless.EmptyServerless template -

dotnet new serverless.EmptyServerless --name EmptyServerlessArm64

Edit the deployment configuration

Navigate to the EmptyServerlessArm64\src\EmptyServerlessArm64 directory.

Open the serverless.template file and add this under “Properties” -

"Architectures": ["arm64"],

Your file should look something like -

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Transform": "AWS::Serverless-2016-10-31",
  "Description": "An AWS Serverless Application.",
  "Resources": {
    "Get": {
      "Type": "AWS::Serverless::Function",
      "Properties": {
        "Handler": "EmptyServerlessArm64::EmptyServerlessArm64.Functions::Get",
        "Architectures": ["arm64"],
        "Runtime": "dotnet6",
        snip...
}

Update the Get method

Open Function.cs and replace the Get(..) method with this -

 1public APIGatewayProxyResponse Get(APIGatewayProxyRequest request, ILambdaContext context)
 2{
 3    context.Logger.LogInformation("Get Request\n");
 4    var architecture = System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture;
 5    var dotnetVersion = Environment.Version.ToString();
 6
 7    var response = new APIGatewayProxyResponse
 8    {
 9        StatusCode = (int)HttpStatusCode.OK,
10        Body = $"Hello AWS Serverless. Architecture: {architecture}, .NET Version: {dotnetVersion}",
11        Headers = new Dictionary<string, string> { { "Content-Type", "text/plain" } }
12    };
13
14    return response;
15}
Build

Try building the code before moving on to deploying the application.

Deploy

Before you can deploy the Lambda, you need an S3 bucket for the CloudFormation stack that will be created.

aws s3api create-bucket --bucket cloudformation-templates-2022

Note, you must use a unique name for the bucket, you can’t use the one I used.

Deploy using -

dotnet lambda deploy-serverless --stack-name EmptyServerlessArm64 --s3-bucket cloudformation-templates-2022 # remember, you need a unique name 

Wait a little while as the cloud resources are created. When it’s done you will see a URL for your deployed application.

Timestamp            Logical Resource Id                      Status
-------------------- ---------------------------------------- ----------------------------------------
3/8/2022 1:43 PM     GetRole                                  CREATE_IN_PROGRESS
3/8/2022 1:43 PM     GetRole                                  CREATE_IN_PROGRESS
3/8/2022 1:44 PM     GetRole                                  CREATE_COMPLETE
3/8/2022 1:44 PM     Get                                      CREATE_IN_PROGRESS
snip...
Stack finished updating with status: CREATE_COMPLETE

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

Open the ApiRUL in your favorite browser to see a friendly message.

Architecture: Arm64, .NET Version: 6.0.1. Hello AWS Serverless.

Clean up

dotnet lambda delete-serverless --stack-name EmptyServerlessArm64

More resources

For lots more on .NET on AWS - https://aws.amazon.com/dotnet

Follow the team on Twitter - @dotnetonaws

Download full source code.

comments powered by Disqus

Related