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 Antwort
3
Akzeptierte Antwort

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
EXPERTE
Uri
beantwortet vor 2 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen