![]() |
VOOZH | about |
This package has been renamed to MinimalLambda. Please migrate to the new package: MinimalLambda
dotnet add package AwsLambda.Host --version 1.3.1
NuGet\Install-Package AwsLambda.Host -Version 1.3.1
<PackageReference Include="AwsLambda.Host" Version="1.3.1" />
<PackageVersion Include="AwsLambda.Host" Version="1.3.1" />Directory.Packages.props
<PackageReference Include="AwsLambda.Host" />Project file
paket add AwsLambda.Host --version 1.3.1
#r "nuget: AwsLambda.Host, 1.3.1"
#:package AwsLambda.Host@1.3.1
#addin nuget:?package=AwsLambda.Host&version=1.3.1Install as a Cake Addin
#tool nuget:?package=AwsLambda.Host&version=1.3.1Install as a Cake Tool
Core framework for building AWS Lambda functions with dependency injection, middleware, and source generation.
A modern .NET framework for building AWS Lambda functions using familiar ASP.NET Core patterns. The core runtime provides:
Install the NuGet package:
dotnet add package AwsLambda.Host
Ensure your project uses C# 11 or later:
<PropertyGroup>
<LangVersion>11</LangVersion>
</PropertyGroup>
Create a simple Lambda handler:
using AwsLambda.Host.Builder;
using Microsoft.Extensions.Hosting;
var builder = LambdaApplication.CreateBuilder();
var lambda = builder.Build();
// The [Event] attribute marks the parameter that receives the deserialized Lambda event
lambda.MapHandler(([Event] string input) => $"Hello {input}!");
await lambda.RunAsync();
The [Event] attribute tells the framework which parameter receives the deserialized event. You can
also inject dependencies:
lambda.MapHandler(([Event] Order order, IOrderService service) =>
service.Process(order)
);
Add middleware for cross-cutting concerns:
lambda.UseMiddleware(async (context, next) =>
{
Console.WriteLine("Before handler");
await next(context);
Console.WriteLine("After handler");
});
Use OnInit() for setup and OnShutdown() for cleanup:
// Service can be injected into the Init handler
lambda.OnInit(ICache cache =>
{
// Runs once at startup - perfect for setting up resources
cache.Warm();
});
// Handlers can also control if the Init phase should be continued or not
lambda.OnInit(async (services, token) =>
{
// Returns false to abort startup
return true;
});
// Runs once at shutdown - cleanup resources
lambda.OnShutdown(async (services, token) =>
{
// ...
});
// Service can be injected into the shutdown handler too handler
lambda.OnShutdown(ITelemetryService telemetryService =>
{
// Runs once at shutdown - great for cleaning up resources
telemetryService.ForceFlush();
});
Register your Lambda handler with the builder. The framework uses source generation to analyze your handler signature:
[Event] attribute marks the input parameter typeHandlers can inject dependencies alongside the event:
lambda.MapHandler(([Event] Order order, IOrderService service) =>
service.Process(order) // Return type automatically serialized to JSON
);
Middleware wraps the handler for cross-cutting concerns. Add as many middlewares as neededβthey compose into a pipeline:
lambda.UseMiddleware(async (context, next) =>
{
// Pre-handler logic
await next(context);
// Post-handler logic
});
lambda.UseMiddleware(async (context, next) =>
{
// Another middleware layer
await next(context);
});
The framework manages initialization and shutdown phases automatically. Add as many callbacks as neededβthey execute in order and then all awaited:
Both run asynchronously and should be kept as short as possible to minimize startup/shutdown time.
lambda.OnInit(async (services, token) =>
{
// One-time setup (runs once, reused across invocations)
return true; // or false to abort startup
});
lambda.OnShutdown(async (services, token) =>
{
// Cleanup before shutdown
});
Register services in the builder; they're available in handlers, middleware, and lifecycle methods:
builder.Services.AddSingleton<ICache, MemoryCache>(); // Reused across invocations
builder.Services.AddScoped<IRepository, Repository>(); // New per invocation
Each invocation receives its own scopeβscoped services are isolated per request. OnInit() and
OnShutdown() handlers receive their own scopes as well. You can also request the
ILambdaHostContext or CancellationToken in any handler, and they're automatically injected.
The framework uses C# source generators and compile-time interceptors to:
Result: Zero runtime reflection, zero performance cost.
To use .NET Native AOT, define a JSON serializer context and annotate with types to serialize:
using System.Text.Json.Serialization;
[JsonSerializable(typeof(string))]
public partial class SerializerContext : JsonSerializerContext;
Register the serializer context with the application:
using AwsLambda.Host;
var builder = LambdaApplication.CreateBuilder();
builder.Services.AddLambdaSerializerWithContext<SerializerContext>();
var lambda = builder.Build();
The AddLambdaSerializerWithContext<TContext>() method registers a source-generated JSON serializer
that uses your context for all Lambda event and response serialization, providing compile-time
serialization metadata and eliminating runtime reflection.
Enable AOT in your project file:
<PublishAot>true</PublishAot>
<PublishTrimmed>true</PublishTrimmed>
<TrimMode>full</TrimMode>
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
See AOT documentation for details.
The framework supports configuration through LambdaHostOptions:
builder.Services.ConfigureLambdaHostOptions(options =>
{
options.InitTimeout = TimeSpan.FromSeconds(10);
options.InvocationCancellationBuffer = TimeSpan.FromSeconds(5);
options.ShutdownDuration = ShutdownDuration.ExternalExtensions;
options.ShutdownDurationBuffer = TimeSpan.FromMilliseconds(100);
options.ClearLambdaOutputFormatting = true;
});
Available options include timeout control, shutdown duration, output formatting, and JSON
serialization customization. The framework automatically registers DefaultLambdaHostJsonSerializer
which uses JsonSerializerOptions and JsonWriterOptions for all Lambda serialization. See
the configuration guide for details.
Additional packages in the aws-lambda-host framework for abstractions, observability, and event source handling.
This project is licensed under the MIT License. See for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 1 NuGet packages that depend on AwsLambda.Host:
| Package | Downloads |
|---|---|
|
AlexaVoxCraft.Lambda.Host
Lambda hosting and middleware integration for Alexa skills using MediatR and AlexaVoxCraft. |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|