After an entire weekend of debugging trying to integrate Sequelize with AWS RDS (Postgres) and serverless-webpack, I thought I’d share some problems I faced and their solutions (there don’t appear to be too many Sequelize topics on this forum).
- Getting Sequelize to work with serverless-webpack
If you’ve just added Sequelize into your code and are getting errors like “module initialization error: Error”, it might be because you’ve excluded dependencies — in Sequelize’s case,pg
. If you followed the serverless-webpack instructions of excluding nodeExternals() in your webpack.config.js, then you’ll run into this problem.
To fix this, you’ll want to force serverless to bundle the pg
library into your lambda code so that Sequelize can actually load. Do this by adding the following to your severless.yml:
custom:
webpackIncludeModules:
forceInclude:
- pg
If that still doesn’t fix things, make sure that your Sequelize connection is actually successfully connecting and authenticating.
- Lambdas are timing out when connecting with AWS RDS. Make sure your Lambdas and RDS DB are in the same VPC, or at least same AZ. If that still doesn’t solve things, then try adding the following line just after your function declaration:
context.callbackWaitsForEmptyEventLoop = false;
This means that the callback() function does not wait for an empty event loop, and will exit the Lambda when reached — ofc unless you want it to wait.
Feel free to add any questions regarding Sequelize here. I’ll be happy to assist where I can.