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.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠