Manually deploying a .NET AWS Lambda function
I couldn't find a complete guide for deploying an AWS Lambda function in .NET from a local machine. I'm publishing my notes on the subject for future reference and anyone else interested.
Since AWS Lambda is a serverless service, quick startup is of the essence. Therefore, it makes sense to use ReadyToRun compilation. In .NET Core 3.1, there was no cross-compilation support for it, so the deployment had to be done from Amazon Linux 2. In .NET 6, cross compilation for Linux is supported on all platforms, so there is no need for building in Amazon Linux 2 anymore.
I wanted to write a deployment script, so the Amazon.Lambda.Tools global tool seemed the best choice. I decided to install it as a local tool so that the exact version in use could be committed to source control. I did the initial setup with the following commands:
dotnet new tool-manifest
dotnet tool install Amazon.Lambda.Tools
This created a dotnet-tools.json
file in the .config
subfolder with the following contents, which I added to source control:
{
"version": 1,
"isRoot": true,
"tools": {
"amazon.lambda.tools": {
"version": "5.7.2",
"commands": ["dotnet-lambda"]
}
}
}
With this file in place, the same version of the tool can easily be installed on a different machine by calling:
dotnet tool restore
For the global tool to work, Amazon credentials must first be set in the ~/.aws/credentials
file. The easiest way to do this is with the AWS CLI. I installed it using Chocolatey:
choco install awscli
I could then call aws configure
to set the AWS Access Key ID and AWS Secret Access Key values, but left the other values unset. The AWS access key can be created in AWS Management Console.
I was now finally ready to deploy the function:
dotnet lambda deploy-function MyLambdaFunction --msbuild-parameters "/p:PublishReadyToRun=true --self-contained false" --function-runtime "dotnet6" --function-handler "FullNamespace::FullNamespace.FunctionClassName::MethodName" --region us-east-1
As the last step, I created a PowerShell script that restores the local tool and installs the Lambda function to three regions:
dotnet tool restore
dotnet lambda deploy-function MyLambdaFunction --msbuild-parameters "/p:PublishReadyToRun=true --self-contained false" --function-runtime "dotnet6" --function-handler "FullNamespace::FullNamespace.FunctionClassName::MethodName" --region us-east-1
dotnet lambda deploy-function MyLambdaFunction --msbuild-parameters "/p:PublishReadyToRun=true --self-contained false" --function-runtime "dotnet6" --function-handler "FullNamespace::FullNamespace.FunctionClassName::MethodName" --region us-west-2
dotnet lambda deploy-function MyLambdaFunction --msbuild-parameters "/p:PublishReadyToRun=true --self-contained false" --function-runtime "dotnet6" --function-handler "FullNamespace::FullNamespace.FunctionClassName::MethodName" --region eu-west-1
In the long run, it makes sense to set up deployment of a Lambda function in a CI/CD service like GitHub Actions. However, it can be beneficial to be able to deploy it from the local machine as well, especially during the early testing phase. In this post, I described the process I followed to achieve that.