AWS Lambda Node.js Handler 的 callback 效果

文件 AWS Lambda Function Handler in Node.js 中寫到:

The Node.js runtimes v6.10 and v8.10 support the optional callback parameter. You can use it to explicitly return information back to the caller.



When the callback is called (explicitly or implicitly), AWS Lambda continues the Lambda function invocation until the event loop is empty.

雖然呼叫 callback 塞了要回傳的資料,Lambda Function 還是會繼續執行到 event loop 清空,但呼叫端其實也沒辦法先得到結果。

AWS Lambda Context Object in Node.js 裡說明 context.callbackWaitsForEmptyEventLoop:

callbackWaitsForEmptyEventLoop – Set to false to send the response right away when the callback executes, instead of waiting for the Node.js event loop to be empty. If false, any outstanding events will continue to run during the next invocation.

也就是說如果把 callbackWaitsForEmptyEventLoop 設為 false,雖然會在 callback 被呼叫時,立刻送出 response,但是 Lambda Function 就會進入 freeze 狀態,其他還未執行的 event 會再下次被呼叫時才執行到。

結論就是,一個 Node.js Lambda Function 本身無法做到先把結果回傳給呼叫端,本身繼續處理完其他可以非同步處理,不影響回傳結果的事。有一個稍微變通方式可以做到,是用 Event 方式呼叫另一個 Lambda Function 做額外非同步處理。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *