Sub topic 4
Controlling traffic paths with route tables
This chapter assumes you know CIDR notation, can read the VPC and subnet commands from the earlier chapters, and can run AWS CLI commands. It covers how route tables decide where traffic goes, how subnets are tied to them, and the mistakes that break connectivity.
What a route table does
A route table is a list of rules called routes. Each route has a destination (a CIDR block) and a target (where to send matching packets). When an instance sends a packet, the VPC router looks at the destination IP address and picks a route from the table associated with the instance's subnet.
Route tables only decide the next hop. They do not filter traffic. Security groups and network ACLs do that, and they are covered in later chapters. A route to the internet gateway does not make an instance reachable. It only makes a path available.
Every route table contains a local route for each CIDR block of the VPC. The local route has the target local. It lets all subnets in the VPC reach each other, and you cannot delete or change it. If you add a secondary CIDR block to the VPC, AWS adds a local route for it automatically.
How the router picks a route
The VPC router uses longest prefix match. If several routes match a destination, the most specific one (the largest prefix length) wins. The order of routes in the table does not matter.
Consider this table:
| Destination | Target |
|---|---|
| 10.0.0.0/16 | local |
| 0.0.0.0/0 | nat-0abc123 |
| 10.1.0.0/16 | pcx-0def456 |
| 10.1.5.0/24 | vgw-0aaa789 |
- A packet to
10.0.3.9matches10.0.0.0/16and0.0.0.0/0. The/16is more specific, so it stays local. - A packet to
10.1.7.4matches10.1.0.0/16and0.0.0.0/0. It goes to the peering connection. - A packet to
10.1.5.20matches three routes. The/24wins, so it goes to the virtual private gateway. - A packet to
93.184.216.34matches only the default route, so it goes to the NAT gateway.
The local route has a special property. Even if you add a more specific route that falls inside the VPC CIDR, such as 10.0.1.0/24 pointing at a network appliance, traffic still follows your more specific route. This is how you steer in-VPC traffic through an inspection appliance. Without such a route, local always applies to VPC addresses.
If two routes have the same destination, AWS has a tiebreak order for routes that come from different sources. A static route wins over a propagated route, and propagated routes are ranked by type. You can avoid most of these cases by not creating overlapping destinations.
Main and custom route tables
Every VPC has a main route table created with it. Any subnet that you do not explicitly associate with a route table uses the main one. You can create custom route tables and associate them with subnets one by one.
This gives a safe pattern:
- Leave the main route table with only the local route.
- Create a custom route table for each kind of subnet (public, private, and so on).
- Associate each subnet explicitly.
This matters because a new subnet that you forget to associate falls back to the main table. If you had put an internet gateway route in the main table, that new subnet would become public by accident. Keeping the main table minimal means a forgotten association gives you an isolated subnet instead of an exposed one.
A subnet is associated with exactly one route table at a time. One route table can serve many subnets.
Route targets
The target field accepts several kinds of resources:
| Target | Typical use |
|---|---|
| `local` | Traffic inside the VPC |
| Internet gateway (`igw-`) | Public internet access for public subnets |
| NAT gateway (`nat-`) | Outbound-only internet access for private subnets |
| Gateway endpoint (`vpce-`) | Private access to S3 and DynamoDB |
| Peering connection (`pcx-`) | Traffic to a peered VPC |
| Transit gateway (`tgw-`) | Hub-style connections to many VPCs and networks |
| Virtual private gateway (`vgw-`) | Site-to-Site VPN or Direct Connect |
| Network interface (`eni-`) | A firewall or router appliance |
Later chapters cover each target in detail. For now, the point is that the same mechanism, a destination plus a target, drives all of them.
Creating and associating a route table
The commands below assume the variables from the earlier chapters (VPC_ID, PUBLIC_SUBNET_ID, IGW_ID) are still set in your shell.
PUBLIC_RT_ID=$(aws ec2 create-route-table --vpc-id "$VPC_ID" --query RouteTable.RouteTableId --output text)
aws ec2 create-route --route-table-id "$PUBLIC_RT_ID" --destination-cidr-block 0.0.0.0/0 --gateway-id "$IGW_ID"
ASSOC_ID=$(aws ec2 associate-route-table --route-table-id "$PUBLIC_RT_ID" --subnet-id "$PUBLIC_SUBNET_ID" --query AssociationId --output text)
echo "Association: $ASSOC_ID"The associate-route-table call returns an association ID. You need it later to move a subnet to a different table, because the command that does that is replace-route-table-association, which takes the association ID and the new table:
aws ec2 replace-route-table-association --association-id "$ASSOC_ID" --route-table-id "$OTHER_RT_ID"To inspect what you built, describe the table and read the routes:
aws ec2 describe-route-tables \
--route-table-ids "$PUBLIC_RT_ID" \
--query "RouteTables[0].Routes[].[DestinationCidrBlock,GatewayId,State]" \
--output tableYou should see the local route for 10.0.0.0/16 and the default route to the internet gateway, both with state active.
Route state and blackholes
Each route has a state. It is active when the target works. It is blackhole when the target no longer exists or is unusable. Examples are a deleted NAT gateway, a deleted peering connection, or a terminated appliance instance.
Traffic that matches a blackhole route is dropped. It does not fall through to a less specific route such as 0.0.0.0/0. This surprises people: deleting a NAT gateway leaves the 0.0.0.0/0 route in place as a blackhole, and the private subnet loses its internet access until you replace or delete the route.
You can find blackholes across a region with a query:
aws ec2 describe-route-tables \
--query "RouteTables[].Routes[?State=='blackhole'].[DestinationCidrBlock]" \
--output textTo fix one, use replace-route to point the destination at a new target:
aws ec2 replace-route --route-table-id "$PRIVATE_RT_ID" --destination-cidr-block 0.0.0.0/0 --nat-gateway-id "$NEW_NAT_ID"Route propagation
A route table can receive routes automatically from a virtual private gateway or from a transit gateway attachment. This is called route propagation. Propagated routes carry the on-premises prefixes learned over a VPN or Direct Connect, so you do not have to type them by hand. You enable it per route table:
aws ec2 enable-vgw-route-propagation --route-table-id "$PRIVATE_RT_ID" --gateway-id "$VGW_ID"Propagated routes appear in describe-route-tables with an origin of EnableVgwRoutePropagation. If a propagated route and a static route have the same destination, the static route wins.
Reading the topology
The diagram shows two subnets with different tables. The public table sends the default route to the internet gateway. The private table sends it to the NAT gateway. Both tables share the same local route.
Common errors
- A subnet is "public" but has no internet access. Check three things in order: the subnet's associated table has a
0.0.0.0/0route to an internet gateway, the instance has a public or Elastic IP, and the security group and network ACL allow the traffic. - Peering is set up but pings fail. A peering connection needs a route in both VPCs' tables, each pointing the other VPC's CIDR at the
pcx-target. One side is not enough. - Return traffic is missing. Routes are not symmetric by themselves. The reply packet is routed by the destination's table, so check both directions.
- The wrong table is in effect. Run
describe-route-tables --filters Name=association.subnet-id,Values=$SUBNET_ID. If this returns nothing, the subnet uses the main table. - A route is rejected as overlapping. You cannot create a peering or transit route for a CIDR that conflicts with an existing destination. This is the reason the CIDR planning chapter asks for non-overlapping ranges.
- Quotas. A route table holds 50 non-propagated routes by default, and the quota can be raised. Summarize prefixes where you can.
Exercise
A private subnet's route table has 10.0.0.0/16 → local and 0.0.0.0/0 → nat-0abc. Someone deletes the NAT gateway. An instance in the subnet then tries to reach 10.0.5.10 (in the VPC) and 8.8.8.8. What happens to each, and why?
Show answer
The traffic to 10.0.5.10 still works. It matches the local route, which is more specific than 0.0.0.0/0 and does not depend on the NAT gateway. The traffic to 8.8.8.8 is dropped. It matches only the default route, which is now a blackhole because its target no longer exists. Blackholed traffic does not fall through to another route. The fix is to create a new NAT gateway and run replace-route to point 0.0.0.0/0 at it.
Exercise
You create a new subnet and forget to associate a route table. Your main route table contains a default route to an internet gateway. What is the risk, and how do you avoid it?
Show answer
The subnet automatically uses the main route table, so it gets the internet gateway route and becomes public without anyone deciding that. Instances there with public IPs become reachable if their security groups allow it. To avoid this, keep only the local route in the main table and give every subnet an explicit association with a custom table.