News

AWS Previews Framework for Writing .NET 6 Lambda Functions

Amazon Web Services is previewing a new framework for using its recently introduced .NET 6 runtime to create 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 allows .NET-centric cloud coders using C# and leveraging the AWS cloud to do their serverless computing projects with the latest edition of Microsoft's open source developer platform, which debuted last November. The new preview has been in the works since about 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 -- natural to C# and .NET 6 -- manner, along with other benefits such as synchronization of Lambda functions with the CloudFormation (an infrastructure automation platform) template 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 code 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<string, string> { { "Content-Type", "text/plain" } }
    };
}

can become much simpler by removing 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;
}

A C# construct, source generators, are used to bridge the gap between the Lambda Annotations programming model and the normal, more idiomatic, programming model. Source generators let C# developers 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 generator support to translate from the low-level, single-event, object programming model of Lambda to an experience more akin to ASP.NET Core with minimal overhead. Because source generators are a C# thing, F# coders are left out of the Annotations party.

"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 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."

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

For now, 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 at Converge 360.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube