In this article seriese I am going to show how to create an AWS Lambda Function using .NET Core and Amazon project template and also how to deploy the Lambda Function to AWS using Terraform.
This article seriese will be of 2 parts.
- Part 1: Create, develop and build AWS Lambda Function using .NET Core
- Create a new Lambda Function using .NET Core and Amazon project template
- Build the project using MsBuild script
- Create deployment package to be deployed on AWS
- Part 2: Deploy Lambda function to AWS using Terraform
- Create Terraform script to deploy Lambda Function on AWS
- Automate Terraform script running via MsBuild
- Preform incremental deployment of Lambda Function
Following tools will be used during the article, so make sure you have them install on your machine.
- Install .NET Core (Download)
- Install AWS Project templates for .NET (Instructions)
- Install AWS CLI (User Guide)
- Install Terraform (Download)
- Install Visual Studio Code (Optional) (Download)
You can use any platform (Windows, Mac, Linux). For this article, I am going to demonstrate everything on Mac.
Following are the activities I am going to perform as during this part of the article.
- Use .NET Core CommandLine (.NET CLI)
- To create AWS Lambda project to listen SQS Queue
- Build the project
- Create and use MsBuild script to
- Build the project
- Run Unit Tests
- Create deployment package
First thing we will be doing is to create an AWS Lambda project running on .NET Core framework. The Lambda function which will be created as part of this project will be triggered when a specific SQS queue detects and event.
You can find more information about the event sources supported by Lambda function here.
Follow below listed steps in order to create a project with AWS Lambda with SQS trigger.
- Open terminal and navigate to the directory where you want to create your solution directory.
Create a directory for the solution and move to the directory.
Create new empty solution
dotnet new sln
Create new AWS Lambda Project, which will be triggered by an SQS event
dotnet new lambda.SQS
This would create two directories,
lambda.SQS is the name of the project template which creates the Lambda project for SQS triggered Lambda Function.
src directory has the project while “test” directory has the Test project to unit test the actual project.
Add these projects to the solution
dotnet sln add .\src\lambdawithsqs\lambdawithsqs.csproj
dotnet sln add .\test\lambdawithsqs.Tests\lambdawithsqs.Tests.csproj
Build the solution
With this we are done with creating and build an AWS Lambda Project with .NET Core.
Next is to create MsBuild script to build the project, run the unit tests and create deployment pakcage.
Now onwards I will use Visual Studio Code to add new/modify existing the files in the solution. If you are not using Visual Studio Code, feel free to open the files using your favorite text editor.
In any case, do not forget to save the files after you make changes to them.
Open Visual Studio Code and open the solution directory from it. This should show all the files/directories availalbe under the solution directory inside the file explorer located at the left side of the Visual Studio Code.
As you notice,
Function.cs is the only code file generated as part of the project. I will not explain the code written in this file. Also, we are not going to change any code of the project as part of this article. The auto-generated code in the project is eoungh right now to move ahead.
Now, let’s create a new directory
Build under the solution directory and create a new file
You can do these either directly in the Visaul Studio Code or by running command from the terminal.
lambdawithsqs.proj and enter following content in the file and save the file.
It won’t be difficult to understand these contents if you are familier with the MSBuild script.
Following is the short description about it.
- The first section
PropertyGroupis the section where the properties of the current MSBuild script are defined and values assigned to them.
- Each of the
<Target>about performing some activity. For example
MSbuildcommand to build the solution.
dotnet vstestcommand to run the unit tests.
dotnet lambda packagecommand to create build package.
- The last three
Targets are used for running one or more above defined targets. Notice
DependsOnTargetsattributes. This enables us to orcastrate the execution of the script as required.
dotnet lambda package command is worth a notice here. This command is specific to lambda functions and it creates a deployment package with its all required dependencies. And this package can be directly deployed to Lambad function on AWS.
Let’s test this script. (Do not forget to save the file.)
Run following command, on terminal, to build the solution and run the unit tests.
dotnet msbuild ./Build/lambdawithsqs.proj /t:BuildAndTest
/t:BuildAndTest is the parameter in above command tells which target to run from the
BuildAndTest target depends on other two targets,
So eventually the above command will build the solution first and run the unit tests after that.
Now lets create the build package of the Lambda function. For this, run following command on the terminal.
dotnet msbuild ./Build/lambdawithsqs.proj /t:BuildTestPackage
This command would create a new directory
Output under solution directory and there is a file
lambdawithsqs.zip create under
The zip file is the package which can be deployed to AWS Lambda as it is by just uploading it. But we are going to save this manual effort by automating the deployment. We will learn about that in the next part of the article.
See you soon in the next part!!!! Until then Happy Coding :)
Some more changes made to the blog.