C# and AWS Lambdas, Part 8 – .NET 6, inside a Container, inside a Lambda

Full source code available here.

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.

I cloned https://github.com/aws/aws-lambda-dotnet, and started digging into this Dockerfile.
With a few minor alterations this should be all I needed to change.

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 (but I found this wasn’t strictly necessary).

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.

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.

Add a Dockerfile that looks like this to the directory where the .csproj file is –

FROM dotnet6_runtime_aws_lambda as base

# https://hub.docker.com/_/microsoft-dotnet-sdk/
FROM mcr.microsoft.com/dotnet/nightly/sdk:6.0.100-preview.2-alpine3.13-amd64 as build

WORKDIR /source

# there is no point doing the restore as it's own step because the donet publish doesn't work with the --no-restore flag.
# COPY *.csproj .
# RUN dotnet restore

COPY . .
RUN dotnet publish -c Release -o /app/publish

FROM base AS final
WORKDIR /var/task
COPY --from=build /app/publish .
CMD ["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 Lamnbda.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *