Freedom_Ben Freedom_Ben - 3 months ago 47
Javascript Question

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)
})

Answer

The Javascript AWS SDK supports streaming the body of the http response so calls like getting a large S3 blob of binary data can be streamed.

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

You'll get the Payload as data.

The Node.js lambda functions themselves don't support streaming, just a callback. Although it looks like you can stream in Java.