1

I'm having trouble running my .NET 7 Lambda locally as a Docker container.

There seems to be loads online about running python or JS lambdas, but not a lot on .NET.

I can't specify the entrypoint of the Lambda in the Docker container.

I'm using the following Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build-env
WORKDIR /App

# Copy everything
COPY . ./
# Restore as distinct layers
RUN dotnet restore
# Build and publish a release
RUN dotnet publish -p:PublishReadyToRun=false -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:7.0
WORKDIR /App
COPY --from=build-env /App/out .
ENTRYPOINT ["dotnet", "LambdaLocal.dll"]

as described here: https://learn.microsoft.com/en-us/dotnet/core/docker/build-container?tabs=windows (though this is for a console app)

My Docker commands are: docker build -t local-lambda-image -f Dockerfile .

and then: docker run --rm -p 9000:8080 --name local-lamba-cont local-lambda-image

(taken from here: https://medium.com/dataengineerbr/how-to-run-aws-lambda-locally-on-your-computer-with-docker-containers-533a3add1b45)

When I try and run the container, the error is "Unhandled exception. System.MissingMethodException: Entry point not found in assembly 'LambdaLocal, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'."

I've also tried docker run -p 9000:8080 --name local-lambda-container local-lambda-image "LambdaLocal::LambdaLocal.Function::FunctionHandler" as suggested in this answer Locally test AWS Lambda container with .NET 5 web api and Lambda RIE , but I get the same error.

How do I specify the entrypoint for a .NET Lambda?

1
  • To be clear, the Medium post you referenced was working with Lambda written in Python. The .Net 7 runtime version of Lambda is kicked off differently. Commented May 18, 2023 at 1:30

1 Answer 1

1

I am assuming you are using the latest templates from AWS, so my answer will be based on what should be their outputs.

First thing, double check that your aws-lambda-tools-default.json has this entry:

"package-type": "image"

I also removed these entries from ours:

"image-command": "LambdaLocal::LambdaLocal.Function::FunctionHandler",
"docker-host-build-output-dir": "./bin/Release/lambda-publish"

and added the following to my Dockerfile:

FROM public.ecr.aws/lambda/dotnet:7 AS base
WORKDIR /var/task
COPY bin/Debug/net7.0/linux-x64/publish .
EXPOSE 8080

In your case, you are building the assets inside of the Dockerfile, where I am building the assets outside of Docker as part of a solution file. But the .Net 7 Lambda function is intended to run within AWS's Amazon Linux 2 image (what this image is based on) and it expects the assets to be in the /var/task folder. You could multi-stage the build within the Dockerfile, like you are doing, with this image instead without too much effort.

Next, in your Dockerfile, remove the CMD line and replace it with this:

ENTRYPOINT ["/lambda-entrypoint.sh", "LambdaLocal::LambdaLocal.Function::FunctionHandler"]

The Lambda RIE should be picking up the image-command (and others) from the JSON settings but that wasn't my experience.

That should get your instance up and running, though you might run into another issue dealing with the default serializer. If you do, take a look at this and you should be able to address it: Improved AWS Lambda JSON Serialization in C#

It may or may not be "improved" but it does get it to work, whereas we were seeing a bug with the serializer utilizing Microsoft's System.Text.Json namespace, instead of Newtonsoft.

This is, effectively, all the steps I had to go through to get our .Net 7 Lambda instances up and functional locally within a container.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.