Determine Lambda URL invoke type

0

Is it possible to detect the type of invoke (BUFFERED or RESPONSE_STREAM) being used in the Lambda handler (Node.js)?

I do not see this documented anywhere.

I've also inspected the request event, environment variables and context of the Lambda to see if perhaps it is set somewhere, but also do not see this anywhere.

This would be very useful for writing handlers that can work with both types of requests.

profile picture
m0ltar
asked a month ago106 views
2 Answers
0

Have you tried inspecting the event itself, the one which triggers the Lambda?

profile picture
EXPERT
Artem
answered a month ago
  • Yes, I did. I used the wrong term when I said "request", it is, in fact, an "event". Edited. I looked at all known "variables" essentially, and I do not see anything obvious.

0

There is no direct way to detect the invoke type (buffered or response stream) from within the Lambda function itself. The invoke type is determined by how the function is called, either synchronously through the Lambda API or asynchronously through event sources like SNS.

A few options to help write handlers that can support both invoke types:

  • Check for the presence of callback parameter in the handler. If it exists, the function was called synchronously and response should be returned via callback. If not present, it was called asynchronously.
  • Inspect the Lambda event object passed to the handler. Events from asynchronous sources like SNS will have a different structure than the raw payload passed during synchronous invokes.
  • Abstract away the differences in a common interface. The handler can call this interface, which then determines invoke type and handles the response appropriately - either returning directly or via callback.
  • Set context.callbackWaitsForEmptyEventLoop=true when a synchronous response is expected. This will prevent the function from exiting until the callback is called.
profile picture
EXPERT
answered a month ago
  • I think you are mixing up the concerns. I was not talking about sync/async. But rather about the Lambda Function URL response type, of which there are two types, both of which are async, given the nature of the HTTP. However, one is using a buffered approach, where the entire response is "buffered" in memory before Lambda spits it out (this is the traditional method). And then there's the new method for response streaming, which uses native Node.js Writable stream to write the response as it arrives.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions