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년 전

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

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

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

관련 콘텐츠