In previous post, we saw how to setup local development environment and test a Lambda function locally.
As a developer, you may want to test the function in AWS environment quickly to make sure your code is working. In this post we will look at how to deploy the function in your AWS account. We will look at CI/CD pipeline to automatically build and deploy Lambda function in next post.
If you followed the instruction in previous post, you will notice there are three files in your solution
- LambdaEntrypoint.cs - This is the entrypoint for AWS Lambda.
- aws-lambda-tools-defaults.json - This is the configuration file used by Lambda tooling. Update this file to add AWS profile you created while configuring AWS CLI and region you want to deploy your code. Note make sure you have this setting in your template as for some reason, it is missing from default template.
"function-runtime": "netcoreapp3.1",
- serverless.template - This is CloudFormation Serverless application model template used to create Lambda function, API Gateway endpoint in AWS
Here is example of my aws-lambda-tools-defaults.json
{
"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": "eu-west-1",
"configuration": "Release",
"framework": "netcoreapp3.1",
"function-runtime": "netcoreapp3.1",
"s3-prefix": "SampleApp/",
"template": "serverless.template",
"template-parameters": ""
}
To deploy the function in your AWS account, run following command. This will build, package your application as zip, upload to S3 bucket and use CloudFormation to create API Gateway and Lambda function
dotnet lambda deploy-serverless
After executing above command, it will prompt you for
1. CloudFormation Stack Name
2. S3 Bucket name where code will be uploaded. Create this bucket before running deploy command.
If your code deploys successfully, you should get the API Gateway URL. Something like this to test you code.
https://xxxxxxx.execute-api.eu-west-1.amazonaws.com/Prod/
Congratulations!!! you have successfully build and deployed your first .Net API as Lambda application fronted by API Gateway.
In next post we will look at automating build and deployment pipeline.
Comments