Skip to content

Add support for Lambda Response Streaming#2288

Draft
normj wants to merge 21 commits intodevfrom
normj/response-streaming
Draft

Add support for Lambda Response Streaming#2288
normj wants to merge 21 commits intodevfrom
normj/response-streaming

Conversation

@normj
Copy link
Member

@normj normj commented Feb 19, 2026

In draft mode while still cleaning up the experience

Issue #, if available:
#1635

Description of changes:
Integrate Lambda response streaming support into the Amazon.Lambda.RuntimeSupport.

A hello world example of using response streaming. In this case sense I wrapped the Stream returned from CreateStream in a StreamWriter the writes will be buffered in StreamWriter till the buffer is full. I call the flush method every 10 iterations to force sending data back to the client.

using Amazon.Lambda.Core;
using Amazon.Lambda.Core.ResponseStreaming;
using Amazon.Lambda.RuntimeSupport;
using Amazon.Lambda.Serialization.SystemTextJson;

#pragma warning disable CA2252

// The function handler that will be called for each Lambda event
var handler = async (string input, ILambdaContext context) =>
{
    using var responseStream = LambdaResponseStreamFactory.CreateStream();

    using var writer = new StreamWriter(responseStream);
    for (var i = 1; i <= 100; i++)
    {
        var message = $"Hello {input} - {i}";
        await writer.WriteLineAsync(message);

        if (i % 10 == 0)
        {
            await writer.FlushAsync();
            await Task.Delay(1000);
        }
    }
};

// Build the Lambda runtime client passing in the handler to call for each
// event and the JSON serializer to use for translating Lambda JSON documents
// to .NET types.
await LambdaBootstrapBuilder.Create(handler, new DefaultLambdaJsonSerializer())
        .Build()
        .RunAsync();

For a use with API Gateway or Lambda Function URL you need to create the stream with the CreateHttpStream passing the status code and response headers.

using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using Amazon.Lambda.Core.ResponseStreaming;
using Amazon.Lambda.RuntimeSupport;
using Amazon.Lambda.Serialization.SystemTextJson;
using System.Net;

#pragma warning disable CA2252

// The function handler that will be called for each Lambda event
var handler = async (APIGatewayProxyRequest request, ILambdaContext context) =>
{
    var prelude = new HttpResponseStreamPrelude
    {
        StatusCode = HttpStatusCode.OK,
        Headers =
        {
            { "Content-Type", "text/plain" }
        }
    };
   
    using var responseStream = LambdaResponseStreamFactory.CreateHttpStream(prelude);

    using var writer = new StreamWriter(responseStream);
    for (var i = 1; i <= 10000000; i++)
    {
        var message = $"Hello - {i} ({responseStream.Length.ToString("N0")}) (Remaining Time: {context.RemainingTime})";
        await writer.WriteLineAsync(message);

        if (i % 100 == 0)
        {
            await writer.FlushAsync();
        }

        if (context.RemainingTime < TimeSpan.FromSeconds(5))
        {
            await writer.WriteLineAsync("Approaching Lambda timeout, stopping the stream.");
            await writer.FlushAsync();
            break;
        }
    }
};

// Build the Lambda runtime client passing in the handler to call for each
// event and the JSON serializer to use for translating Lambda JSON documents
// to .NET types.
await LambdaBootstrapBuilder.Create(handler, new DefaultLambdaJsonSerializer())
        .Build()
        .RunAsync();

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@normj normj marked this pull request as draft February 19, 2026 01:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant