DEV Community

Cover image for Using Amazon .NET Lambda Annotations (Preview)
Anita Andonoska for AWS Community Builders

Posted on • Updated on • Originally published at Medium

Using Amazon .NET Lambda Annotations (Preview)

In this post, we’ll explore the new .NET Lambda Annotations framework.

Lambda Annotations is a new framework introduced with the release of .NET 6 managed runtime for Lambda (currently in preview). Lambda Annotations framework simplifies writing .NET Lambda functions by reducing boilerplate code and synchronizing the Lambda functions implemented in your code with your project’s CloudFormation template.

How it works

The Annotations framework makes the experience of writing Lambda feel more natural in C#. The current release is mainly focused on simplifying Lambda functions that are API Gateway invoked, also there are other improvements in regards to Dependency Injection, that can be used with other Lambda invocation types.

Here is an example, with the “LambdaFunction” attribute placed on a method that indicates the method should be exposed as a Lambda function, with the specified MemorySize. You can set other attributes too, like Timeout, Role, Policies, etc. The “RestApi” attribute configures the Lambda function to be called from an API Gateway REST API.

When the project is compiled, the Annotations framework based on the .NET Lambda attributes generates new code into the build that under the hood handles all of the translation, to the Lambda programming model. Also, it will synchronize all of the C# methods with the “LambdaFunction” attribute in the project’s CloudFormation template. The CloudFormation template can still be edited to add additional AWS resources or to further customize the Lambda function. Configuring CloudFormation properties for the functions using the .NET attributes is optional. If you prefer you can continue to configure the additional settings in the CloudFormation template.

Another attribute, that I am especially excited about is the “LambdaStartup” attribute. Placed on a class, indicates the class should be used as the startup class and is used to configure the dependency injection and middleware. There can only be one class in a Lambda project with this attribute. The “LambdaStartup” attribute really simplifies the configuration of dependency injection.

Here’s an example of how can be used:


Besides registering custom services, you can register Amazon services as well by using the “AddAWSService” extension method (for this, you need to install the “AWSSDK.Extensions.NETCore.Setup” NuGet package).

The code еxamples can be found in this GitHub repo.

Please note that Lambda Annotations require the deployment of the Lambda function to be done using a CloudFormation template. In the future, other deployment technologies may be supported.

How to get started

To get started with Lambda Annotations, there is a blueprint as part of the Visual Studio AWS Toolkit for Visual Studio 2022. The steps in detail can be found here.

Besides that, you can also use Lambda Annotations within your existing .NET 6 project. To get started you need Amazon.Lambda.Annotations NuGet package. Then you can start using the Lambda Annotation attributes.

Top comments (0)