There are three project files to review: Dockerfile, aws-lambda-tools-defaults.json, and Function.cs.
The following code shows the Dockerfile which is created by using the selected blueprint. It performs
WORKDIR /var/task
# This COPY command copies the .NET Lambda project's build artifacts from the host machine into the image.
# The source of the COPY should match where the .NET Lambda project publishes its build artifacts. If the Lambda function is being built
# with the AWS .NET Lambda Tooling, the `--docker-host-build-output-dir` switch controls where the .NET Lambda project
# will be built. The .NET Lambda project templates default to having `--docker-host-build-output-dir`
# set in the aws-lambda-tools-defaults.json file to "bin/Release/net5.0/linux-x64/publish".
#
# Alternatively Docker multi-stage build could be used to build the .NET Lambda project inside the image.
# For more information on this approach checkout the project's README.md file.
COPY "bin/Release/net5.0/linux-x64/publish" .
To further customize your Dockerfile, you could also utlize:
• ENTRYPOINT: The base image already includes an ENTRYPOINT, which is the startup process executed when the image is started. If you wish to specify your own, then you are overriding that base entry point.
• CMD: CMD instructs AWS which custom code you want executed. It expects a fully-qualified name to your custom method. This line either needs to be included directly in the Dockerfile or can be specified during the publish process.
# Example of alternative way to specify the Lambda target method rather than during the publish process.
CMD [ "AWSLambdaDocker::AWSLambdaDocker.Function::FunctionHandler"]
Examine the aws-lambda-tools-defaults.json file.
• Field docker-host-build-output-dir sets the output directory of the build process that correlates with the instructions in the Dockerfile.
• Field image-command is a fully-qualified name to your method, the code you want the Lambda function to run. The syntax is: {Assembly}::{Namespace}.{ClassName}::{MethodName}. For more information, see Handler signatures. Setting image-command here pre-populates this value in Visual Studio's Publish wizard later on.
"Information": [
"This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
"To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",
"dotnet lambda help",
"All the command line options for the Lambda command can be specified in this file."
], "profile": "default", "region": "us-east-2",
Examine the Function.cs file. Function.cs defines the c# functions to expose as Lambda functions.
The FunctionHandler is the Lambda functionality that runs when the Lambda function runs. In this project, there is one function defined: FunctionHandler, which calls ToUpper() on the input text.
Your project is now ready to publish to Lambda.
Publish to Lambda
Docker images that are generated by the build process are uploaded to Amazon Elastic Container Registry (Amazon ECR). Amazon ECR is a fully-managed Docker container registry that you use to store, manage, and deploy Docker container images. Amazon ECR hosts the image, which Lambda then references to provide the programmed Lambda functionality when invoked.
To publish your function to Lambda
1. In Solution Explorer, open the context (right-click) menu for the project, and then choose Publish to AWS Lambda.
2. On the Upload Lambda Function page, do the following:
a. For Package Type, Image has been automatically selected as your Package Type because the publish wizard detected a Dockerfile within your project.
b. For Function Name, enter a display name for your Lambda instance. This name is the reference name displayed in the both the AWS Explorer in Visual Studio and the AWS Management Console.
c. For Description, enter text to display with your instance in the AWS Management Console.
d. For Image Command, enter a fully-qualified path to the method you want the Lambda function
AD Policy and then choose AWSLambdaBasicExecutionRole.
NoteYour account must have permission to run the IAM ListPolicies action, or the Role Name list will be empty.
4. Choose Upload.
The Uploading Function page displays while the function is uploading. The publish process then builds the image based on the configuration parameters, creates the Amazon ECR repository if necessary, uploads the image into the repository, and creates the Lambda referencing that repo with that image.
After the function is uploaded, the Function page opens and displays your new Lambda function’s configuration.
5. To manually invoke the Lambda function, on the Test Function tab, enter hello image based lambda into the request free-text input field and then choose Invoke. Your text, converted to uppercase, will appear in Response.
6. To view the repository, in the AWS Explorer, under Amazon Elastic Container Service, choose Repositories.
You can reopen the Function: view at any time by double-clicking on your deployed instance located in the AWS Explorer under the AWS Lambda node.
Important
If your AWS Explorer window is not open, you can dock it via View -> AWS Explorer
Next Steps
For information about creating and testing Lambda images, see Using Container Images with Lambda.
For information about container image deployment, permissions, and overriding configuration settings, see Configuring Functions.