Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
})
like image 615
Freedom_Ben Avatar asked Sep 08 '16 01:09

Freedom_Ben


People also ask

How do I return a response in Lambda?

Returning a valueIf you use the RequestResponse invocation type, such as Synchronous invocation, AWS Lambda returns the result of the Python function call to the client invoking the Lambda function (in the HTTP response to the invocation request, serialized into JSON).

Does AWS Lambda support node JS?

AWS Lambda now supports Node. js 16 as both a managed runtime and a container base image. Developers creating serverless applications in Lambda with Node. js 16 can take advantage of new features such as support for Apple silicon for local development, the timers promises API, and enhanced performance.

How do I monitor Lambda invocation?

Invocation metrics are binary indicators of the outcome of an invocation. For example, if the function returns an error, Lambda sends the Errors metric with a value of 1. To get a count of the number of function errors that occurred each minute, view the Sum of the Errors metric with a period of 1 minute.


1 Answers

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.

The Javascript lambda functions don't support any streaming options except for logging and inbound events, just a callback that returns a chunk of data.
The Java SDK does have a specific handler for streams -com.amazonaws.services.lambda.runtime.RequestStreamHandler.

like image 185
Matt Avatar answered Sep 28 '22 07:09

Matt