Build, Zip, Update a .NET Core 3.1 AWS Lambda, and Run a Test, with a Single Command
Want to learn more about AWS Lambda and .NET? Check out my A Cloud Guru course on ASP.NET Web API and Lambda.
I’ve been working on a Lambda that has required a lot of trial and error, running unit tests locally was not good enough because my problem was around how serialization worked up AWS. I am working on a few blogs about this.
I was repeatedly building the code in VS Code, using Total Commander to zip the files, then moving to the AWS UI to upload the zip, and finally hit test.
Since first writing this post I found a simpler way build, zip and send the zip to AWS - you have to have the the dotnet lambda tools installed.
Option 1
Use the dotnet lambda tool to deploy the Lambda, my function is named “ApiCaller” -
dotnet lambda deploy-function -fn ApiCaller
Run it by calling -
dotnet lambda invoke-function ApiCaller --payload '"{ \"Uri\": \"http://httpbin.org/ip\" }"'
Option 2
Here is a simple batch file that does builds, zips, uploads and runs the Lambda, save it to a file called UploadAndTest.bat
-
dotnet build
rm .\bin\Debug\netcoreapp3.1\%1.zip
7za.exe a .\bin\Debug\netcoreapp3.1\%1.zip .\bin\Debug\netcoreapp3.1\*
aws lambda update-function-code --function-name %1 --zip-file fileb://bin\Debug\netcoreapp3.1\%1.zip
aws lambda invoke --function-name ApiCaller --cli-binary-format raw-in-base64-out --payload %2 response.json
I run it by calling -
UploadAndTest.bat ApiCaller '"{ \"Uri\": \"http://httpbin.org/ip\" }"'
If you don’t have 7Zip installed see here - Zipping Files From the Command Line in Windows 10