- Notifications
You must be signed in to change notification settings - Fork 14
Description
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.
Status
Currently, 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.
Update April 8th, 2025
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.
Update March 4th, 2025
The AWS Lambda integration has reached developer preview status. The following blog posts have been released for the preview.
- Building and Debugging .NET Lambda applications with .NET Aspire (Part 1)
- Building and Debugging .NET Lambda applications with .NET Aspire (Part 2)
9.1.4 Release February 27th 2025
- Update Aspire dependency to version 9.1.0
- Update Amazon.Lambda.TestTool to version 0.9.0
9.1.3 Release February 20th 2025
- Add support for Lambda function's written as class libraries. See "Lambda Class Library Programming model" below for more details.
- Add ability to configure Lambda log format and level using the options parameter of
AddAWSLambdaFunction.
9.1.2 Release February 7th 2025
- Amazon.Lambda.TestTool the underlying emulator will now be automatically installed/updated as part of AppHost startup. If you want to customize the install/update behavior use the
AddAWSLambdaServiceEmulatormethod. - Integrate Aspire's Opentelemetry exporters for Lambda function. For example on how to use Aspire's Service Defaults project with .NET Lambda functions check out the repository playground project for Lambda: https://github.com/aws/integrations-on-dotnet-aspire-for-aws/tree/main/playground/Lambda
Lambda Class Library Programming model
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.
Getting Started
Before getting started ensure Visual Studio is updated to the latest version. This will ensure you have the latest .NET Aspire workload installed.
- Create or open a Lambda function in Visual Studio that is using the executable programming model. In Visual Studio project templates you can use the “Empty Top-Level Function” blueprint.
- In the solution add a new project using the .NET Aspire AppHost project template. For the .NET Aspire version you should choose the latest version. As of this writing is 9.0.
- On the AppHost add a NuGet reference to Aspire.Hosting.AWS version 9.1.3 or above.
- On the AppHost project add a project reference to the Lambda project.
- In the
Program.csfile of the AppHost add the following code. Be sure to change theProjects.GettingStartedLambdaAspireto the name of the your project and set thelambdaHandlerto 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(); - Make the AppHost project the startup project and push F5 to launch the Visual Studio debugger.
- From the Aspire dashboard the Lambda project will be added as a resource as well as the Lambda service emulator. To debug the Lambda function click the debugger icon in the actions for the Lambda project.
- The debugger icon will launch the Lambda test page where Lambda events can be invoked. Break points can be set in the Lambda function for debugging.
Debugging API Gateway Lambda functions
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.
Logs
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.





