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

  • IDE Irony: Coding Errors Cause 'Critical' Vulnerability in Visual Studio

    In a larger-than-normal Patch Tuesday, Microsoft warned of a "critical" vulnerability in Visual Studio that should be fixed immediately if automatic patching isn't enabled, ironically caused by coding errors.

  • Building Blazor Applications

    A trio of Blazor experts will conduct a full-day workshop for devs to learn everything about the tech a a March developer conference in Las Vegas keynoted by Microsoft execs and featuring many Microsoft devs.

  • Gradient Boosting Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the gradient boosting regression technique, where the goal is to predict a single numeric value. Compared to existing library implementations of gradient boosting regression, a from-scratch implementation allows much easier customization and integration with other .NET systems.

  • Microsoft Execs to Tackle AI and Cloud in Dev Conference Keynotes

    AI unsurprisingly is all over keynotes that Microsoft execs will helm to kick off the Visual Studio Live! developer conference in Las Vegas, March 10-14, which the company described as "a must-attend event."

  • Copilot Agentic AI Dev Environment Opens Up to All

    Microsoft removed waitlist restrictions for some of its most advanced GenAI tech, Copilot Workspace, recently made available as a technical preview.

Subscribe on YouTube