Search code examples
terraformamazon-rdsamazon-ecsamazon-vpc

How to connect an ECS instance to a RDS database on the same VPC with Terraform?


I am using Terraform to automatize this process. I could already connect the ECS to the RDS using the auto-generated RDS' DNS.

The problem with this approach is that I needed to regenerate the Docker image with this generated RDS' DNS in the environment variables and then upload the new working image to ECR.

Since both are running on the same VPC, is there a way to connect to the RDS using a fixed RDS hostname?


Solution

  • You have this tagged with Terraform, so using Terraform you would take the RDS instance's DNS address output from the RDS resource, and pass it as an environment variable to your ECS task via the ECS task definition.