Search code examples
javascriptnode.jsamazon-web-servicesaws-lambda

How to stream AWS Lambda response in node?


I have an AWS Lambda function, and I need to invoke it from my node app and stream the result back to the client. I've looked in the docs but can't see a way. I want to do something like this:

lambda.invoke(params).then(data => data.pipe(res))

or even

lambda.invoke(params, (err, data) => {
  // data should be a pipeable stream instead of buffered
  data.pipe(res)
})

Solution

  • The Javascript AWS SDK supports streaming the body of the API responses so API calls like getting a large S3 blob of binary data can be streamed to Javascript functions.

    lambda.invoke(lambdaDef)
      .createReadStream()
      .on('data', function(data) {
        console.log("Got data:", data.toString())
      })
    

    You'll get the Payload of the response as data.

    Response Streaming for the lambda functions themselves was added in 2023. For the Node.js 14.x, 16.x and 18.x managed runtimes or a custom runtime, via either a Function URL or when using the AWS SDK directly like above. (Thanks @Eric)

    const { pipeline } = require('node:stream/promises')
    
    exports.handler = awslambda.streamifyResponse(
      async (event, response_stream, _context) => {
        const data = createSomeReadStream()
        await pipeline(data, responseStream)
      }
    )