Amazon.Lambda.Annotations 0.5.1-preview

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
This is a prerelease version of Amazon.Lambda.Annotations.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Amazon.Lambda.Annotations --version 0.5.1-preview
NuGet\Install-Package Amazon.Lambda.Annotations -Version 0.5.1-preview
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Amazon.Lambda.Annotations" Version="0.5.1-preview" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Amazon.Lambda.Annotations --version 0.5.1-preview
#r "nuget: Amazon.Lambda.Annotations, 0.5.1-preview"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Amazon.Lambda.Annotations as a Cake Addin
#addin nuget:?package=Amazon.Lambda.Annotations&version=0.5.1-preview&prerelease

// Install Amazon.Lambda.Annotations as a Cake Tool
#tool nuget:?package=Amazon.Lambda.Annotations&version=0.5.1-preview&prerelease

Amazon.Lambda.Annotations

The Lambda Annotations is a programming model for writing .NET Lambda function. At a high level the programming model allows idiomatic .NET coding patterns and uses C# Source Generators to bridge the gap between the Lambda programming model to the more idiomatic programming model.

For example here is a simplistic example of a .NET Lambda function that acts like a calculator plus method using the normal Lambda programming model. It respondes to an API Gateway REST API, pulls the operands from the resource paths, does the addition and returns back an API Gateway response.

public class Functions
{
    public APIGatewayProxyResponse LambdaMathPlus(APIGatewayProxyRequest request, ILambdaContext context)
    {
        if (!request.PathParameters.TryGetValue("x", out var xs))
        {
            return new APIGatewayProxyResponse
            {
                StatusCode = (int)HttpStatusCode.BadRequest
            };
        }
        if (!request.PathParameters.TryGetValue("y", out var ys))
        {
            return new APIGatewayProxyResponse
            {
                StatusCode = (int)HttpStatusCode.BadRequest
            };
        }

        var x = int.Parse(xs);
        var y = int.Parse(ys);

        return new APIGatewayProxyResponse
        {
            StatusCode = (int)HttpStatusCode.OK,
            Body = (x + y).ToString(),
            Headers = new Dictionary<string, string> { { "Content-Type", "text/plain" } }
        };
    } 
}

Using Amazon.Lambda.Annotations the same Lambda function can be written like this.

public class Functions
{
    [LambdaFunction]
    [RestApi("/plus/{x}/{y}")]
    public int Plus(int x, int y)
    {
        return x + y;
    }
}

Source Generator

To bridge the gap from Lambda Annotations programming model to the normal programming model a .NET source generator is included in this package. After adding the attributes to your .NET code the source generator will generate the translation code between the 2 programming models. It will also keep in sync the generated information including a new function handler string into the CloudFormation template. The usage of source generator is transparent to the user.

To see the code that is generated by the source generator turn the verbosity to detailed when executing a build. From the command this is done by using the --verbosity switch.

dotnet build --verbosity detailed

To change the verbosity in Visual Studio go to Tools → Options → Projects and Solutions and adjust the MSBuild verbosity drop down boxes.

Dependency Injection

Lambda Annotations supports using dependency injection. A class can be marked with a LambdaStartup attribute. The class will have a ConfigureServices method for configuring services.

The services can be injected by either constructor injection or using the FromServices attribute on a method parameter of the function decorated with the LambdaFunction attribute.

Services injected via the constructor have a lifecycle for the length of the Lambda compute container. For each Lambda invocation a scope is created and the services injected using the FromServices attribute are created within the scope.

Example startup class:

[LambdaStartup]
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAWSService<Amazon.S3.IAmazonS3>();
        services.AddScoped<ITracker, DefaultTracker>();
    }
}

Example function using DI:

public class Functions
{
    IAmazonS3 _s3Client;

    public Functions(IAmazonS3 s3Client)
    {
        _s3Client = s3Client;
    }


    [LambdaFunction]
    [HttpApi(HttpMethod.Put, HttpApiVersion.V2, "/process/{name}")]
    public async Task Process([FromServices] ITracker tracker, string name, [FromBody] string data)
    {
        tracker.Record();

        await _s3Client.PutObjectAsync(new PutObjectRequest
        {
            BucketName = "storage-bucket",
            Key = name,
            ContentBody = data
        });
    }
}

Lambda .NET Attributes

List of .NET attributes currently supported.

  • LambdaFunction
    • Placed on a method. Indicates this method should be exposed as a Lambda function.
  • LambdaStartup
    • Placed on a class. Indicates this type 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.

Event Attributes

Event attributes configuring the source generator for the type of event to expect and setup the event source in the CloudFormation temlate. If an event attribute is not set the parameter to the LambdaFunction must be the event object and the event source must be configured outside of the code.

  • RestApi
    • Configures the Lambda function to be called from an API Gateway REST API. The HTTP method and resource path are required to be set on the attribute.
  • HttpApi
    • Configures the Lambda function to be called from an API Gateway HTTP API. The HTTP method, HTTP API payload version and resource path are required to be set on the attribute.

Parameter Attributes

  • FromHeader
    • Map method parameter to HTTP header value
  • FromQuery
    • Map method parameter to query string parameter
  • FromRoute
    • Map method parameter to resource path segment
  • FromBody
    • Map method parameter to HTTP request body. If parameter is a complex type then request body will be assumed to be JSON and deserialized into the type.
  • FromServices
    • Map method parameter to registered service in IServiceProvider
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (4)

Showing the top 4 popular GitHub repositories that depend on Amazon.Lambda.Annotations:

Repository Stars
aws/aws-lambda-dotnet
Libraries, samples and tools to help .NET Core developers develop AWS Lambda functions.
aws-samples/serverless-test-samples
This repository is designed to provide guidance for implementing comprehensive test suites for serverless applications.
Particular/docs.particular.net
All content for ParticularDocs
hlaueriksson/CommandQuery
Command Query Separation for 🌐ASP.NET Core ⚡AWS Lambda ⚡Azure Functions ⚡Google Cloud Functions
Version Downloads Last updated
1.3.0 3,066 4/5/2024
1.2.0 42,594 2/16/2024
1.1.0 72,412 11/16/2023
1.0.0 132,437 7/14/2023
0.13.5 8,190 6/23/2023
0.13.4 1,726 6/19/2023
0.13.3 16,255 5/8/2023
0.13.2 9,822 4/24/2023
0.13.1 21,040 4/7/2023
0.13.0 28,580 3/1/2023
0.12.0 37,815 2/14/2023
0.11.0 1,964 2/8/2023
0.10.0-preview 13,331 12/8/2022
0.9.0-preview 3,311 10/26/2022
0.8.0-preview 2,470 9/13/2022
0.7.0-preview 232 8/29/2022
0.6.0-preview 1,405 8/2/2022
0.5.1-preview 2,130 5/3/2022
0.5.0-preview 14,589 2/17/2022
0.4.3-preview 893 1/22/2022
0.4.2-preview 261 12/22/2021