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
已提問 2 個月前檢視次數 113 次
2 個答案
0

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

profile picture
專家
Artem
已回答 2 個月前
  • 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
專家
已回答 2 個月前
  • 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.

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南