Sub topic 13
Scaling VPC connectivity with Transit Gateway
This chapter assumes you know CIDR notation, route tables, and how VPC peering works from the previous chapter. You should be comfortable running AWS CLI commands. By the end you can explain what a Transit Gateway (TGW) does, build one that connects two VPCs, and avoid the usual routing mistakes.
Why peering stops scaling
VPC peering is a one-to-one link between two VPCs. It is not transitive: if VPC A peers with B and B peers with C, A cannot reach C through B. To connect every VPC to every other VPC you need a full mesh.
The number of peering connections for n VPCs is n × (n − 1) / 2. Each connection also needs route entries in both VPCs.
| VPCs | Peering connections needed |
|---|---|
| 3 | 3 |
| 5 | 10 |
| 10 | 45 |
| 20 | 190 |
A Transit Gateway replaces the mesh with a hub-and-spoke design. Each VPC attaches once to the TGW. The TGW is a regional virtual router that forwards traffic between all of its attachments. The number of connections grows linearly: 20 VPCs need 20 attachments.
The working model
A Transit Gateway has four parts you need to keep apart.
- Transit Gateway: the regional router. It has its own Amazon-side ASN (a BGP identifier) and default settings.
- Attachment: a connection from a resource to the TGW. Resources can be VPCs, Site-to-Site VPN connections, Direct Connect gateways, peered Transit Gateways, or Connect attachments.
- TGW route table: a table of destination CIDRs and the attachment that should receive that traffic. One TGW can have many route tables.
- Association and propagation: each attachment is associated with exactly one TGW route table, which decides how traffic from that attachment is routed. An attachment can also propagate its CIDRs into one or more route tables, which adds routes automatically.
Two routing layers must agree. The TGW route table decides where the TGW sends a packet. The VPC route table decides whether a packet leaves the VPC toward the TGW at all. Forgetting the VPC side is the most common reason a new TGW "does not work".
How a packet travels
Suppose an instance in VPC A (10.0.0.0/16) calls an instance in VPC B (10.1.0.0/16).
- The VPC A subnet route table has a route for 10.1.0.0/16 whose target is the TGW. The packet goes to the TGW.
- The TGW looks at the route table associated with the VPC A attachment. It finds 10.1.0.0/16 pointing at the VPC B attachment.
- The TGW places the packet into VPC B through an elastic network interface in the attached subnet.
- Security groups and network ACLs in VPC B evaluate the traffic as usual.
Building a two-VPC hub
This example assumes VPC_A and VPC_B hold VPC IDs, and SUBNET_A and SUBNET_B hold one subnet ID from each VPC. The CIDRs must not overlap. It uses the default TGW route table, where every attachment is both associated and propagating.
TGW_ID=$(aws ec2 create-transit-gateway \
--description "core hub" \
--options DefaultRouteTableAssociation=enable,DefaultRouteTablePropagation=enable \
--query TransitGateway.TransitGatewayId --output text)
aws ec2 wait transit-gateway-available --transit-gateway-ids "$TGW_ID"
ATT_A=$(aws ec2 create-transit-gateway-vpc-attachment \
--transit-gateway-id "$TGW_ID" --vpc-id "$VPC_A" --subnet-ids "$SUBNET_A" \
--query TransitGatewayVpcAttachment.TransitGatewayAttachmentId --output text)
ATT_B=$(aws ec2 create-transit-gateway-vpc-attachment \
--transit-gateway-id "$TGW_ID" --vpc-id "$VPC_B" --subnet-ids "$SUBNET_B" \
--query TransitGatewayVpcAttachment.TransitGatewayAttachmentId --output text)
aws ec2 wait transit-gateway-attachment-available \
--transit-gateway-attachment-ids "$ATT_A" "$ATT_B"The TGW now knows both VPCs. Next, each VPC needs a route that sends the other VPC's range to the TGW. Use the route table of the subnets that should reach the other VPC.
aws ec2 create-route --route-table-id "$RT_A" \
--destination-cidr-block 10.1.0.0/16 --transit-gateway-id "$TGW_ID"
aws ec2 create-route --route-table-id "$RT_B" \
--destination-cidr-block 10.0.0.0/16 --transit-gateway-id "$TGW_ID"Here RT_A and RT_B are the VPC route table IDs, and the CIDRs match the example VPCs above. To confirm that propagation worked, list the routes in the TGW route table.
TGW_RT=$(aws ec2 describe-transit-gateways --transit-gateway-ids "$TGW_ID" \
--query "TransitGateways[0].Options.AssociationDefaultRouteTableId" --output text)
aws ec2 search-transit-gateway-routes --transit-gateway-route-table-id "$TGW_RT" \
--filters "Name=type,Values=propagated" \
--query "Routes[].[DestinationCidrBlock,State]" --output tableYou should see both VPC CIDRs in the active state.
Segmenting traffic with multiple route tables
The default route table lets every attachment reach every other attachment. That is convenient but often too open. Separate route tables let you build isolated groups.
A common pattern uses one route table for production VPCs, one for development VPCs, and one for shared services.
- Create a TGW with default association and propagation disabled.
- Create the route tables, for example
prod,dev, andshared. - Associate each attachment with the table that describes who it may talk to.
- Propagate each attachment's CIDR only into the tables of the groups that should reach it.
For instance, associate production VPCs with the prod table. Propagate the shared-services VPC into prod and dev, but do not propagate dev VPCs into prod. Prod then cannot route to dev, because no route exists.
You can also add static routes. A static route wins over a propagated route for the same prefix. A blackhole route drops matching traffic, which is useful for explicitly blocking a range.
Gotchas and common errors
- Missing VPC route. The TGW has the route, but the VPC route table has no entry pointing at the TGW. Packets never leave the VPC.
- Overlapping CIDRs. The TGW cannot route to two attachments with the same prefix. Overlapping propagated routes conflict, so plan address space ahead of time.
- One subnet per Availability Zone. A VPC attachment places a network interface in each subnet you choose. Traffic can only reach the VPC through an AZ where you chose a subnet. Choose a subnet in every AZ that has workloads, or traffic to those AZs may be dropped or take an extra cross-AZ hop.
- Security groups do not span VPCs through the TGW. You cannot reference a security group from another VPC in a rule across a TGW attachment. Use CIDR ranges in the rules instead. (Peering within a Region can reference peer security groups, but a TGW cannot.)
- Appliance mode. For stateful inspection appliances in a VPC, enable appliance mode on that attachment. Otherwise return traffic can use a different AZ than the request, and the appliance sees only half of the flow.
- Cost. You pay per attachment per hour and per gigabyte processed by the TGW. For two VPCs with heavy traffic, a peering connection is cheaper because peering has no hourly fee, though cross-AZ data transfer charges still apply.
- Limits. Quotas apply to attachments, route tables, and routes per table. Check current Service Quotas before you plan a large design.
TGW or peering?
| Need | Better choice |
|---|---|
| Two or three VPCs, high throughput, lowest cost | Peering |
| Many VPCs that must reach each other | Transit Gateway |
| Central egress, inspection, or shared services | Transit Gateway |
| Route-table-based segmentation between groups | Transit Gateway |
| Connect VPCs and on-premises through one hub | Transit Gateway |
Connecting regions and on-premises
A TGW is regional. To join Regions, create a TGW in each Region and a peering attachment between them. Peering attachments use static routes only, so you add each remote CIDR yourself.
Site-to-Site VPN and Direct Connect attach to the same TGW. This lets on-premises networks reach every attached VPC through one connection. The next chapter covers hybrid connectivity in detail.
Cleaning up
Delete attachments before the TGW. Deletion of an attachment can take a few minutes, and the TGW cannot be deleted while attachments exist. Remove the VPC routes that point at the TGW too, because they become blackholes once the TGW is gone.
Exercise
You attach VPC A (10.0.0.0/16) and VPC B (10.1.0.0/16) to a Transit Gateway that uses the default route table. Both attachments show "available", and the TGW route table lists both CIDRs as active. An instance in A still cannot reach an instance in B. Name the most likely missing configuration and the exact fix for VPC A.
Show answer
The VPC route tables have no routes pointing at the TGW. In VPC A, add a route in the subnet's route table with destination 10.1.0.0/16 and target the Transit Gateway (aws ec2 create-route --route-table-id RTA --destination-cidr-block 10.1.0.0/16 --transit-gateway-id TGWID). VPC B needs the mirror route for 10.0.0.0/16. Security groups and network ACLs should also be checked afterward.
Exercise
You want dev VPCs to reach a shared-services VPC but not production VPCs. Which association and propagation settings achieve this with separate route tables?
Show answer
Associate dev attachments with a dev route table and production attachments with a prod table. Propagate the shared-services attachment into both dev and prod. Do not propagate dev attachments into prod, or production attachments into dev. To let shared services reply to both groups, associate it with a table that has routes to both, or propagate the dev and prod attachments into the shared-services table. The dev table then has no route to production CIDRs, so that traffic is dropped.