AWS Lambda with SQS trigger using .NET Core

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.

  1. Install .NET Core (Download)
  2. Install AWS Project templates for .NET (Instructions)
  3. Install AWS CLI (User Guide)
  4. Install Terraform (Download)
  5. 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.

Part 1: Create, develop and build AWS Lambda Function using .NET Core

Following are the activities I am going to perform as during this part of the article.

  1. Use .NET Core CommandLine (.NET CLI)
    • To create AWS Lambda project to listen SQS Queue
    • Build the project
  2. 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.

  1. Open terminal and navigate to the directory where you want to create your solution directory.
  2. Create a directory for the solution and move to the directory.

    1
    2
    mkdir lambdawithsqs
    cd lambdawithsqs
  3. Create new empty solution

    1
    dotnet new sln
  4. Create new AWS Lambda Project, which will be triggered by an SQS event

    1
    dotnet new lambda.SQS

This would create two directories, src and test. lambda.SQS is the name of the project template which creates the Lambda project for SQS triggered Lambda Function.
The src directory has the project while “test” directory has the Test project to unit test the actual project.

  1. Add these projects to the solution

    1
    2
    dotnet sln add .\src\lambdawithsqs\lambdawithsqs.csproj
    dotnet sln add .\test\lambdawithsqs.Tests\lambdawithsqs.Tests.csproj
  2. Build the solution

    1
    dotnet build

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.

Lambda Project Structure

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 lambdawithsqs.proj.

You can do these either directly in the Visaul Studio Code or by running command from the terminal.

1
2
mkdir Build
touch ./Build/lambdawithsqs.proj

Open lambdawithsqs.proj and enter following content in the file and save the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<Project>
<PropertyGroup>
<RootDirectory>$(MSBuildProjectDirectory)\..</RootDirectory>
<OutputDirectory>..\..\Output\</OutputDirectory>
<ProjectDirectory>..\src\lambdawithsqs</ProjectDirectory>
<TestAssemblyFile>
$(RootDirectory)\test\lambdawithsqs.Tests\bin\Release\netcoreapp2.1\lambdawithsqs.Tests.dll
</TestAssemblyFile>
<SolutionName>lambdawithsqs.sln</SolutionName>
<PackageName>lambdawithsqs.zip</PackageName>
</PropertyGroup>
<Target Name="Compile">
<Exec WorkingDirectory="$(RootDirectory)" Command="dotnet restore">
<Output TaskParameter="ExitCode" ItemName="exitCode" />
</Exec>
<MSBuild Projects="$(RootDirectory)\$(SolutionName)" Targets="Rebuild" Properties="Configuration=Release">
<Output TaskParameter="TargetOutputs" ItemName="BuildAssemblies" />
</MSBuild>
</Target>
<Target Name="UnitTests">
<Message Text="Running Unit Tests" />
<Exec Command="dotnet vstest @(TestAssemblies)">
<Output TaskParameter="ExitCode" ItemName="exitCode" />
</Exec>
</Target>
<Target Name="Package">
<Message Text="Creating build package from $(ProjectDirectory)" />
<Exec WorkingDirectory="$(ProjectDirectory)" Command="dotnet lambda package -o $(OutputDirectory)$(PackageName)">
<Output TaskParameter="ExitCode" ItemName="exitCode" />
</Exec>
</Target>
<ItemGroup>
<TestAssemblies Include="$(TestAssemblyFile)">
<UnitTest>True</UnitTest>
</TestAssemblies>
</ItemGroup>
<Target Name="Build" DependsOnTargets="Compile" />
<Target Name="BuildAndTest" DependsOnTargets="Compile;UnitTests" />
<Target Name="BuildTestPackage" DependsOnTargets="Compile;UnitTests;Package" />
</Project>

It won’t be difficult to understand these contents if you are familier with the MSBuild script.

Following is the short description about it.

  1. The first section PropertyGroup is the section where the properties of the current MSBuild script are defined and values assigned to them.
  2. Each of the <Target> about performing some activity. For example
    1. Compile target runs MSbuild command to build the solution.
    2. UnitTests target runs dotnet vstest command to run the unit tests.
    3. Package target runs dotnet lambda package command to create build package.
  3. The last three Targets are used for running one or more above defined targets. Notice DependsOnTargets attributes. 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.

1
dotnet msbuild ./Build/lambdawithsqs.proj /t:BuildAndTest

/t:BuildAndTest is the parameter in above command tells which target to run from the lambdawithsqs.proj file. BuildAndTest target depends on other two targets, Compile and UnitTests.
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.

1
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 Output directory.

Output Directory With Package

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.