Serverless lambda rest api not updating database properly

0

I am invoking my lambda REST api to update tables in dynamo db but it only updates the database after the second time its invoked. e.g - 1st call I post "some data" to lambda api it doesn't do anything but 2nd call I post "other data" it will process the first call I made("some data"). If I call lambda the third time then it will process "other data". If I wait between 1st and 2nd call the "some data" is lost and never processed. Everything works fine when I run the server locally. I am using api-gateway, lambda and express serverless-http packages. I have tried logging before and after the db calls are made looks like the code is executing but the database is not updating. What am I doing wrong?

1回答
3
承認された回答

As you are using node.js I think the issue is that the function returns before the function finished updating the database. Once the function returns, the Lambda execution environment is frozen and it will be unfrozen when the next request is received.

You should either update the DB using a synchronous mode, i.e., use await when updating the DB, or make sure that the context.callbackWaitsForEmptyEventLoop is set to true. See more information here.

profile pictureAWS
エキスパート
Uri
回答済み 2年前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ