1
votes

I am developing a web app with AWS Lambda and API Gateway with Nodejs. When I run my api with localhost through serverless offline (pointing my production DB, on AWS RDS) everything works fine. However, when I deploy to AWS I get the following:

module initialization error: Error at new ConnectionManager (/var/task/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:29:15) at new PostgresDialect (/var/task/node_modules/sequelize/lib/dialects/postgres/index.js:14:30) at new Sequelize (/var/task/node_modules/sequelize/lib/sequelize.js:239:20) at Object. (/var/task/src/controllers/run-controller.js:4

I assume this means there is some issue connecting to the database. I have no issue hitting it with postman via localhost, but using my APIG url, the above error is thrown and shown in the cloudwatch logs.

Please let me know what other information I can provide!

Update Narrowing in on this a bit more, when I run a test in the lambda console against one of my functions, it shows the following:

"errorMessage": "Please install 'pg' module manually"

It seems my node modules are not available? Like I said, I'm using Serverless, does sls deploy not include node modules?

Update 2 Indeed there was an issue with the node module pg not being included in the package. I've added pg to be forced included with the following:

custom:
  webpackIncludeModules:
    forceInclude:
      - pg

That resolves the above issue with needing to install pg manually. However, I'm now only getting timeouts from my deployed functions, but they still work locally.

1
did you attach policies to the lambda for accessing RDS? If not, that will definitely cause connection failures. - Kim Burgaard
facepalm Thank you! This is almost certainly the issue, I'm looking into adding the policy. - Justin Kruse
@KimBurgaard - In addition to the updates above, I added a policy with the following statement: { "Effect": "Allow", "Action": [ "rds-db:connect" ], "Resource": [ "arn:aws:rds-db:us-west-2:<my-accountId>:dbuser:*/<my-db-username>" ] } I pulled it from the docs: docs.aws.amazon.com/AmazonRDS/latest/UserGuide/… however, it says rds-db is not a recognized service and connect is not a recognized action... any suggestions? - Justin Kruse

1 Answers

0
votes

I figured out what the issue was. Not only did I need to make sure that pg was getting included properly with webpack, but I also needed to set up the VPC configuration in my serverless.yml under the provider section:

vpc:
  securityGroupIds:
    - sg-######
  subnetIds:
    - subnet-######
    - subnet-######
    - subnet-######

This set up configuration for all of my functions, allowing them to access my database.