The .NET 6 runtime for AWS Lambda gave .NET developers a few more options when developing AWS Lambda functions. One such option is top-level statements. The top-level statements feature was designed to greatly simplify developing applications in C# by removing a lot of the “boilerplate” code. For instance, in this tutorial we will utilize a console application to develop an AWS Lambda function and in this console application you will notice that our Program.cs file does not have a Main function or even a namespace statement. Checkout Microsoft’s tutorial on top-level statements to learn more.
In this tutorial, we’ll take a look at using top-level statements as a way to build AWS Lambda functions. We’ll start out by generating a .NET console application and then we’ll bring you through the process of turning that console app into a deployable AWS Lambda function.
Remember, for any example solution from AWS with .NET, we focus on the code that exemplifies the problem we are trying to solve. We don’t include logging, input validation, exception handling, etc., and we embed the configuration data within classes instead of using environment variables, configuration files, key/value stores and the like. These items should not be skipped for proper solutions.
Warning: some AWS services may have fees associated with them.
Our Dev Environment
This tutorial was developed using Ubuntu 20.04, AWS CLI v2, .NET 6 SDK and Visual Studio Code 1.66.2. Some commands/constructs may very across systems.
Developing the AWS Lambda Function
Create the Console Application Project
Our first task is to create the console application with the following command:
$ dotnet new console -n TopLevelLambda
With the console application in place, let’s add a few dependencies using the dotnet CLI.
$ dotnet add package Amazon.Lambda.APIGatewayEvents
$ dotnet add package Amazon.Lambda.RuntimeSupport
$ dotnet add package Amazon.Lambda.Serialization.Json
Modify the Program Class
Delete everything in the Program.cs file and replace with the following code:
|var handler = (APIGatewayProxyRequest apiGatewayProxyRequest) =>|
|return apiGatewayProxyRequest?.QueryStringParameters?.Values ?? new string;|
|LambdaBootstrapBuilder lambdaBootstrapBuilder = LambdaBootstrapBuilder.Create(handler, new JsonSerializer());|
|LambdaBootstrap lambdaBootstrap = lambdaBootstrapBuilder.Build();|
Let’s talk about the code a bit. First, we build our handler function. The handler function is what gets invoked when the Lambda function runs. This handler function accepts one parameter with a type of, APIGatewayProxyRequest. The logic of the handler is simple. Essentially, the handler function will return any values that are present in the querystring of an HTTP request. As you can probably surmise, the trigger for this AWS Lambda function would be a request coming in from AWS API Gateway or the URL for the Lambda.
The last three lines of code builds and starts the Lambda runtime client, passing in the handler function and a newly instantiated JsonSerializer object. The JsonSerializer object’s responsibility is to translate the JSON documents received from AWS Lambda into .NET objects.
That’s it, it’s time to deploy the top-level statement based AWS Lambda function.
Packaging and Uploading the Lambda Function
Checkout this article on packaging and uploading an AWS Lambda function that takes you through, step-by-step, on how to create a Lambda function, create an HTTPS endpoint for the Lambda Function and then packaging and uploading the Lambda Function.
Important: specify the handler by using only the assembly name when deploying AWS Lambda functions that are based on top-level statements. For the example in this tutorial, the assembly name and the name of the handler would both be, “TopLevelLambda”.
We have concluded this tutorial where you have learned how to build a .NET AWS Lambda function using top-level statements.
Now that you have a working solution, let’s take things a little further…
- Test the handler by providing different URL parameter values.
- Test the handler by removing all URL parameter values.
- Modify the handler to surface the keys of the querystring parameters.
Want to know more about the tech in this article? Checkout these resources: