Test .NET Core AWS Lambda

The following post shows some techniques about test .NET Core AWS Lambda, more in specific, it focuses on testing .NET Core AWS Lambda using the LambdaTestTool. I’ve already spoken about serverless, .NET Core, and AWS Lambda in the following article: Fast growing architectures with serverless and .NET Core. This post will be focused more on the testing side.

The testing issues in a serverless computing

As said in the previous post about serverless it is hard to test serverless applications. Especially when their complexity grows, it is hard to detect issues before running them on the cloud. Furthermore, from a development perspective, it is hard to deal with the problems if every time we need to re-deploy our services to verify the result.

Existing testing tools

Some existing testing tools help us to deal with the serverless systems. Speaking about AWS, things like https://github.com/localstack/localstack provides a way to emulate the AWS stack on your machine by installing it or by running a docker image. The point of these frameworks is that they consume a lot in terms of resources and they are not always suitable for integration testing.

Testing lambda using LambdaTestTool

The LambdaTestTool is a utility produced by the AWS .NET Team which provides a useful and light way approach to the Lambda testing. Furthermore, it provides a way to debugging your lambda locally by triggering some input events and attaching the debugger of your preferred IDE or code editor.

Recently, the AWS .NET Team also released a new version of the LambdaTestTool with the following PR:

https://github.com/aws/aws-lambda-dotnet/pull/364

which enables the debugging also for all the lambdas that use the serverless framework.

Installation

The LambdaTestTool is implemented as part of the dotnet tool suite (I’ve already written about dotnet tools in the following post: Artless HTTP server using .NET Core), it is cross-platform, and can be installed by using the following command:

dotnet tool install -g Amazon.Lambda.TestTool-2.1

Moreover, it is possible to update the tool at the latest version using the following instruction:

dotnet tool update -g Amazon.Lambda.TestTool-2.1

The above-mentioned code installs the new dotnet tool on your local machine. Therefore, it is possible to use it to run the lambda locally by configuring your IDE or code editor properly.

Debug the lambda

The LambdaTestTool runs an ASP.NET instance which returns the following interface:

The UI provides a way to select the config file of the lambda, the function in the project to execute, the credentials, the AWS region and the message to send as the input of the function.

It is possible to configure the LambdaTestTool on multiple editors, such as Visual Studio, Visual Studio Code, Rider and Visual Studio for Mac. The next section shows the configurations for Visual Studio Code and Rider.

Visual studio code

The same approach can be taken with a Visual Studio Code, by adding the following section in the launch.json file:

Rider

For example, in the case of Rider IDE, it is possible to use the tool by creating a new configuration in the IDE:

The Exe path field refers to the path to the dotnet tool:

<home-directory>/.dotnet/tools/.store/amazon.lambda.testtool-2.1/<nuget-version>/amazon.lambda.testtool-2.1/<nuget-version>/tools/netcoreapp2.1/any/Amazon.Lambda.TestTool.dll

The working directory field is the root folder of the lambda project.

Summary

It is possible to get more information about the LambdaTestTool on Github: https://github.com/aws/aws-lambda-dotnet/tree/master/Tools/LambdaTestTool. The tool is still in a preview phase, but it is a very useful and fast way when we want to verify and testing a .NET Core AWS Lambda. What’s still missing, is a quick way to integrate the package and run it directly in the code, for example, as a part of a unit testing process.

Cover image by The Cutty Sark – © National Maritime Museum