Sub topic 2
Three ways to connect VPCs and when to use each
This chapter assumes you already know CIDR blocks, subnets, route tables, security groups and network ACLs. It also assumes you can run AWS CLI commands and know what a Network Load Balancer (NLB) does. You will compare VPC peering, AWS Transit Gateway and AWS PrivateLink, and learn the criteria for choosing among them. Later chapters configure each option in detail.
The question to ask first
Before you pick a service, decide what the two sides need to share.
- Network reachability. Any host in one VPC may need to open connections to any host in another, and often in both directions. Peering and Transit Gateway provide this.
- Access to one service. Clients in one VPC need to call a single application in another, and nothing else. PrivateLink provides this.
Peering and Transit Gateway join networks at the IP routing layer. PrivateLink does not join networks. It publishes one service, and consumers reach it through network interfaces in their own subnets.
Option 1: VPC peering
A VPC peering connection is a one-to-one link between two VPCs. The two VPCs can be in the same account or different accounts, and in the same Region or different Regions. Traffic stays on the AWS network.
You set it up in three steps: request the connection, accept it in the peer VPC, and add a route in each VPC that points the other VPC's CIDR at the peering connection.
aws ec2 create-vpc-peering-connection \
--vpc-id vpc-0aaa1111bbb22222c \
--peer-vpc-id vpc-0ddd3333eee44444f
aws ec2 accept-vpc-peering-connection \
--vpc-peering-connection-id pcx-0123456789abcdef0
aws ec2 create-route \
--route-table-id rtb-0123456789abcdef0 \
--destination-cidr-block 10.1.0.0/16 \
--vpc-peering-connection-id pcx-0123456789abcdef0Run the final create-route command again in the peer VPC's route table, with the first VPC's CIDR as the destination. Without that second route, return traffic has nowhere to go.
Peering has these properties:
- It has no hourly charge. You pay only for data transfer, and same-AZ traffic between peered VPCs in a Region is free.
- It has no separate gateway to size. Throughput is limited by the instances themselves, not by a shared device.
- It is non-transitive. If VPC A peers with B and B peers with C, A cannot reach C through B. Peering also cannot forward traffic from a peer to a VPN, a Direct Connect link or an internet gateway.
- It requires that the two VPCs have non-overlapping CIDR blocks.
Peering is a good choice for a small, stable number of VPCs that need full connectivity, or for two VPCs that move a lot of data and should avoid per-GB processing fees.
Option 2: AWS Transit Gateway
A transit gateway is a Regional virtual router. You attach VPCs, VPN connections, Direct Connect gateways and other transit gateways to it. Route tables inside the transit gateway decide which attachment can reach which. Each VPC needs only one attachment and one set of routes, no matter how many other networks join the hub.
Transit Gateway has these properties:
- It is transitive through the hub. Attached networks can reach each other if the transit gateway route tables allow it.
- It supports segmentation. Separate route tables let you keep production apart from development, or force traffic through an inspection VPC.
- It works with on-premises networks, so one hub can serve both VPCs and VPN or Direct Connect links.
- It costs money. You pay an hourly charge for each attachment and a per-GB charge for data processed.
- It still requires non-overlapping CIDRs for networks that must reach each other.
Option 3: AWS PrivateLink
With PrivateLink, a provider puts a service behind a Network Load Balancer and publishes it as an endpoint service. A consumer creates an interface VPC endpoint in its own VPC. The endpoint places elastic network interfaces (ENIs) in the consumer's subnets, and each ENI gets a private IP address from the consumer's range. Clients connect to those addresses.
PrivateLink has these properties:
- Traffic flows one way. Consumers open connections to the service. The provider cannot start connections into the consumer VPC through the endpoint.
- It exposes only the listener ports on the load balancer. The consumer never sees the provider's subnets, instances or CIDR.
- Overlapping CIDRs are not a problem. The consumer sees only addresses from its own range, so the two VPCs do not need distinct ranges.
- It is a good fit for SaaS-style sharing across many accounts. The provider controls who may connect, using allowed principals and optional manual acceptance.
- It costs an hourly charge for each endpoint in each AZ, plus a per-GB charge.
PrivateLink is for TCP services behind an NLB (or a Gateway Load Balancer for inspection appliances). It is not a general-purpose network path. Protocols that need arbitrary ports, server-initiated connections or full host-to-host reachability do not fit.
Side-by-side comparison
| Criterion | VPC peering | Transit Gateway | PrivateLink |
|---|---|---|---|
| What is connected | Two networks | Many networks | One service to many consumers |
| Direction of initiation | Either side | Either side | Consumer to provider only |
| Transitive routing | No | Yes, by route table | Not applicable |
| Overlapping CIDRs | Not allowed | Not allowed between communicating networks | Allowed |
| On-premises access | Not through the peer | Yes, VPN and Direct Connect | Reachable by on-premises clients through the endpoint's private IPs |
| Main cost | Data transfer | Per attachment per hour plus per GB | Per endpoint per AZ per hour plus per GB |
| Scaling effort | Grows with the number of pairs | One attachment per network | One endpoint per consumer VPC |
Why the mesh grows so fast
A full mesh of n VPCs needs n × (n − 1) / 2 peering connections, and each connection needs routes in both VPCs.
| VPCs | Peering connections |
|---|---|
| 5 | 10 |
| 10 | 45 |
| 20 | 190 |
| 50 | 1,225 |
Adding VPC number 21 means 20 new connections, 20 acceptances and 40 route table changes. With a transit gateway it means one attachment and a route in the new VPC. The next chapter looks at peering limits in more detail.
A decision flow
The diagram turns the criteria above into a sequence of questions.
Read it as a starting point. Real designs often mix options. A shared-services hub might use Transit Gateway for broad connectivity and PrivateLink for a few services that must cross overlapping address ranges.
Edge cases and common mistakes
- Choosing Transit Gateway for two VPCs. If two VPCs need to talk and nothing else will join them, a peering connection is cheaper and simpler.
- Expecting peering to transit. A VPC with a VPN to on-premises cannot share that VPN with its peers. Use Transit Gateway when many VPCs need the same on-premises link.
- Using PrivateLink as a network. An endpoint reaches one service. If clients need to reach several services on many ports, you create several endpoints or move to a routed design.
- Forgetting that routing is only half the job. Peering and Transit Gateway both need route entries in each VPC, and security groups and network ACLs must still allow the traffic. A connection that shows as active can still carry no packets.
- Planning CIDRs too late. Overlap is easy to avoid at design time and costly to fix afterwards. If you cannot renumber a VPC, PrivateLink is the usual workaround.
- Ignoring Region boundaries. A transit gateway belongs to one Region. Reaching another Region needs a transit gateway peering attachment, covered later in this tutorial.
Exercises
Exercise
A company has 12 VPCs in one Region, all with distinct CIDRs. Every VPC must reach every other VPC and a shared Direct Connect link. Which option fits best, and why does peering fail here?
Show answer
Transit Gateway fits best. A full peering mesh would need 12 × 11 / 2 = 66 connections with routes in both VPCs of each pair, and peering is non-transitive, so the VPCs could not share the Direct Connect link through a peer. A transit gateway needs 12 attachments and also accepts a Direct Connect gateway attachment.
Exercise
A SaaS vendor wants 300 customer accounts to call its API privately. Many customers use 10.0.0.0/16, and the vendor must not give them access to its internal network. Which option fits, and which two properties of it matter most?
Show answer
PrivateLink fits. Overlapping CIDRs are allowed because customers only see endpoint ENIs with addresses from their own range, and access is limited to the listener ports of the vendor's Network Load Balancer, so the vendor's network is not exposed.