Sub topic 13
Scaling VPC connectivity with Transit Gateway
This chapter assumes you know how route tables work and how VPC peering connects two VPCs. It also assumes the AWS CLI variables from earlier chapters (VPC_ID, PRIVATE_SUBNET_ID, PRIVATE_RT_ID) are still set.
Why peering stops scaling
Peering is one-to-one and non-transitive. If VPC A peers with B, and B peers with C, A still cannot reach C. Connecting N VPCs in a full mesh takes N(N-1)/2 peerings, and each one needs routes in both VPCs. At 10 VPCs that is 45 peerings.
An AWS Transit Gateway (TGW) is a regional router that you manage. Each VPC connects to it once, and the TGW forwards traffic between all attached networks. That gives you a hub-and-spoke layout with N attachments instead of a mesh.
How it works
A TGW has four main parts:
- Attachment: a connection from a VPC, VPN, Direct Connect gateway, or another TGW to the hub.
- Transit gateway route table: decides where traffic arriving from an attachment goes next.
- Association: each attachment is associated with exactly one TGW route table, which is used to route traffic coming in from that attachment.
- Propagation: an attachment can advertise its CIDR into one or more TGW route tables, so routes appear without manual entry.
By default, a new TGW has one default route table. All attachments associate with it and propagate into it, so every attached network can reach every other. To isolate networks, for example production from development, disable the defaults and create separate TGW route tables.
Build a hub with one VPC attached
TGW_ID=$(aws ec2 create-transit-gateway \
--description "Regional hub" \
--query TransitGateway.TransitGatewayId --output text)
until [ "$(aws ec2 describe-transit-gateways --transit-gateway-ids "$TGW_ID" \
--query 'TransitGateways[0].State' --output text)" = "available" ]; do
sleep 10
done
aws ec2 create-transit-gateway-vpc-attachment \
--transit-gateway-id "$TGW_ID" \
--vpc-id "$VPC_ID" \
--subnet-ids "$PRIVATE_SUBNET_ID"
aws ec2 create-route \
--route-table-id "$PRIVATE_RT_ID" \
--destination-cidr-block 10.1.0.0/16 \
--transit-gateway-id "$TGW_ID"The attachment places an elastic network interface in each subnet you list. Choose one subnet per Availability Zone you use. If a zone has no attachment subnet, traffic from that zone's resources cannot reach the TGW.
The last command is the step people forget. The TGW's route tables only cover traffic after it enters the TGW. Each VPC route table still needs a route that sends the remote CIDR (here 10.1.0.0/16, a second VPC) to the TGW.
Common errors
- Overlapping CIDRs. The TGW cannot route between networks that use the same range. Fix this in your address plan before attaching.
- Missing VPC routes. The attachment exists and propagation is on, but traffic still fails because the VPC route table has no route to the TGW.
- Security rules. Security groups and network ACLs still apply. A security group can reference a CIDR across the TGW, but it cannot reference a security group in another VPC through a TGW.
- Cost surprises. You pay per attachment-hour and per GB processed, so a TGW costs more than peering for a simple two-VPC link.
Choosing between peering and TGW
Use peering for a few VPCs with heavy traffic, since it has no per-GB processing fee. Use a TGW when you have many VPCs, need transitive routing, want central route control, or need one attachment point for VPN or Direct Connect.
Exercise
Three VPCs are attached to one Transit Gateway with default association and propagation. Instances in VPC A cannot reach VPC B. Name two places you would check first.
Show answer
Check that VPC A's route table has a route for VPC B's CIDR pointing to the TGW. Then check that VPC B's attachment propagates its CIDR into the TGW route table that VPC A's attachment uses. Security groups and network ACLs on both sides are the next place to look.