Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down
35 changes: 35 additions & 0 deletions Libraries/src/Amazon.Lambda.Core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading