AWS VPC: Route Tables, Subnet Association and NAT
1. Concepts
Route table
A route table is a set of rules that decides where traffic leaving a subnet goes. Each VPC has a main route table by default, and you can create your own.
Route
A route is one rule in a route table. It has two parts:
| Part | Meaning |
|---|---|
| Destination | The IP range the traffic is going to |
| Target | Where to send that traffic |
| Destination | Target | Meaning |
|---|---|---|
10.0.0.0/16 |
local |
Traffic inside the VPC (added automatically) |
0.0.0.0/0 |
Internet Gateway | All other IPv4 traffic goes to the internet |
0.0.0.0/0 |
NAT Gateway | All other IPv4 traffic goes out through NAT |
The most specific route wins. Traffic to 10.0.11.5 matches local (/16) rather than 0.0.0.0/0.
Subnet association
Association links a subnet to a route table, so the subnet follows that table’s routes.
- A subnet is associated with exactly one route table at a time.
- One route table can serve many subnets.
- A subnet with no explicit association uses the main route table.
A subnet is public if its route table has a route to an Internet Gateway. It is private if it has no such route. The subnet’s name does not make it either one.
Internet Gateway (IGW)
The IGW connects the VPC to the internet. An instance also needs a public IPv4 address (or Elastic IP) to use it directly.
NAT Gateway
NAT (Network Address Translation) lets private instances start connections to the internet, for example to install updates. The internet cannot start a connection back to them.
- This lab uses a zonal public NAT Gateway, which is created in a public subnet.
- It needs an Elastic IP.
- The private route table points
0.0.0.0/0to it. - It is charged per hour and per GB processed.
Outbound path:
Private EC2 -> private route table -> NAT Gateway -> public route table -> IGW -> Internet
2. Architecture diagram
Internet
|
Internet Gateway
|
+---------------------- VPC 10.0.0.0/16 ----------------------+
| | |
| Public route table: 0.0.0.0/0 -> IGW |
| | | | |
| public-1a public-1b public-1c |
| 10.0.1.0/24 10.0.2.0/24 10.0.3.0/24 |
| [Public EC2] |
| [NAT Gateway] |
| ^ |
| | |
| Private route table: 0.0.0.0/0 -> NAT |
| | | | |
| private-1a private-1b private-1c |
| 10.0.11.0/24 10.0.12.0/24 10.0.13.0/24 |
| [Private EC2] |
+-------------------------------------------------------------+
Tasks
- create vpc and 6 subnets (3+3)
- create IGW and attach vpc
- create 2 route table (public and pravite)
- create Nat gateway
- create rout in public routetable and attach IGW and assocate with all public subnets
- create rout in private routetable and attach NAT and assocate with all private subnets
- Create 2 ec2 instance (one instance with public subnet and another instance with private subnet)
