Sub topic 2
Three ways to connect VPCs and when to use each
This chapter assumes you know CIDR blocks, subnets, route tables, security groups, and how to run AWS CLI commands. It compares the three options for connecting VPCs and gives you criteria for choosing one.
The three options
- VPC peering is a one-to-one link between two VPCs. Traffic stays on the AWS network, and each side adds a route to the other's CIDR block.
- AWS Transit Gateway is a regional router. You attach many VPCs (and VPN or Direct Connect links) to it, and it forwards traffic between them using its own route tables.
- AWS PrivateLink exposes one service, not a whole network. A consumer VPC reaches a provider's service through an interface endpoint, a network interface with a private IP address in the consumer's subnet.
Peering and Transit Gateway connect networks. PrivateLink connects a client to a service.
How they differ
| Question | Peering | Transit Gateway | PrivateLink |
|---|---|---|---|
| What is reachable? | Whole VPC CIDR | Whole attached CIDRs, as routes allow | One service only |
| Direction | Both ways | Both ways | Consumer starts connections |
| Transitive routing | No | Yes | Not applicable |
| Overlapping CIDRs | Not allowed | Not allowed | Allowed |
| Scales to many VPCs | Poorly | Well | Well |
| Extra hourly charge | None | Per attachment | Per endpoint and per AZ |
Overlapping CIDRs work with PrivateLink because the consumer never routes to the provider's address space. It only talks to the endpoint's local IP address.
Choosing
Use these criteria:
- Expose one service to many consumers, possibly in other accounts or with overlapping CIDRs. Choose PrivateLink. The provider fronts the service with a Network Load Balancer.
- Connect two or three VPCs with the lowest cost and latency. Choose peering. It has no hourly fee and adds no extra hop.
- Connect many VPCs, share on-premises links, or segment traffic by environment. Choose Transit Gateway. Its route tables give you central control.
A worked example
A company has 12 VPCs that all need to reach each other, plus one billing API owned by another team.
- Full-mesh peering needs 12 × 11 / 2 = 66 connections, and each VPC needs 11 routes. Transit Gateway needs 12 attachments and one route per remote CIDR.
- The billing API should not give every VPC access to the billing team's network. Publishing it as a PrivateLink endpoint service lets each VPC reach only that API.
Exercise
A team wants 30 VPCs in one Region to route to each other, and one VPC hosts an internal API that only two other accounts should call, one of which uses the same CIDR range as the API's VPC. Which option do you use for each requirement, and why?
Show answer
Use Transit Gateway for the 30-VPC network, because full-mesh peering would need 435 connections and each VPC would need 29 routes. Use PrivateLink for the API, because it exposes only that service, lets you control which accounts may connect, and tolerates the overlapping CIDR range.