Cannot connect docker container on ECS Fargate to RDS db instance

0

I've been pulling my hair out trying to connect my docker container on ECS Fargate to my RDS instance without success for the past 3 hours and I don't know why it's not working.

I have a ruby docker image with db config like this:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
production:
  <<: *default
  host: <%= ENV['DATABASE_URL'] %>
  username: <%= ENV['DATABASE_USERNAME'] %>
  password: <%= ENV['DATABASE_PASSWORD'] %>
  database: my_project_db

where

DATABASE_URL=myprojectdbinstance.c42n4hggvli1.eu-central-1.rds.amazonaws.com

which is the RDS endpoint URL. My database identifier name is myprojectdbinstance and initial database name is my_project_db.

When I run my ECS task I get an error:

ActiveRecord::NoDatabaseError: We could not find your database: myprojectdbinstance.c42n4hggvli1.eu-central-1.rds.amazonaws.com. Which can be found in the database configuration file located at config/database.yml

PG::ConnectionBad: could not translate host name "myprojectdbinstance.c42n4hggvli1.eu-central-1.rds.amazonaws.com" to address: Name does not resolve

My ECS and RDS db are in the same VPC. The ECS Task is in projects-alb-sg security group, my RDS accepts inbound traffic from projects-alb-sg security group. My VPC supports DNS resolution and DNS hostnames and RDS database is publicly available. I have created DB subnet group with the same subnets where my ECS task is located. I have already tried all possible settings and it doesn't work.

My VPC has DNS resolution enabled.

What am I missing?

3 Antworten
0

¿Maybe you need a public IP to resolve "myprojectdbinstance.c42n4hggvli1.eu-central-1.rds.amazonaws.com" DNS?

beantwortet vor einem Jahr
  • I do set Public IP on the task

0

Fagrate 1.3 or 1.4 ?

With Fargate 1.4, did you create a Task Execution Role ?

Take a look at this tutorial.

https://aws.amazon.com/premiumsupport/knowledge-center/ecs-fargate-task-database-connection/

Did you create the environment variables in the task definition ?

profile pictureAWS
Roly
beantwortet vor einem Jahr
  • Fargate is latest, I do have the default execution role ecsTaskExecutionRole. I do have the environment variables in the task definition. DATABASE_URL, DATABASE_USERNAME and DATABASE_PASSWORD, which point to my RDS DB endpoint, username and password.

0

It turned out that the right DATABASE_URL should be postgres://postgres@mydbendpoint/my_project_db and it worked.

jedrek
beantwortet vor einem Jahr

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen