Sub topic 12
Connecting two VPCs with peering
This chapter assumes you know CIDR notation, route tables, and security groups from earlier chapters, and that you can run AWS CLI commands. It covers how to connect two VPCs with a peering connection and the errors that usually follow.
What peering is
A VPC peering connection is a private link between two VPCs. Instances in either VPC can reach each other by private IP address, as if they were on the same network. Traffic stays on the AWS network and does not cross the internet. The VPCs can be in the same account or in different accounts. They can also be in the same Region or in different Regions.
Peering has three rules that cause most mistakes:
- The two VPCs must have non-overlapping CIDR blocks. AWS rejects the request if they overlap.
- Peering is not transitive. If A peers with B and B peers with C, A cannot reach C through B.
- Peering does not move any traffic by itself. You must add routes on both sides.
Create and accept the connection
The VPC that starts the request is the requester. The other is the accepter. The peering connection stays in pending-acceptance until the accepter approves it. This example assumes two VPCs in the same account and Region, with their IDs in VPC_A and VPC_B.
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"For a different account, add --peer-owner-id. For a different Region, add --peer-region. The accepter then runs the accept command in its own account or Region.
Add routes on both sides
Each VPC needs a route that sends the other VPC's CIDR block to the peering connection. Here VPC A uses 10.0.0.0/16 and VPC B uses 10.1.0.0/16. Set RT_A and RT_B to the route tables of the subnets that need access.
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"Route only the subnets that need the link. The route is attached to a route table, so it applies to every subnet associated with that table.
Allow the traffic
A route only provides a path. Security groups and network ACLs still decide what gets through. In a same-Region peering you can reference a security group from the peer VPC as a source. In the database tier, for example, you could allow TCP 5432 from the app tier's security group in the other VPC. For cross-Region peering, use CIDR ranges instead.
Common errors
| Symptom | Likely cause |
|---|---|
| `InvalidParameterValue` when creating the connection | The two CIDR blocks overlap |
| Connection stays in `pending-acceptance` | The accepter has not run the accept command |
| Ping or connections time out | A route is missing on one side, or a security group or network ACL blocks the traffic |
| A can reach B but not C behind B | Peering is not transitive. Peer A with C directly, or use Transit Gateway |
When peering stops scaling
Peering is simple and has no hourly charge. Data transfer is billed, and traffic between Availability Zones or Regions costs more than traffic within one AZ. Because it is not transitive, connecting N VPCs in a full mesh needs N(N-1)/2 connections. Ten VPCs need 45 connections, and each one needs routes on both sides. At that size, the next chapter's Transit Gateway is easier to manage.
Exercise
VPC A (10.0.0.0/16) is peered with VPC B (10.1.0.0/16), and VPC B is peered with VPC C (10.2.0.0/16). An instance in A cannot reach an instance in C, even though all routes to B work. Why, and what are two ways to fix it?
Show answer
Peering is not transitive, so traffic from A cannot pass through B to reach C. You can create a direct peering connection between A and C, and add routes on both sides. Alternatively, attach all three VPCs to a Transit Gateway.