You have multiple options for creating .NET Core Lambda function projects, such as using Visual Studio 2019, Visual Studio Code, and .NET Core CLI. In this post, you use .NET Core CLI.
By default, .NET Core CLI doesn’t support Lambda projects. You need the Amazon.Lambda.Templates nuget package to create your project.
When you create a .NET Core Lambda function project, it adds the configuration file aws-lambda-tools-defaults.json at the root of the project directory. This file holds the various configuration parameters for Lambda execution. You want to make sure that the function role is set to the IAM role you created earlier, and that the profile is set to default.
The updated aws-lambda-tools-defaults.json file should look like the following code:
{
"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-1",
"configuration": "Release",
"framework": "netcoreapp3.1",
"function-runtime": "dotnetcore3.1",
"function-memory-size": 256,
"function-timeout": 30,
"function-handler": "Dotnetlambda4::Dotnetlambda4.Function::FunctionHandler",
"function-role": "arn:aws:iam::awsaccountnumber:role/testlambdarole"
}
After you update your project configuration, you’re ready to create the buildspec.yml file.
As a prerequisite to configuring the CodeCommit stage, you created a Lambda function project. For the CodeBuild stage, you need to create a buildspec file.
Create a buildspec.yml file with the following definition and save it at the root of the CodeCommit directory:
version: 0.2
env:
variables:
DOTNET_ROOT: /root/.dotnet
secrets-manager:
AWS_ACCESS_KEY_ID_PARAM: CodeBuild:AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY_PARAM: CodeBuild:AWS_SECRET_ACCESS_KEY
phases:
install:
runtime-versions:
dotnet: 3.1
pre_build:
commands:
- echo Restore started on `date`
- export PATH="$PATH:/root/.dotnet/tools"
- pip install --upgrade awscli
- aws configure set profile $Profile
- aws configure set region $Region
- aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID_PARAM
- aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY_PARAM
- cd Dotnetlambda4
- cd src
- cd Dotnetlambda4
- dotnet clean
- dotnet restore
build:
commands:
- echo Build started on `date`
- dotnet new -i Amazon.Lambda.Templates::*
- dotnet tool install -g Amazon.Lambda.Tools
- dotnet tool update -g Amazon.Lambda.Tools
- dotnet lambda deploy-function "Dotnetlambda4" --function-role "arn:aws:iam::yourawsaccount:role/youriamroleforlambda" --region "us-east-1"
You’re now ready to commit your changes to the CodeCommit repository.
To push changes to your CodeCommit repository, enter the following git commands.
git add --all
git commit –a –m “Initial Comment”
git push
After you commit the changes, you can create your CI/CD pipeline using CodePipeline.
To create your pipeline with a CodeCommit and CodeBuild stage, complete the following steps:
After you save your pipeline, push the code changes of the Lambda function from the local repository to the remote CodeCommit repository.
After a few seconds, you should see the activation of the CodeCommit stage and transition to CodeBuild stage. Pipeline creation can take up to a few minutes.
You can verity your pipeline on the CodePipeline console. This should deploy the Lambda function changes to the Lambda environment.