To deploy a .NET Core function to AWS Lambda, you can use the Visual Studio AWS Toolkit. I find the AWS Toolkit to be...annoying (or worse). It requires permissions unrelated to updating a function, it slows down Visual Studio, and it sometimes just crashes. Below is a simple PowerShell script that will build and update an AWS Lambda Function. The code makes the following assumptions.
- You have AWS Tools for PowerShell installed.
- The AWS Lambda Function is already created. You are only updating it.
- You have an AWS IAM User with the lambda:UpdateFunctionCode permission granted on the function in question.
- The AWS IAM User has its credentials stored in a local profile.
$ErrorActionPreference = "Stop"
$lambda_function_name = "my-function-name"
$project_folder = "C:\Projects\MyFunctionProjectFolder\"
$zip_file = "$project_folder\output.zip"
# this assumes you have a profile in the C:\Users\myusername\.aws\credentials file
$aws_profile = "aws_profile_name"
$aws_region = "us-east-1"
#navigate to the project folder
cd $project_folder
#build react - optional, maybe you are deploying a .NET Core Razor Pages site with React, you will build react first
# npm install
# npm run build
#build
dotnet lambda package -o output.zip
#deploy
Update-LMFunctionCode -FunctionName $lambda_function_name -ZipFilename $zip_file -Region $aws_region -ProfileName $aws_profile
#delete the zipped build file
Remove-Item -Path $zip_file -Force
Example AWS Permissions Needed
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "lambda:UpdateFunctionCode",
"Resource": "arn:aws:lambda:us-east-1:123123123123:function:my-function-name"
}
]
}