Public ECS and Private RDS Connectivity under different VPCs

0

We have an existing ECS Cluster, and inside it, there's a service with a task definition container. The database host is stated in task definition. I created a new database, but now it is private (new vpc, new subnets). I also set up an EC2 instance to connect as a bastion host. How can my ECS task definition connect to the private RDS, knowing that my ECS were created under default VPC in public, and the RDS were on custom vpc in private subnet. I have tested the connectivity manually via MySQL workbench but currently my application cannot connect on the RDS.

3 Answers
1

Hello.

If the VPCs are different, you will need to set up VPC peering or TransitGateway to enable communication between the VPCs.
https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_VPC.Scenarios.html#USER_VPC.Scenario3

You can configure VPC peering by following the steps in the document below.
After completing the VPC peering settings, try configuring the RDS security group to allow the ECS task security group.
https://docs.aws.amazon.com/vpc/latest/peering/create-vpc-peering-connection.html

profile picture
EXPERT
answered 16 days ago
profile picture
EXPERT
Steve_M
reviewed 16 days ago
  • HI, I created a new ECS Service under new VPC (VPC used by my RDS) if this is the case how can I connect my service to the RDS. I am very new to aws, if possible you could provide the step by step would be appreciated

  • If you have created an ECS service in the same VPC as RDS, you can connect by configuring the security group. Please set the inbound rules of the security group set in RDS as follows.

    TypeProtocolPortsource
    MySQL/AuroraTCP3306ECS Security Group

    Rules can be added to security groups by following the steps in the document below. https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/working-with-security-groups.html#adding-security-group-rule

0

Hey Hii,

Your ECS tasks can't reach the private RDS because they're in separate VPCs. you will try to fix it simply using AWS PrivateLink.

  1. Create a PrivateLink endpoint for your RDS in the custom VPC. This is a private connection point within your VPC. [https://docs.aws.amazon.com/vpc/latest/privatelink/what-is-privatelink.html ]

  2. Update your ECS task definition security group to allow traffic from the PrivateLink endpoint's security group.

  3. Use environment variables in your task definition instead of directly referencing the database host. Update your application to use this variable at runtime. This injects the PrivateLink endpoint address during launch.

I think in this way it will create everything secure and isolated!

answered 16 days ago
0

If your ECS tasks and RDS instance are in different VPCs, you have a few options to enable connectivity between them:

VPC Peering:

Create a VPC peering connection between the two VPCs (the one hosting the ECS cluster and the one hosting the RDS instance). Update the security groups in both VPCs to allow inbound traffic from the other VPC's security group associated with the ECS tasks and RDS instance, respectively. In your ECS task definition, use the private IP address or DNS name of the RDS instance to connect to it.

AWS PrivateLink:

Create an AWS PrivateLink endpoint for your RDS instance in the VPC where your ECS cluster resides. Update your ECS task definition to use the PrivateLink endpoint to connect to the RDS instance. Configure the security groups to allow inbound traffic from the ECS tasks to the PrivateLink endpoint.

AWS Transit Gateway:

Set up an AWS Transit Gateway and associate both VPCs (ECS and RDS) with it. Configure routing tables in both VPCs to route traffic destined for the other VPC through the Transit Gateway. Update the security groups in both VPCs to allow inbound traffic from the other VPC's security group associated with the ECS tasks and RDS instance, respectively. In your ECS task definition, use the private IP address or DNS name of the RDS instance to connect to it.

Regardless of the option you choose, you'll need to ensure that the security groups are configured correctly to allow traffic between the ECS tasks and the RDS instance. Additionally, you may need to configure DNS resolution within your VPCs or use the private IP addresses or DNS names of the RDS instance in your application code.

AWS
answered 12 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions