All Posts

Return large objects with AWS Lambda’s new Streaming Response

How it works

const { Readable } = require('stream')

module.exports.handler = awslambda.streamifyResponse(
    async (requestStream, responseStream, context) => {
        const file = await downloadLargeFileFromS3()
        const fileStream = Readable.from(file)
        fileStream.pipe(responseStream)
        await responseStream.finished()
    }
)

async (requestStream, responseStream, context) => …

async (event, context) => …

Use cases

Using API Gateway websockets to send progress updates from a long-running function.

Using client-side polling to send progress updates from a long-running function.

With streaming response, you can drastically simplify things. The invocation that handles the initial user request can stream periodic updates to the client and then send the full result when it’s ready.

Using streaming response to send progress updates from a long-running function.

Caveats

Payload size limit

Compatibility with API Gateway and ALB

Compatibility with ALB

Runtime support

 

This may also interest you