Sub topic 12
Connecting two VPCs with peering
Prerequisites and scope
This chapter assumes you can read CIDR notation, know how route tables and security groups work, and can run AWS CLI commands. It builds on the CIDR planning and route table chapters. By the end you will be able to create a peering connection, make traffic flow across it, and diagnose the usual reasons it does not.
What a peering connection is
A VPC peering connection is a one-to-one networking link between two VPCs. Instances in either VPC can reach instances in the other by private IP address, as if both were on the same network. Traffic stays on the AWS network and does not cross the public internet.
A peering connection is not a gateway or an appliance. It has no bandwidth limit of its own and no single point of failure. It is a routing relationship that AWS enforces in the underlying network.
The two VPCs can be in the same account or different accounts. They can also be in the same Region or different Regions. Cross-Region peering works the same way as same-Region peering. The difference is that you must name the peer Region when you create the request.
Rules that shape every design
Three rules decide most peering designs.
- CIDR blocks must not overlap. AWS refuses to create the connection if the two VPCs have overlapping IPv4 ranges. If you did not plan ahead, you cannot fix this later without rebuilding one VPC or adding a secondary CIDR block that does not overlap.
- Peering is not transitive. If VPC A peers with VPC B, and VPC B peers with VPC C, then A cannot reach C through B. A needs its own peering connection with C.
- Peering does not forward traffic to gateways. A peer cannot use your internet gateway, NAT gateway, VPN connection, or Direct Connect link. It also cannot use your gateway VPC endpoints. Traffic that reaches your VPC over a peering connection can only be destined for resources inside that VPC.
Because of the transitivity rule, the number of connections grows quickly. A full mesh of n VPCs needs n × (n − 1) / 2 peering connections. Five VPCs need 10. Ten VPCs need 45. That growth is the main reason teams move to Transit Gateway, which the next chapter covers.
The request and accept lifecycle
A peering connection is created in two steps. The owner of the requester VPC sends a request. The owner of the accepter VPC accepts it. The connection moves through these states:
A request that nobody accepts expires after seven days. Once the connection is active, either side can delete it at any time.
An active connection does nothing by itself. Traffic flows only after you add routes and allow it in security groups.
Building a peering connection
The following commands peer two VPCs in the same account and Region. They assume the variables VPC_A, VPC_B, RT_A, and RT_B already hold the two VPC IDs and a route table ID from each VPC. VPC A uses 10.0.0.0/16 and VPC B uses 10.1.0.0/16.
PCX_ID=$(aws ec2 create-vpc-peering-connection \
--vpc-id "$VPC_A" \
--peer-vpc-id "$VPC_B" \
--query VpcPeeringConnection.VpcPeeringConnectionId \
--output text)
aws ec2 accept-vpc-peering-connection --vpc-peering-connection-id "$PCX_ID"
aws ec2 wait vpc-peering-connection-exists --vpc-peering-connection-ids "$PCX_ID"For a different account, add --peer-owner-id with the other account's ID. The other account then runs accept-vpc-peering-connection with its own credentials. For a different Region, add --peer-region. The accepter must then run the accept command in the peer Region.
Add routes on both sides
Each side needs a route that sends the other VPC's CIDR block to the peering connection. This is the step people forget most often, and it must be done in both directions.
aws ec2 create-route \
--route-table-id "$RT_A" \
--destination-cidr-block 10.1.0.0/16 \
--vpc-peering-connection-id "$PCX_ID"
aws ec2 create-route \
--route-table-id "$RT_B" \
--destination-cidr-block 10.0.0.0/16 \
--vpc-peering-connection-id "$PCX_ID"A route belongs to a route table, and a route table applies only to the subnets associated with it. If a VPC has several route tables, such as separate public and private ones, add the route to each table whose subnets need to reach the peer. Prefer the narrowest destination that works. You can route to a single peer subnet, such as 10.1.2.0/24, instead of the whole VPC to limit exposure.
Allow the traffic in security groups
Routes only provide a path. Security groups still decide whether packets are accepted. Within the same Region, a security group rule can name a security group in the peer VPC as its source. In a different account, include the account ID, as in 123456789012/sg-0abc1234. Referencing a security group is better than listing CIDR blocks, because it follows instances as they scale.
aws ec2 authorize-security-group-ingress \
--group-id "$DB_SG" \
--protocol tcp \
--port 5432 \
--source-group "$PEER_APP_SG" \
--group-owner "$PEER_ACCOUNT_ID"Here PEER_APP_SG is a security group in the peer VPC and PEER_ACCOUNT_ID is the account that owns it. Omit --group-owner when both VPCs are in your account. Security group references do not work across Regions. In that case, use the peer VPC's CIDR block as the source.
If you use network ACLs, check that they allow the peer's CIDR block in both directions. Because network ACLs are stateless, you must allow the return traffic on ephemeral ports as well.
DNS across a peering connection
By default, if you resolve the public DNS name of an instance in the peer VPC, you get its public IP address. Traffic to that address leaves through the internet gateway, not over the peering connection. To get the private IP address instead, enable DNS resolution support on the peering connection. Each side sets its own option:
aws ec2 modify-vpc-peering-connection-options \
--vpc-peering-connection-id "$PCX_ID" \
--requester-peering-connection-options AllowDnsResolutionFromRemoteVpc=true \
--accepter-peering-connection-options AllowDnsResolutionFromRemoteVpc=trueBoth VPCs must also have DNS resolution and DNS hostnames enabled. In a cross-account or cross-Region setup, each account runs the modify command for its own side.
Common errors and how to diagnose them
| Symptom | Likely cause | Check |
|---|---|---|
| Create request fails with an overlap error | The two VPCs share part of an IPv4 range | Compare all CIDR blocks, including secondary ones |
| Connection stays in pending-acceptance | The accepter has not accepted, or accepted in the wrong Region | Run the accept command with the accepter's credentials and Region |
| Ping or connection times out | A route is missing on one side | Inspect the route table of the subnet for the peer CIDR |
| Times out with routes in place | The security group or network ACL blocks the traffic | Check the destination's security group and both network ACLs |
| Name resolves to a public IP | DNS resolution from the remote VPC is off | Enable the peering DNS option on both sides |
| A third VPC cannot reach the far side | Peering is not transitive | Create a direct connection or use Transit Gateway |
| Internet-bound traffic from the peer fails | Peers cannot use your NAT gateway or internet gateway | Give the peer its own egress path |
VPC Flow Logs help here. A REJECT record for the peer's address points to a security group or network ACL. If you see no records at all, the traffic is probably not arriving, which suggests a routing problem.
Limits and costs
A VPC has a default quota of 50 active peering connections, and you can raise it. Traffic within the same Availability Zone over a peering connection is free. Traffic between Availability Zones in the same Region, and cross-Region traffic, is charged at data transfer rates. Check current pricing for your Regions.
Peering also has no transit path for edge-to-edge routing. If a shared services VPC needs to give many VPCs access to a VPN or an internet egress point, peering cannot do it. Use Transit Gateway for that.
When to choose peering
Peering fits when you have a small number of VPCs with simple, direct needs. Examples are an application VPC that talks to one shared database VPC, or a pair of accounts that exchange traffic. It has no hourly charge, no throughput cap, and low latency.
Choose Transit Gateway instead when the VPC count grows, when you need transitive routing, or when you want central control of routes and attachments.
Exercise
VPC A (10.0.0.0/16) is peered with VPC B (10.1.0.0/16). The peering connection is active and the security groups allow the traffic, but an instance in A cannot reach an instance in B. You find a route to 10.1.0.0/16 through the peering connection in A's route table. What should you check next, and why?
Show answer
Check VPC B's route table for the subnet that holds the target instance. It needs a route to 10.0.0.0/16 through the peering connection. Without it, packets reach B but replies have no path back to A. Also confirm that you edited the route table associated with the instance's subnet and not a different table.
Exercise
VPC A is peered with VPC B, and VPC B has a NAT gateway. Can instances in A use B's NAT gateway to reach the internet? Why or why not?
Show answer
No. Peering does not forward traffic to gateways in the peer VPC, and it is not transitive. A must have its own NAT gateway or internet gateway for outbound internet access.