AWS Introduces a Framework for Writing .NET 6 Lambda Functions — Visual Studio Magazine

New

AWS Introduces a Framework for Writing .NET 6 Lambda Functions

Amazon Web Services previews a new framework for using its recently introduced .NET 6 runtime to build AWS Lambda functions, the foundation of serverless computing in the Amazon cloud.

The .NET Annotations Lambda Framework (Preview), announced earlier this month, comes shortly after AWS introduced the .NET 6 managed runtime for AWS Lambda. The runtime enables .NET-centric cloud coders to use C# and leverage the AWS Cloud to deliver their serverless computing projects with the latest edition of Microsoft’s open-source development platform, which debuted in last November. The new preview has been in the works since around that time.

The idea of ​​the new Annotations framework is to provide a high-level library to simplify writing .NET Lambda functions in a more idiomatic way – natural to C# and .NET 6 – along with other benefits such as synchronization of Lambda functions with the CloudFormation template (an infrastructure automation platform) associated with a specific project.

The Lambda Blueprint Selection Wizard
[Click on image for larger view.] The Lambda Blueprint Selection Wizard (source: AWS).

The net effect (no pun intended) is to simplify coding, with AWS providing an example of how the code is used in the normal Lambda programming model:

public APIGatewayHttpApiV2ProxyResponse LambdaMathAdd(APIGatewayHttpApiV2ProxyRequest request, ILambdaContext context)
{
    if (!request.PathParameters.TryGetValue("x", out var xs))
    {
        return new APIGatewayHttpApiV2ProxyResponse
        {
            StatusCode = (int)HttpStatusCode.BadRequest
        };
    }
    if (!request.PathParameters.TryGetValue("y", out var ys))
    {
        return new APIGatewayHttpApiV2ProxyResponse
        {
            StatusCode = (int)HttpStatusCode.BadRequest
        };
    }
    var x = int.Parse(xs);
    var y = int.Parse(ys);
    return new APIGatewayHttpApiV2ProxyResponse
    {
        StatusCode = (int)HttpStatusCode.OK,
        Body = (x + y).ToString(),
        Headers = new Dictionary { { "Content-Type", "text/plain" } }
    };
}

can become much simpler by removing the typical Lambda function code through the use of two .NET attributes that annotate the code:

[LambdaFunction]
[HttpApi(LambdaHttpMethod.Get, "/add/{x}/{y}")]
public int Add(int x, int y)
{
    return x + y;
}

The AC# construct, source generators, is used to bridge the gap between the Lambda Annotations programming model and the normal, more idiomatic programming model. Source generators allow C# developers to inspect user code and generate new source files that can be added during compilation. At a high level, AWS explained that the new library uses .NET Source Builder support to move from Lambda’s low-level, single-event object programming model to a closer ASP.NET Core-like experience with minimal overhead. Since source generators are a C# thing, F# coders are excluded from the Annotations part.

“After adding the attributes to your .NET code, the source generator will generate the translation code between the two programming models,” AWS said. “It will also keep generated information in sync, including a new function handler string in the CloudFormation template. Using the source generator is transparent to the user.”

The Amazon.Lambda.Annotations NuGet package contains the .NET attributes used to annotate code for Lambda, as well as the C# source generator used to create the generated translation code. It has been downloaded over 221,000 times.

Currently, the preview offering only supports Lambda functions leveraging REST APIs, but AWS hopes to simplify other event sources like S3 and DynamoDB events in the future.

More information can be found in:

About the Author


David Ramel is an editor and writer for Converge360.



Comments are closed.