The question has already been answered but I wanted to add if you are connecting to public DNS of RDS(eg. prod.upd9999upd.us-east-1.rds.amazonaws.com
) then you must enable DNS resolution to private IP. This is done through AllowDnsResolutionFromRemoteVpc
.
Example: To connect Vpc EC2_PROD(172.0.0.0/16
) to Vpc RDS_PROD(30.0.0.0/16
).
1) Create Peering connection from EC2 VPC(Requester) to RDS VPC (Accepter).
Make sure to enable AllowDnsResolutionFromRemoteVpc
with UI by right clicking on already created peering connection and "Edit DNS Settings". Or with following command
aws ec2 modify-vpc-peering-connection-options --vpc-peering-connection-id "pcx-04a511409bb08ef16" --requester-peering-connection-options '{"AllowDnsResolutionFromRemoteVpc":true}' --accepter-peering-connection-options '{"AllowDnsResolutionFromRemoteVpc":true}' --region us-east-1
Your final peering connection will look like:
aws ec2 describe-vpc-peering-connections --profile aws-work --region us-east-1
{
"VpcPeeringConnections": [
{
"Status": {
"Message": "Active",
"Code": "active"
},
"Tags": [
{
"Value": "ec2-to-rds-peering-connection",
"Key": "Name"
}
],
"AccepterVpcInfo": {
"PeeringOptions": {
"AllowEgressFromLocalVpcToRemoteClassicLink": false,
"AllowDnsResolutionFromRemoteVpc": true,
"AllowEgressFromLocalClassicLinkToRemoteVpc": false
},
"VpcId": "vpc-RDS",
"Region": "us-east-1",
"OwnerId": "?",
"CidrBlockSet": [
{
"CidrBlock": "30.0.0.0/16"
}
],
"CidrBlock": "30.0.0.0/16"
},
"VpcPeeringConnectionId": "pcx-04a511409bb08ef16",
"RequesterVpcInfo": {
"PeeringOptions": {
"AllowEgressFromLocalVpcToRemoteClassicLink": false,
"AllowDnsResolutionFromRemoteVpc": true,
"AllowEgressFromLocalClassicLinkToRemoteVpc": false
},
"VpcId": "vpc-ec2",
"Region": "us-east-1",
"OwnerId": "?",
"CidrBlockSet": [
{
"CidrBlock": "172.0.0.0/16"
}
],
"CidrBlock": "172.0.0.0/16"
}
}
]
}
2) Your Requester VPC (Ec2 VPC) route table must have Accepter IP cider(eg 30.0.0.0/16
) added. (see Routes
tab below)
aws ec2 describe-route-tables --filters Name=tag:Name,Values=EC2_PROD --profile aws-work --region us-east-1
{
"RouteTables": [
{
"Associations": [
{
"RouteTableAssociationId": "rtbassoc-?",
"Main": true,
"RouteTableId": "rtb-?"
}
],
"RouteTableId": "rtb-?",
"VpcId": "vpc-EC2_PROD",
"PropagatingVgws": [],
"Tags": [
{
"Value": "EC2_PROD",
"Key": "Name"
}
],
"Routes": [
{
"GatewayId": "local",
"DestinationCidrBlock": "172.0.0.0/16",
"State": "active",
"Origin": "CreateRouteTable"
},
{
"Origin": "CreateRoute",
"DestinationCidrBlock": "30.0.0.0/16", // Accepter IP cider block
"State": "active",
"VpcPeeringConnectionId": "pcx-04a511409bb08ef16"
},
{
"GatewayId": "igw-???",
"DestinationCidrBlock": "0.0.0.0/0",
"State": "active",
"Origin": "CreateRoute"
}
]
}
]
}
3) Similarly Acceptor VPC (RDS VPC) route table must have Requester IP cider(eg. 172.0.0.0/16
) added. (see Routes
tab below)
aws ec2 describe-route-tables --filters Name=tag:Name,Values=RDS_PROD --profile aws-work --region us-east-1
{
"RouteTables": [
{
"Associations": [
{
"SubnetId": "subnet-?",
"RouteTableAssociationId": "rtbassoc-?",
"Main": false,
"RouteTableId": "rtb-?"
}
],
"RouteTableId": "rtb-?",
"VpcId": "vpc-RDS",
"PropagatingVgws": [],
"Tags": [
{
"Value": "RDS_PROD",
"Key": "Name"
}
],
"Routes": [
{
"Origin": "CreateRoute",
"DestinationCidrBlock": "172.0.0.0/16", // Requester IP cider block
"State": "active",
"VpcPeeringConnectionId": "pcx-04a511409bb08ef16"
},
{
"GatewayId": "local",
"DestinationCidrBlock": "30.0.0.0/16",
"State": "active",
"Origin": "CreateRouteTable"
},
{
"GatewayId": "igw-???",
"DestinationCidrBlock": "0.0.0.0/0",
"State": "active",
"Origin": "CreateRoute"
}
]
}
]
}
4) Finally also update firewall/ security group on Accepter VPC(RDS) to allow connection from Ec2 VPC on port 3306 if its mysql.
aws ec2 describe-security-groups --filters Name=tag:Name,Values=RDS_FIREWALL --profile aws-work --region us-east-1
{
"SecurityGroups": [
{
"IpPermissionsEgress": [
{
"IpProtocol": "-1",
"PrefixListIds": [],
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"UserIdGroupPairs": [],
"Ipv6Ranges": []
}
],
"Description": "Dev",
"Tags": [
{
"Value": "RDS_FIREWALL",
"Key": "Name"
}
],
"IpPermissions": [
{
"PrefixListIds": [],
"FromPort": 3306,
"IpRanges": [
{
"Description": "EC2_VPC_IP_CIDER",
"CidrIp": "172.0.0.0/16"
}
],
"ToPort": 3306,
"IpProtocol": "tcp",
"UserIdGroupPairs": [],
"Ipv6Ranges": []
}
],
"GroupName": "RDS_FIREWALL",
"VpcId": "vpc-???",
"OwnerId": "???",
"GroupId": "sg-???"
}
]
}