ASP.NET Core hosting extensions for AWS Lambda, providing timeout-aware cancellation tokens and graceful request handling. Links Lambda timeout detection with HTTP request cancellation for improved reliability and proper resource cleanup in serverless applications.
$ dotnet add package LayeredCraft.Lambda.AspNetCore.HostingExtensionsExtensions and middleware for Amazon.Lambda.AspNetCoreServer.Hosting. Provides ASP.NET Core components designed specifically for AWS Lambda hosting, delivering improved reliability, observability, and developer experience.
dotnet add package LayeredCraft.Lambda.AspNetCore.HostingExtensions
using LayeredCraft.Lambda.AspNetCore.Hosting.Extensions;
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Add Lambda timeout-aware cancellation early in the pipeline
app.UseLambdaTimeoutLinkedCancellation();
// Your other middleware
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Custom safety buffer of 500ms for cleanup operations
app.UseLambdaTimeoutLinkedCancellation(TimeSpan.FromMilliseconds(500));
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
[ApiController]
[Route("api/[controller]")]
public class DataController : ControllerBase
{
private readonly IDataService _dataService;
public DataController(IDataService dataService)
{
_dataService = dataService;
}
[HttpGet]
public async Task<IActionResult> GetData(CancellationToken cancellationToken)
{
try
{
// This will be cancelled if Lambda timeout approaches
var data = await _dataService.GetDataAsync(cancellationToken);
return Ok(data);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
// Lambda timeout occurred - return appropriate response
return StatusCode(504, "Request timeout");
}
}
}
The LambdaTimeoutLinkMiddleware creates a sophisticated cancellation token that triggers when either:
ILambdaContext.RemainingTime with safety buffer)The middleware replaces HttpContext.RequestAborted with this linked token, enabling downstream code to respond to Lambda timeouts through standard CancellationToken patterns.
The configurable safety buffer (default: 250ms) ensures your application has time to:
When running locally (Kestrel, IIS Express) where ILambdaContext is unavailable, the middleware operates as a pass-through with only standard client disconnect cancellation active.
The middleware automatically sets appropriate HTTP status codes on timeout:
We welcome contributions! Please see our Contributing Guidelines for details.
# Clone the repository
git clone https://github.com/LayeredCraft/lambda-aspnetcore-hosting-extensions.git
cd lambda-aspnetcore-hosting-extensions
# Restore dependencies
dotnet restore
# Build the project
dotnet build
# Run tests
dotnet test
This project is licensed under the MIT License.
See CHANGELOG.md for details on releases and changes.
Built with ❤️ by the LayeredCraft team