AWS Lambda .NET Class Library
Explore the structure of AWS Lambda .NET class library projects and understand how to configure them for serverless applications. Learn about essential project components, custom AWS Lambda settings, and key NuGet packages for handling JSON serialization and AWS events. This lesson helps you build foundational skills to develop and deploy AWS Lambda functions using .NET.
We'll cover the following...
In this lesson, we’ll take a detailed look at the .NET SDK for AWS Lambda and see how the most basic AWS Lambda application project is structured. The example we’ll use is in the following playground.
{
"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": "",
"region": "",
"configuration": "Release",
"function-architecture": "x86_64",
"function-runtime": "dotnet6",
"function-memory-size": 256,
"function-timeout": 30,
"function-handler": "LambdaApp::LambdaApp.Function::FunctionHandler"
}The project in this playground represents a simple AWS Lambda function that accepts a request from ELB and returns an HTML response. We don’t need to know what ELB is. With Lambda Test Tool, we don’t even need it to invoke the function. We chose this project type for two reasons:
It’s an AWS Lambda project template.
It’s the simplest project template that allows us to use more complex object input rather than very basic string input.
If we want to execute this function, we can click the “Run” button in the playground. Once the application is loaded, we can select “Application Load Balancer” as the example request to populate the “Function Input” field with the appropriate data. Then we can click the “Execute Function” button to see the response.
Let’s go through all the components of a .NET project that are essential for an AWS Lambda function.
Essential components of an AWS Lambda project
We can open the LambdaApp.csproj file and see what configuration is required to make a .NET project work as an AWS Lambda function. There are multiple ways to build a .NET project into an AWS Lambda, but the simplest way is to use a class library that AWS runtime can execute. This means that the project cannot be executed directly as a stand-alone application. It needs an AWS environment, either real or emulated.
Here are two ways we can tell the project is a class library:
The value of the
Sdkattribute of theProjecttag on line 1 isMicrosoft.NET.Sdk, which is shared between basic application types, such as a console application and a class library.There is no
OutputTypetag in the file, which indicates that the project is not executable directly.
The following illustration demonstrates how this type of AWS Lambda application responds to an event.
As we can see, the standard AWS Lambda application is represented by a class library. That means it cannot be executed on its own and it needs a runtime to trigger its function handler. Next, we’ll add some custom XML elements that are only relevant to AWS Lambda.
Adding elements specific to AWS Lambda
Here are the elements that are essential to an AWS Lambda project:
AWSProjectType: We have this element on line 7. For an AWS Lambda application, the value needs to beLambda.CopyLocalLockFileAssemblies: This element can be seen on line 8. It’s not strictly required, but if it’s present and set totrue, the project and its dependencies will be copied to the build output folder, which will help various tools, such as Mock Lambda Test Tool, find all the required files.PublishReadyToRun: This element is on line 9. If present and set totrue, it will generate a run image during the build so the application can start faster.
As well as having custom elements in the project file, we need to install the appropriate NuGet packages. We will learn about these next.
Essential NuGet packages
Amazon.Lambda.Core is a NuGet package that is essential for any AWS Lambda .NET project. We can find it on line 12 of the LambdaApp.csproj file. This package may also be referenced implicitly. If it happens to be a dependency of another NuGet package, its library will be installed if we install the other NuGet package, even if we don’t reference Amazon.Lambda.Core directly.
The other packages cover the functionality needed in specific scenarios. The example includes the following:
The
Amazon.Lambda.Serialization.SystemTextJsonpackage listed on line 13 is needed for implementing JSON serializers. It’s not strictly required, but it makes our job as developers much easier by allowing us to write function handlers with any JSON-serializable data types.The
Amazon.Lambda.ApplicationLoadBalancerEventspackage listed on line 14 contains classes that make it easier to interact with ELB. Again, it’s not strictly required, but without it, we would have to come up with our own data types that represent ELB events.
These are the most fundamental components of an AWS Lambda .NET project. However, this is not the only .NET project configuration supported by AWS Lambda, and additional dependencies are required in other project types.