.NET 6 Web API in a Container in a Lambda

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

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

Introduction

At the time of posting, AWS Lambda does not support .NET 6 with a managed runtime. But it is still very easy to deploy a .NET 6 Lambda Web API as a container image. In this post, you will see how with just a few commands you can build and deploy a Web API application in a container, in a Lambda, and make it accessible over the web!

You don’t even need Visual Studio, VS Code, or Rider!!

If your goal is to use managed runtime when it is available, the only change you will need to make is to use a different .NET template (Docker won’t be necessary either).

tl;dr just show me the commands!

Prerequisites

  1. You need an AWS account, you can get one for free.
  2. Install and configure the AWS CLI.
  3. The .NET 6 SDK.
  4. Docker.

Installing the tools

Install the Amazon Lambda tools - dotnet tool install -g Amazon.Lambda.Tools

And the Amazon Lambda Templates - dotnet new -i Amazon.Lambda.Templates

After they are installed you can see all the available .NET 6 Lambda templates using -

dotnet new ".NET 6" --list --tag Lambda

Template Name                                         Short Name                                    Language  Tags
----------------------------------------------------  --------------------------------------------  --------  ---------------------
Lambda ASP.NET Core Web API (.NET 6 Container Image)  serverless.image.AspNetCoreWebAPI             [C#],F#   AWS/Lambda/Serverless
Lambda Custom Runtime Function (.NET 6)               lambda.CustomRuntimeFunction                  [C#],F#   AWS/Lambda/Function
Lambda Empty Function (.NET 6 Container Image)        lambda.image.EmptyFunction                    [C#],F#   AWS/Lambda/Function
Lambda Empty Serverless (.NET 6 Container Image)      serverless.image.EmptyServerless              [C#],F#   AWS/Lambda/Serverless

You will be using “Lambda ASP.NET Core Web API (.NET 6 Container Image) serverless.image.AspNetCoreWebAPI”, the first on the list. But there are a couple of steps before you create the application.

Create the S3 bucket

The deployment tool needs an S3 bucket to store its template. Create that using -

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.

Start Docker

If you have Docker installed, start it. If not, install and start it.

Create the Web API application

Now that you have the Lambda templates you can create the Lambda container based .NET 6 Web API application with a simple command -

dotnet new serverless.image.AspNetCoreWebAPI --name AspNet6ContainerWebApi

Build and deploy

Change to the source code directory - cd AspNet6ContainerWebApi\src\AspNet6ContainerWebApi

To see the deployment options run - dotnet lambda

Commands to deploy and manage AWS Lambda functions:

        deploy-function         Command to deploy the project to AWS Lambda
        invoke-function         Command to invoke a function in Lambda with an optional input
        list-functions          Command to list all your Lambda functions
        delete-function         Command to delete a Lambda function
        get-function-config     Command to get the current runtime configuration for a Lambda function
        update-function-config  Command to update the runtime configuration for a Lambda function

Commands to deploy and manage AWS Serverless applications using AWS CloudFormation:

        deploy-serverless       Command to deploy an AWS Serverless application
        list-serverless         Command to list all your AWS Serverless applications
        delete-serverless       Command to delete an AWS Serverless application
snip...

“deploy-serverless” is what you need. It takes a couple of parameters, the name of the CloudFormation stack, and the name of the S3 bucket created earlier -

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

If everything is working you will see something like -

Created CloudFormation stack AspNet6ContainerWebApi

Timestamp            Logical Resource Id                      Status
-------------------- ---------------------------------------- ----------------------------------------
1/6/2022 11:07 AM    AspNet6ContainerWebApi                   CREATE_IN_PROGRESS
1/6/2022 11:07 AM    AspNetCoreFunctionRole                   CREATE_IN_PROGRESS
1/6/2022 11:07 AM    AspNetCoreFunctionRole                   CREATE_IN_PROGRESS
1/6/2022 11:07 AM    AspNetCoreFunctionRole                   CREATE_COMPLETE
1/6/2022 11:07 AM    AspNetCoreFunction                       CREATE_IN_PROGRESS
1/6/2022 11:07 AM    AspNetCoreFunction                       CREATE_IN_PROGRESS
snip...
Stack finished updating with status: CREATE_COMPLETE

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

The last line of the output is the URL of the API, it is publicly available.

Up and running

Use your favorite browser to test out the API.

Grab the ApiURL from above (replacing “xxxxxxxxxx” with the real URL) and open -

  • https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/Prod/ you will see “Welcome to running ASP.NET Core on AWS Lambda”

  • https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/Prod/api/values - you will see “[“value1”,“value2”]”

Cleaning up

To delete all the resources you created, run -

dotnet lambda delete-serverless --stack-name AspNet6ContainerWebApi.

Just the commands

If all you want are the commands to do everything quickly, here you are -

dotnet tool install -g Amazon.Lambda.Tools

dotnet new -i Amazon.Lambda.Templates

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

dotnet new serverless.image.AspNetCoreWebAPI --name AspNet6ContainerWebApi

cd AspNet6ContainerWebApi\src\AspNet6ContainerWebApi

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

# clean up
# dotnet lambda delete-serverless --stack-name AspNet6ContainerWebApi
comments powered by Disqus

Related