Deploy to AWS
The functions deploy as one container image and one CloudFormation stack. src/Presentation/serverless.template has a Lambda function for each handler method, and Lambda Annotations keeps it in step every time you build. The Amazon.Lambda.Tools CLI builds the image, pushes it to Amazon ECR and creates or updates the stack in one go.
-
Install the CLI.
Terminal window dotnet tool install -g Amazon.Lambda.Tools -
Fill in
src/Presentation/aws-lambda-tools-defaults.json. The template leavesprofile,regionandstack-nameempty on purpose. We don’t know where you live.{"profile": "default","region": "ap-southeast-2","configuration": "Release","template": "serverless.template","stack-name": "worksonmymachine-movies"} -
Deploy.
Terminal window dotnet lambda deploy-serverless --project-location ./src/PresentationEach function gets the basic execution role, so it can write to CloudWatch Logs and nothing else.
-
Invoke it for real. CloudFormation names each function after the stack and the resource, so look the name up first.
Terminal window FUNCTION=$(aws cloudformation describe-stack-resource --stack-name worksonmymachine-movies \--logical-resource-id GetMovies --query StackResourceDetail.PhysicalResourceId --output text)aws lambda invoke --function-name "$FUNCTION" \--cli-binary-format raw-in-base64-out --payload '{"limit":5}' response.jsonOr call it over HTTP. The stack’s
ApiUrloutput is the API Gateway address.Terminal window API_URL=$(aws cloudformation describe-stacks --stack-name worksonmymachine-movies \--query "Stacks[0].Outputs[?OutputKey=='ApiUrl'].OutputValue" --output text)curl "${API_URL}movies?limit=5"
One image, many functions
Section titled “One image, many functions”Each Lambda function runs one handler method. The stack already has GetMovies, GetAuthors and GetReviews, plus their HTTP twins HttpGetMovies, HttpGetAuthors and HttpGetReviews, all running the same image with a different command. They share the code but scale and bill separately. Add a method with [LambdaFunction], build, and the next deploy adds its function too.
Send the telemetry somewhere
Section titled “Send the telemetry somewhere”Logs land in CloudWatch Logs whatever you do. For traces and metrics too, give the function a collector and set OTEL_EXPORTER_OTLP_ENDPOINT to it. Container images can’t use Lambda layers, so either build the AWS Distro for OpenTelemetry collector extension into the image, or run a collector as a gateway the function can reach. The collector then decides where everything goes, X-Ray included, and changing your mind never means touching the function. otel-collector.yaml is a good starting point for its configuration.
Settings worth a look
Section titled “Settings worth a look”| Setting | Template default | Why you might change it |
|---|---|---|
MemorySize on [LambdaFunction] |
256 MB | Lambda gives you CPU in proportion to memory, so more memory often means a faster cold start |
Timeout on [LambdaFunction] |
30 seconds | The handler stops its work when the invocation is about to run out of time, so set this to what your callers will wait for |