Issues with promises/callbacks/async/await

I gotta say, I’m having immense problems with Lambda promise/callbacks vs running node locally. Wondering if anyone else has had the weeks of headaches we’ve run into.

Running Node 8.10 with Sequelize + RDS Postgres. Locally I’ve got serverless-webpack and serverless-offline working beautifully, everything is great, no issues.

In production it’s just one multi-hour nighmarish debugging session after another. External API calls just never return, sequelize DB calls failing to return, etc. I’m getting consistent timeouts on all sorts of callbacks, tried in every form: callback hell, wrapping in Promises, async/await. Every single one of them fails for no reason, just times out the lambda session.

What is the deal with AWS Lambda stack? Is anyone else running into these issues? It’s a complete nightmare and I really wish I could get any of this to mimic how it runs for me locally.

Checklist time:

  1. Can your Lambda access RDS? Typically this means - Have you deployed your Lambda inside a VPC that can access the RDS database?
  2. Make sure the first line of your handler function is context.callbackWaitsForEmptyEventLoop = false;

If you can post code that will generally help debugging.

I’m doing as Buggy says, and my async / await code works just fine. Usually if I have a bug it’s because I’m missing an await. Even TypeScript is not very good at noticing such things.

Turns out this was a problem with my VPC. It’s rather easy to shoot yourself in the foot with AWS network settings.

Why would you disable this? Doesn’t seem like good practice. Your event loop should be empty before you finish.

I don’t know enough about Lambda and Node to know why, but my Lambdas just timeout if I don’t include that line.

I would suggest that you console.log() at every major step in your code. Then I would look at cloudwatch to see what the last logged statement was. I have found that I regularly make calls to services or reference variables that don’t exist and the code just exits. Look at you last logged statement and see what happens in your code right after that last console.log(). Also, verify that the lambda hasnt timed out (default is 6 seconds). Your last line in cloudwatch will include how long the function took to execute.

Here is an example of using promises in serverless to read a file: