Search code examples
node.jspostgresqlamazon-web-servicesaws-lambdanode-postgres

Node function on AWS Lambda can't connect to database with node-pg


My function works locally, but, when I deploy to AWS Lambda, it can't seem to connect to my postgres database. Here's the error:

{ [Error: connect ECONNREFUSED] code: 'ECONNREFUSED', errno: 'ECONNREFUSED', syscall: 'connect' }, isOperational: true, code: 'ECONNREFUSED', errno: 'ECONNREFUSED', syscall: 'connect' }

My database is hosted on an Azure virtual machine, and I'm not having problems connecting to it from any other app nor from this app when run locally. What could be causing the connection to fail when running on Lambda?


Solution

  • It turned out to be something dumb. I am using node-lambda and thought the .env file it creates was propagated to the function's environment. Logging my DB connection string showed this wasn't the case. Once I set that, everything was golden.