Sub topic 4
Controlling traffic paths with route tables
This chapter assumes you know CIDR notation, can run AWS CLI commands, and have a VPC with at least one subnet, as built in the earlier chapters. A route table decides where a subnet's outbound traffic goes. It does not decide whether the traffic is allowed. Security groups and network ACLs make that decision.
The working model
A route table is a list of rules. Each rule has a destination CIDR block and a target. When a packet leaves a resource in a subnet, AWS looks at the subnet's route table and picks the rule with the longest prefix match. That is the most specific CIDR that contains the destination address.
Every route table in a VPC gets a local route automatically. It covers the VPC's CIDR block, so all subnets can reach each other by default. You can't delete the local route.
| Destination | Target | Meaning |
|---|---|---|
| 10.0.0.0/16 | local | Traffic inside the VPC |
| 0.0.0.0/0 | igw-… | Everything else goes to the internet gateway |
| 10.1.0.0/16 | pcx-… | A peered VPC (a more specific route than 0.0.0.0/0) |
Packets to 10.1.4.7 match both 10.1.0.0/16 and 0.0.0.0/0. The /16 wins because it is more specific.
Main and custom route tables
Each VPC has a main route table. Any subnet without an explicit association uses it. Two habits keep you safe:
- Leave the main route table with only the local route.
- Create custom route tables for each role, such as public and private, and associate them explicitly.
If someone adds an internet route to the main table, every subnet without its own association becomes public. A subnet can be associated with only one route table. One route table can serve many subnets.
Creating a route and associating it
This is the pattern from the public subnet setup. It assumes VPC_ID, IGW_ID, and PUBLIC_SUBNET_ID are already set.
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"
aws ec2 associate-route-table --route-table-id "$PUBLIC_RT_ID" --subnet-id "$PUBLIC_SUBNET_ID"To check the result, list the table:
aws ec2 describe-route-tables --route-table-ids "$PUBLIC_RT_ID" --query "RouteTables[0].Routes"Each route has a State. A route shows blackhole when its target no longer exists, for example after you delete a NAT gateway or a peering connection. Traffic matching a blackhole route is dropped.
Common targets
- local: traffic within the VPC.
- Internet gateway: public internet access.
- NAT gateway: outbound-only internet access for private subnets.
- VPC peering connection or transit gateway: other VPCs and networks.
- Gateway endpoint: S3 or DynamoDB. AWS adds the route for you when you name the route table.
Common errors
- Route exists but the subnet is still private. The route table isn't associated with that subnet, so the main table is in use. Check the association.
- No return path. Peering needs a route in both VPCs. Adding one side only gives one-way traffic that never completes.
- Overlapping CIDRs. Two routes with the same destination are not allowed in one table. Peered VPCs with overlapping ranges can't be routed correctly.
- Blackhole routes. Replace or delete routes whose targets were removed.
Exercise
A subnet's route table has 10.0.0.0/16 to local, 0.0.0.0/0 to a NAT gateway, and 10.0.5.0/24 to a peering connection. Where does a packet to 10.0.5.20 go, and why?
Show answer
It goes to the peering connection. The destination matches 10.0.0.0/16, 10.0.5.0/24, and 0.0.0.0/0, and AWS picks the longest prefix, which is the /24 route. Note that AWS does not allow a route to be more specific than the local route unless it is a supported exception, so in practice this exact table would be rejected for a peering route inside the VPC's own CIDR.
Exercise
You added a 0.0.0.0/0 route to an internet gateway in a new route table, but instances in the subnet still cannot reach the internet. Name two things to check in the route configuration.
Show answer
Check that the route table is associated with the subnet, because otherwise the subnet uses the main route table. Also check that the route's state is active and the internet gateway is attached to the VPC.