The next major effort being worked on by the AWS team is build a local development environment for .NET Lambda functions. The work is being done across this aspire repository and the aws/aws-lambda-dotnet where a new version of the Mock Lambda Test tool is being created to act as a Lambda service emulator.
StatusCurrently, the .NET Aspire integration Lambda local development is in preview. The new AddAWSLambdaFunction
and AddAWSAPIGatewayEmulator
APIs are marked with the RequiresPreviewFeatures
attribute which requires users to opt in for preview features to use them. The easiest way to opt-in is to add the line #pragma warning disable CA2252
. At the start of your AppHost’s Program.cs
.
The Lambda integration relies on a new .NET Tool called Amazon.Lambda.TestTool which is currently in preview. Starting with version 9.1.2
of Aspire.Hosting.AWS the package the tool will automatically be installed/updated as part of the AppHost startup.
Presented on the AWS Serverless office hours show demonstrating the AWS Lambda + .NET Aspire support.
https://www.twitch.tv/videos/2427524327
Version 9.1.6 was released with support for SQS as an event source. This can be configured using the WithSQSEventSource
method from the Lambda function.
The AWS Lambda integration has reached developer preview status. The following blog posts have been released for the preview.
AddAWSLambdaFunction
.AddAWSLambdaServiceEmulator
method.Starting with version 9.1.3 Lambda functions written as class libraries can be used in the .NET Aspire AppHost. There is an issue with the JetBrain's Rider IDE that is causing the class library Lambda function projects to not start. The Rider team have committed a fix that will be released in an upcoming EAP version. JetBrains/aspire-plugin#354
Getting class library support working in .NET Aspire required some tricky changes due to IDE and .NET expecting to be debugging an executable. For the most part the tricky changes should be transparent to user's. For those curious or see some unusual behavior here is what we have to do for class library support.
When running the .NET Aspire AppHost project attached to a debugger the AWS integration will write a launch profile into the Lambda project's launchsettings.json
file. The profile sets up the command line arguments for the dotnet CLI to use Amazon.Lambda.RuntimeSupport as the executable that will host the class library Lambda function. Amazon.Lambda.RuntimeSupport is the .NET Lambda runtime client used in Lambda to run Lambda functions. As part of the Amazon.Lambda.TestTool NuGet package we are distributing the binaries of Amazon.Lambda.RuntimeSupport and its dependencies so we can automatically setup the launch profile.
When the .NET Aspire AppHost is run outside of an IDE, for example running dotnet run
in the AppHost's directory, a different behavior had to be used because dotnet run
will not launch a class library even if we specify a launch profile. For this scenario the integration generates in the temp folder a .NET executable project that depends on the Lambda class library and the Amazon.Lambda.RuntimeSupport NuGet package. The code in the generated project launches the RuntimeSupport client using the function handler string the user specified.
Before getting started ensure Visual Studio is updated to the latest version. This will ensure you have the latest .NET Aspire workload installed.
Program.cs
file of the AppHost add the following code. Be sure to change the Projects.GettingStartedLambdaAspire
to the name of the your project and set the lambdaHandler
to your .NET Lambda project’s assembly name.#pragma warning disable CA2252
var builder = DistributedApplication.CreateBuilder(args);
builder.AddAWSLambdaFunction<Projects.GettingStartedLambdaAspire>
("GettingStartedLambda", lambdaHandler: "GettingStartedLambdaAspire");
builder.Build().Run();
The .NET Aspire includes new functionality for debugging Lambda functions used as part of the API Gateway REST or HTTP API. For example using the following code for the .NET Aspire AppHost registers 3 Lambda functions and configures with the API Gateway emulator using the AddAWSAPIGatewayEmulator
method.
using Aspire.Hosting.AWS.Lambda;
#pragma warning disable CA2252 // This API requires opting into preview features
var builder = DistributedApplication.CreateBuilder(args);
var defaultRouteLambda = builder.AddAWSLambdaFunction<Projects.WebDefaultLambdaFunction>("LambdaDefaultRoute", lambdaHandler: "WebDefaultLambdaFunction");
var addRouteLambda = builder.AddAWSLambdaFunction<Projects.WebAddLambdaFunction>("AddDefaultRoute", lambdaHandler: "WebAddLambdaFunction");
var minusRouteLambda = builder.AddAWSLambdaFunction<Projects.WebMinusLambdaFunction>("MinusDefaultRoute", lambdaHandler: "WebMinusLambdaFunction");
builder.AddAWSAPIGatewayEmulator("APIGatewayEmulator", Aspire.Hosting.AWS.Lambda.APIGatewayType.HttpV2)
.WithReference(defaultRouteLambda, Method.Get, "/")
.WithReference(addRouteLambda, Method.Get, "/add/{x}/{y}")
.WithReference(minusRouteLambda, Method.Get, "/minus/{x}/{y}");
builder.Build().Run();
When you launch the .NET Aspire the dashboard shows all of the Lambda functions as well as a new API Gateway emulator. The endpoint shown for the API Gateway emulator can be used like the real endpoint of an API Gateway REST or HTTP API endpoint. Meaning you can make requests to using HTTP clients or browsers. The requests will be translated into the Lambda event and sent to Lambda function that matches the route of the request. For route that use use wild card routes the {proxy+}
token should be used.
The logs of the Lambda function can be viewed by looking in the .NET Aspire dashboard by clicking on the console output of the Lambda resource. The console output of the Lambda service emulator will show what is returned from the Lambda function.
cdschneider, martincostello, luizfds and McDoitMcDoit, Vlaaaaaaad, 474D and AeroqualHayden
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4