C# and AWS Lambdas, Part 8 - .NET 6, inside a Container, inside a Lambda
Full source code available here.
- Part 1 - Hello World
- Part 2 - Web API and an API Gateway
- Part 3 - Pulumi IaC for Web API and an API Gateway
- Part 4 - Storing the Zip in S3, Setup with Pulumi IaC
- Part 5 - Updating the Zip in S3 and Updating the Running Lambda, with Pulumi IaC
- Part 6 - .NET 5 inside a Container inside a Lambda
- Part 7 - .NET 5 Web API inside a Container inside a Lambda, with API Gateway in front
- Part 8 - .NET 6, inside a Container, inside a Lambda
If you want to see the simplest way to get .NET 6 in a container working on Lambda, check this newer post - ".NET 6 Web API in a Container in a Lambda".
After playing with .NET 5 libraries and Web API applications inside Lambda containers I wanted to see if I could get a .NET 6 library running too.
The Dockerfile pulls down a .NET runtime from https://dotnetcli.azureedge.net/dotnet.
Grabs an image to build the internal .NET support into the Lambda -
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS builder
Updating those two elements should be all that is needed.
.NET 6 Lambda Image
I updated the runtime to the latest .NET 6 preview, available from https://versionsof.net/core/6.0/6.0.0-preview.2/. In the attached example, I use the .NET 6 Runtime, but you might want to use the .NET 6 ASP.NET Runtime.
I also updated the builder to .NET 6.
FROM mcr.microsoft.com/dotnet/nightly/sdk:6.0.100-preview.2-bullseye-slim-amd64 AS builder
To build the image run this inside the
Dotnet6LambdaImage directory -
docker build -t dotnet6_runtime_aws_lambda .
That’s that, you now have a base image to run .NET 6 inside a Lambda container. Now to the library.
You can create a simple .NET Lambda application using a command like -
dotnet new lambda.EmptyFunction --name Dotnet6LambdaLibrary
You’ll need to update the
.csproj file to make the target framework net6.0.
The attached example has stripped away the tests and the directory structure.
Dockerfile that looks like this to the directory where the
.csproj file is -
1FROM dotnet6_runtime_aws_lambda as base 2 3# https://hub.docker.com/_/microsoft-dotnet-sdk/ 4FROM mcr.microsoft.com/dotnet/nightly/sdk:6.0.100-preview.2-alpine3.13-amd64 as build 5 6WORKDIR /source 7 8# there is no point doing the restore as its own step because the dotnet publish doesn't work with the --no-restore flag. 9# COPY *.csproj . 10# RUN dotnet restore 11 12COPY . . 13RUN dotnet publish -c Release -o /app/publish 14 15FROM base AS final 16WORKDIR /var/task 17COPY --from=build /app/publish . 18CMD ["Dotnet6LambdaLibrary::Dotnet6LambdaLibrary.Function::FunctionHandler"]
The first line sets the image built above as the base. The last line tells the Lambda what to call when running the function. The rest in the middle are fairly standard Docker commands.
From inside the
Dotnet6LambdaLibrary directory, build the library -
docker build -t dotnet6dockerlambdalibrary .
Deploy to AWS as shown in part 6 of this series.
Test as shown in part 1 of this series.
If you want to build and deploy .NET 6 Web API applications to Lambda containers, change the runtime that is pulled down in
Dotnet6LambdaImage/Dockerfile to a more suitable one found here https://versionsof.net/core/6.0/6.0.0-preview.2/, then check out part 7 of this series to see how to hook up a HTTP Gateway to the Lambda.
I think that wraps up this series on C# and .NET in AWS Lambdas, I thought I was going to post four blogs on the topic, but ended up with eight. Hope they are helpful.
Full source code available here.
- C# and AWS Lambdas, Part 7 – .NET 5 Web API inside a Container inside a Lambda, with API Gateway in front
- C# and AWS Lambdas, Part 6 – .NET 5 inside a Container inside a Lambda
- Testing .NET 6 Lambda Containers with the Runtime Interface Emulator
- .NET 6 Web API in a Container in a Lambda
- Using Entity Framework for .NET 6 with SQL Server in a Docker Container