From 9e9c88f1ac039fbe224528738930ccfd776949fd Mon Sep 17 00:00:00 2001 From: Norm Johanson Date: Thu, 6 Aug 2026 11:16:37 -0700 Subject: [PATCH] Update README.md files for Response Streaming --- .../README.md | 31 ++++++++++++++++ Libraries/src/Amazon.Lambda.Core/README.md | 35 +++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/README.md b/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/README.md index 0098747bf..ac550e4d1 100644 --- a/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/README.md +++ b/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/README.md @@ -87,6 +87,37 @@ builder.Services.AddAWSLambdaHosting(LambdaEventSource.HttpApi, options => }); ``` +### Response streaming + +You can stream the ASP.NET Core response back to the caller incrementally instead of buffering the entire payload. This raises the maximum response size beyond the standard 6 MB buffered limit and lets clients start receiving data sooner. Enable it by setting `EnableResponseStreaming` to `true`: + +```csharp +builder.Services.AddAWSLambdaHosting(LambdaEventSource.RestApi, options => +{ + options.EnableResponseStreaming = true; +}); +``` + +When enabled, the hosting layer builds the HTTP prelude from the response's status code, headers, and cookies and streams the body through the Lambda response stream. Standard results such as `Results.Json(...)` and `Results.Text(...)` continue to work unchanged. + +#### Configuring API Gateway for streaming + +A streaming function requires a different API Gateway integration than a buffered one. In your CloudFormation/`serverless.template`, the `x-amazon-apigateway-integration` must point the integration URI at the `/response-streaming-invocations` path (instead of the buffered `/invocations` path) and set `responseTransferMode` to `STREAM`: + +```json +"x-amazon-apigateway-integration": { + "type": "aws_proxy", + "httpMethod": "POST", + "payloadFormatVersion": "1.0", + "uri": { + "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2021-11-15/functions/${AspNetCoreFunction.Arn}/response-streaming-invocations" + }, + "responseTransferMode": "STREAM" +} +``` + +For more details and end-to-end examples, see [Announcing response streaming for .NET on AWS Lambda](https://aws.amazon.com/blogs/developer/announcing-response-streaming-for-net-on-aws-lambda/). + ### Customizing request and response marshalling Callbacks let you inspect or modify the ASP.NET Core feature objects after the Lambda event has been marshalled into them. The second parameter is the raw Lambda request or response object — cast it to the appropriate type for your event source (`APIGatewayHttpApiV2ProxyRequest` for `HttpApi`, `APIGatewayProxyRequest` for `RestApi`, `ApplicationLoadBalancerRequest` for `ApplicationLoadBalancer`). diff --git a/Libraries/src/Amazon.Lambda.Core/README.md b/Libraries/src/Amazon.Lambda.Core/README.md index 62e1b993a..519ab7f47 100644 --- a/Libraries/src/Amazon.Lambda.Core/README.md +++ b/Libraries/src/Amazon.Lambda.Core/README.md @@ -84,6 +84,41 @@ public string ToUpper(string input, ILambdaContext context) } ``` +## Response Streaming + +This package includes types under the `Amazon.Lambda.Core.ResponseStreaming` namespace that let a handler stream its response back incrementally instead of buffering the entire payload. This raises the maximum response size beyond the standard 6 MB buffered limit and lets callers receive data as soon as it is produced. + +Use `LambdaResponseStreamFactory` to create a write-only `LambdaResponseStream` (a `System.IO.Stream`) and write to it with any standard stream consumer, such as `StreamWriter`. Once a handler creates a response stream, all output must be written to the stream and the handler's return value is ignored. + +```csharp +using Amazon.Lambda.Core.ResponseStreaming; + +public async Task StreamHandler(string input, ILambdaContext context) +{ + await using var responseStream = LambdaResponseStreamFactory.CreateStream(); + using var writer = new StreamWriter(responseStream); + + for (var i = 0; i < 5; i++) + { + await writer.WriteLineAsync($"Chunk {i}"); + await writer.FlushAsync(); + } +} +``` + +When the function is invoked through a Lambda Function URL or API Gateway, use `CreateHttpStream(HttpResponseStreamPrelude)` instead. The prelude sets the HTTP status code, headers, and cookies and is sent as the first chunk before the response body. + +```csharp +var prelude = new HttpResponseStreamPrelude +{ + StatusCode = HttpStatusCode.OK, + Headers = { ["Content-Type"] = "text/plain" } +}; +await using var responseStream = LambdaResponseStreamFactory.CreateHttpStream(prelude); +``` + +Response streaming also requires a current version of the `Amazon.Lambda.RuntimeSupport` package. For more details and end-to-end examples, see [Announcing response streaming for .NET on AWS Lambda](https://aws.amazon.com/blogs/developer/announcing-response-streaming-for-net-on-aws-lambda/). + ## ILambdaSerializer The `Amazon.Lambda.Core.ILambdaSerializer` interface allows you to implement a custom serializer to convert between arbitrary types and Lambda streams.