einSelbst
einSelbst

Reputation: 2319

Docker Node app on AWS Lightsail Container Service loosing connection to AWS RDS Postgres

I have a node.js app (express) running in docker deployed on AWS Lightsail Container Service. It uses a postgres database on AWS RDS (via knex).

This might be several related problems.

The connection usually works fine for about 2 days, then sometimes after running migrations, the app on lightsail is not able to connect to RDS.

For some time I still can connect to the RDS DB from DBeaver, my local running non-dockerized app and also my local running dockerized version of the app but after a while these connections also stops working.

At that point there is no way to access the RDS DB anymore.

I then start to do things like deleting and recreating the VPC Peering (Lightsail VPC - default VPC), delete and re-add the inbound rules to the security group, create another database. Basically everything I can think of. At some point the connection starts working again. Unfortunately this time it still doesn't work from the app on lightsail, but only from my dev machine.

Things I triple-checked:

The error that shows in the Lightsail Docker Logs is

[23/Oct/2022:16:28:42] Error: connect ECONNREFUSED 127.0.0.1:5432
[23/Oct/2022:16:28:42] at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1148:16)

What could it be that I have overlooked?

Upvotes: 0

Views: 308

Answers (1)

einSelbst
einSelbst

Reputation: 2319

I found the issue. I was loading the wrong config because an environment var wasn't set.

export function getConfig(processVariables: ProcessVariables): Config {
  const environment: Environment = processVariables.ENV || 'local'
  switch (environment) {
    case 'production':
      return getProductionConfig(processVariables)
    case 'localdocker':
      return getLocalDockerConfig(processVariables)
    case 'local':
      return getLocalConfig(processVariables)
  }
}

For localdocker I did set the ENV env var, but not for production. Whenever I switched the database connection for local because I was running migrations against the live db and deployed before I changed the database connection back, production was working.

What helped confirm the issue was debugging the knex connection string.

I do hope I don't experience the issue again where I can't connect to RDS at all.

Upvotes: 0

Related Questions