AWSBeginner
Private subnet can't reach the Internet: route tables, NAT Gateway and VPC endpoints
Key results
- If the private subnet route table has no 0.0.0.0/0 route, the host cannot reach the Internet, even with a NAT gateway in place.
- Real tcpdump: the same packet has its source rewritten twice, 10.0.2.10 → 10.0.1.10 (NAT gateway) → 203.0.113.10 (Elastic IP).
- Downloading 100 MB from S3: through the NAT, the NAT processes 100,664,409 bytes; through a gateway endpoint, it processes 0 bytes.
- Real Singapore prices: 500 GB/month to S3 costs $29.50 in NAT processing charges, versus $0 through a gateway endpoint.
What you need
- No prior VPC knowledge: the guide explains it from scratch.
- A Linux machine with Docker and containerlab (about 2 GB of free RAM); step 7 also needs Terraform and AWS CLI v2. Setup is in the "Lab setup" guide.
- The network part runs as 6 Linux containers playing the VPC; the AWS part runs on Moto (a mock of the AWS APIs, no real packets). No AWS account needed.
- Time
- 45 min
- Cost
- $0, runs on your own machine (a real NAT gateway in Singapore: $0.059/hour + $0.059/GB)
- Amazon VPC
- NAT gateway
- Internet gateway
- VPC endpoint
- Amazon S3
- Terraform
- containerlab
Watch the lab video

🎬 Goes live 12 Dec 2026, 20:00 (UTC+7)
An application server in a private subnet needs to call an external API, download patches or push backups to S3, but curl just hangs. The cause is almost always in one of three places: the route table, the NAT gateway, or where the NAT gateway was placed. This guide rebuilds the VPC model with Linux containers, breaks each piece on purpose so you see the real symptoms, then uses a VPC endpoint so traffic to S3 skips the NAT and its processing charge.
You do not need to know VPC networking beforehand. Each concept is explained when it shows up, and every config line comes with what it does and why it is set that way.
A few concepts first
- VPC
- Your own private network on AWS, with its own IP range. Servers (EC2) live inside a VPC.Example: The VPC in this guide uses 10.0.0.0/16.
- Subnet
- A slice of the VPC inside one Availability Zone. Each subnet is associated with exactly one route table.Example: Public subnet 10.0.1.0/24 and private subnet 10.0.2.0/24.
- Route table
- A list of "to reach this destination, hand the packet to that target". Where a packet goes is decided by the table of the subnet the host sits in.Example: 0.0.0.0/0 → nat-gateway means anything without a more specific route goes to the NAT gateway.
- Internet gateway (IGW)
- The VPC's door to the Internet. It maps each private IP to its matching public IP (one-to-one), so only hosts with a public IP or Elastic IP can use it.Example: A subnet with a 0.0.0.0/0 → IGW route is called a public subnet.
- NAT gateway
- A service that lets many hosts with only private IPs share one address to go out. Nobody outside can start a connection in.Example: The app host 10.0.2.10 reaches the Internet as the NAT gateway's Elastic IP 203.0.113.10.
- Gateway endpoint
- A private path from the VPC to S3 or DynamoDB, declared as one route in a route table. No NAT involved, no extra AWS charge.Example: The route pl-xxxx (S3's IP ranges) → vpce-xxxx in the private subnet's route table.
The difference between a public and a private subnet lives only in the route table, not in a name or a checkbox:
The lab model and how it runs
This section explains the lab before you type a single command: which machines exist, how they are wired, which way a packet travels, what each file does, and the order of the whole run. Once you have read it, every command in the steps below maps to a box on the diagram.
The AWS design the lab reproduces
On AWS, the VPC router is invisible: it sits at the .1 address of every subnet and reads the route tables to forward packets. You cannot log in to it; you only edit route tables through the console or the API. The lab turns it into its own container (l03-nat-vpcrtr) so you can inspect and edit each table.
The real lab diagram
Every box below is a container really running on the lab machine, and every line is a real virtual cable. The label on a line gives the port at each end (for example eth2 ↔ eth1 means port eth2 of this node is cabled to port eth1 of the other node), the IP range of that cable, and the last octet of the IP at each end.
Each component
| Component | What it is | Role on real AWS | Created by / file |
|---|---|---|---|
l03-nat-app |
nicolaka/netshoot container, IP 10.0.2.10/24 on eth1 |
EC2 instance in the private subnet, no public IP | topo.clab.yml (node app) + cauhinh/app.sh |
l03-nat-vpcrtr |
Container with 4 ports eth1–eth4, packet forwarding on, 2 routing tables (100 and 200) and 2 rules that pick a table |
Hidden VPC router + route tables rt-public (table 100) and rt-private (table 200) + subnet associations |
topo.clab.yml (node vpcrtr) + cauhinh/vpcrtr.sh |
l03-nat-natgw |
Container with IP 10.0.1.10/24, forwarding on, an iptables rule that counts bytes; NAT (MASQUERADE) is switched on in step 4 |
Public NAT gateway, placed in the public subnet | topo.clab.yml (node natgw) + cauhinh/natgw.sh |
l03-nat-igw |
Container joining the VPC to the “internet”; holds the Elastic IP 203.0.113.10; one-to-one SNAT/DNAT; drops packets from private IPs that have no Elastic IP |
Internet gateway + the NAT gateway’s Elastic IP | topo.clab.yml (node igw) + cauhinh/igw.sh |
l03-nat-web |
Container with IP 203.0.113.80/24 running python3 -m http.server on port 80, logging the caller IP to /tmp/web.log |
Any server on the internet (an API, a patch repository) | topo.clab.yml (node web) + cauhinh/web.sh |
l03-nat-s3 |
Container with IP 198.51.100.20/24 (internet side) and 10.255.1.2/30 (endpoint side), serving the 100 MB file backup-100mb.bin, log in /tmp/s3.log |
Amazon S3 in the same Region | topo.clab.yml (node s3) + cauhinh/s3.sh |
Network l03-mgmt |
Docker bridge network, range 172.20.103.0/24, joins eth0 of all 6 containers |
Nothing on AWS: only lets containerlab manage the containers | topo.clab.yml (mgmt block) |
| 6 veth pairs | Virtual cables between two containers | The networks inside the VPC and the paths out | topo.clab.yml (links block) |
| Terraform + Moto | main.tf calls the EC2 API against Moto (an AWS API emulator) at localhost:4566 |
The same API calls you would make on real AWS: create VPC, subnets, IGW, NAT, route tables, endpoint | terraform/main.tf |
How the components connect
There are 6 cables between containers, plus a shared management network. Each cable is its own IP range, and each end has an IP in that range:
| # | Cable (veth pair) | End A: container, port, IP | End B: container, port, IP | IP range | On AWS this is |
|---|---|---|---|---|---|
| 1 | app:eth1 ↔ vpcrtr:eth1 |
l03-nat-app eth1 10.0.2.10 |
l03-nat-vpcrtr eth1 10.0.2.1 |
10.0.2.0/24 |
Private subnet |
| 2 | vpcrtr:eth2 ↔ natgw:eth1 |
l03-nat-vpcrtr eth2 10.0.1.1 |
l03-nat-natgw eth1 10.0.1.10 |
10.0.1.0/24 |
Public subnet |
| 3 | vpcrtr:eth3 ↔ igw:eth1 |
l03-nat-vpcrtr eth3 10.255.0.1 |
l03-nat-igw eth1 10.255.0.2 |
10.255.0.0/30 |
Where the IGW attaches to the VPC (no IP on AWS; the lab needs a small range to wire it) |
| 4 | igw:eth2 ↔ web:eth1 |
l03-nat-igw eth2 203.0.113.1 and 203.0.113.10 (Elastic IP) |
l03-nat-web eth1 203.0.113.80 |
203.0.113.0/24 |
The internet |
| 5 | igw:eth3 ↔ s3:eth1 |
l03-nat-igw eth3 198.51.100.1 |
l03-nat-s3 eth1 198.51.100.20 |
198.51.100.0/24 |
Path to S3’s public address over the internet |
| 6 | vpcrtr:eth4 ↔ s3:eth2 |
l03-nat-vpcrtr eth4 10.255.1.1 |
l03-nat-s3 eth2 10.255.1.2 |
10.255.1.0/30 |
Private path inside the AWS network from the VPC to S3 via the gateway endpoint |
| - | eth0 of every container |
6 containers | bridge l03-mgmt |
172.20.103.0/24 |
Nothing, management only. The default route via eth0 is removed |
203.0.113.0/24 and 198.51.100.0/24 are reserved for documentation (RFC 5737) and never clash with a real network. The 10.x ranges are private IPs.
Where each container sends a packet whose destination is not on one of its own cables, its default gateway:
| Container | Default gateway | Other routes | Why |
|---|---|---|---|
app |
via 10.0.2.1 (VPC router) |
none | A real EC2 instance also sends everything outside its subnet to the router at .1. |
vpcrtr |
none in the main table | Tables 100 and 200 (the route tables), chosen by the port the packet came in on | The router uses the route table of the subnet the packet came from, as on AWS. |
natgw |
via 10.0.1.1 (VPC router) |
none | The NAT gateway hands translated packets to the router, which uses the table of the subnet holding the NAT. |
igw |
none | 10.0.0.0/16 via 10.255.0.1 (back to the VPC); 203.0.113.0/24, 198.51.100.0/24 directly connected |
The IGW only needs the way back to the VPC and the way out to the internet. |
web |
none | only 203.0.113.0/24, directly connected |
A host on the internet sees only the Elastic IP 203.0.113.10 and knows nothing about 10.x. |
s3 |
none | 203.0.113.0/24 via 198.51.100.1; 10.0.0.0/16 via 10.255.1.1 |
Answers internet clients through igw, and VPC hosts through the endpoint path. |
Once the lab is fixed (step 4), one packet from app to the web server travels like this:
apprunscurl http://203.0.113.80/. The destination is not in10.0.2.0/24, soappsends the packet to its default gateway10.0.2.1, which is porteth1ofvpcrtr. The source of the packet is10.0.2.10.vpcrtrsees the packet arrive oneth1(private subnet), so theip rulepicks table 200 (rt-private). The line0.0.0.0/0 via 10.0.1.10matches, and the packet leaves oneth2towardsnatgw.natgwreceives the packet oneth1. The MASQUERADE rule rewrites the source10.0.2.10to10.0.1.10and remembers the pair. The packet is sent to the default gateway10.0.1.1, back tovpcrtron the same cable.vpcrtrsees the packet arrive oneth2(public subnet), so the rule picks table 100 (rt-public). The linedefault via 10.255.0.2matches, and the packet leaves oneth3towardsigw.igwrewrites the source10.0.1.10to the Elastic IP203.0.113.10and sends it out ofeth2toweb. The web server sees the caller as203.0.113.10.webreplies to203.0.113.10.igwrewrites the destination back to10.0.1.10and sends it throughvpcrtr(the packet arrives oneth3, which has no rule of its own, so the main table is used and10.0.1.0/24is oneth2) tonatgw.natgwlooks up the remembered pair, rewrites the destination back to10.0.2.10, hands it tovpcrtr, andvpcrtrdelivers it toapponeth1.
The path to S3 through the gateway endpoint (step 6) is shorter: at point 2, table 200 has an extra line 198.51.100.0/24 via 10.255.1.2. That line is more specific than 0.0.0.0/0, so it wins, and the packet goes straight out of eth4 to s3, never through natgw, with its source still 10.0.2.10. s3 replies along 10.0.0.0/16 via 10.255.1.1.
What is prepared for you
The whole lab lives in one folder. Each file has one job:
03-private-subnet-nat/├── topo.clab.yml # 6 containers, 6 veth cables, management network l03-mgmt├── lenh-tat.sh # shortcuts: "app <command>" = "docker exec l03-nat-app <command>"├── cauhinh/ # one config script per container│ ├── app.sh # IP 10.0.2.10, default gateway 10.0.2.1│ ├── vpcrtr.sh # 4 ports, table 100 (rt-public), table 200 (rt-private, NO 0.0.0.0/0 yet), 2 ip rules│ ├── natgw.sh # IP 10.0.1.10, default 10.0.1.1, byte-counting rule "dem-nat"; NAT NOT on yet│ ├── igw.sh # Elastic IP 203.0.113.10, one-to-one SNAT/DNAT, drops packets from private IPs│ ├── web.sh # IP 203.0.113.80, web server answering "hello from 203.0.113.80"│ └── s3.sh # IPs 198.51.100.20 + 10.255.1.2, creates backup-100mb.bin, web server├── terraform/│ └── main.tf # the same design on the AWS API: 11 resources, run against Moto└── README.md # lab summaryWhen you run sudo containerlab deploy -t topo.clab.yml, containerlab does this, in order:
- Creates the Docker network
l03-mgmt(bridge,172.20.103.0/24). - Creates 6 containers
l03-nat-app,l03-nat-vpcrtr,l03-nat-natgw,l03-nat-igw,l03-nat-web,l03-nat-s3from thenicolaka/netshootimage, witheth0plugged intol03-mgmt. - Creates 6 veth pairs from the
linksblock and plugs each end into the right container under the right port name. - Mounts
cauhinh/<name>.shinto the container at/cauhinh.sh(read-only) and runssh /cauhinh.sh: IPs, routes,iptables, web servers. - Writes the container names into
/etc/hostson the lab host.
What is real and what is simulated:
| In the lab | Real or simulated | How it differs from real AWS |
|---|---|---|
Packets, routing tables, NAT, tcpdump, byte counters |
Real: a real Linux kernel forwards and translates every packet | AWS does not use iptables; the NAT gateway and the VPC router are services AWS runs for you. How packets are forwarded and translated matches what the AWS documentation describes. |
VPC router (vpcrtr) |
Simulated by a container | On AWS you cannot log in to it; you only edit route tables through the console or API. |
Internet (web) and S3 (s3) |
Simulated by containers on documentation ranges | Real S3 is an HTTPS API on many IP ranges; the lab uses one web server and one range 198.51.100.0/24. |
| Gateway endpoint | Simulated by 1 route + 1 dedicated cable | AWS uses a prefix list (pl-...) holding S3’s IP ranges in the Region, and adds the route itself when you associate a route table. |
| Terraform + Moto (step 7) | Real AWS API (same calls, same parameters), fake backend | Moto only answers API calls: no packets, no bill. On Moto, the NAT gateway gets random IPs that are not in the subnet. |
Mapping to real AWS resources (also the resources in main.tf):
| In the container lab | Real AWS resource | Terraform resource |
|---|---|---|
Range 10.0.0.0/16 |
VPC | aws_vpc.lab |
Cable 1 10.0.2.0/24 |
Private subnet | aws_subnet.private |
Cable 2 10.0.1.0/24 |
Public subnet | aws_subnet.public |
igw |
Internet gateway | aws_internet_gateway.igw |
203.0.113.10 on igw |
Elastic IP | aws_eip.nat |
natgw |
NAT gateway | aws_nat_gateway.nat |
| Table 100 / table 200 | Public / private route table | aws_route_table.public / aws_route_table.private |
ip rule iif ... |
Subnet association | aws_route_table_association (2 of them) |
Cable 6 + route 198.51.100.0/24 |
Gateway endpoint for S3 | aws_vpc_endpoint.s3 |
The whole run at a glance
The guide runs in this order. Each row has the expected result so you know you are on track:
| Order | What | Step | Expected result |
|---|---|---|---|
| 1 | Build the lab with containerlab | Step 1 | 6 containers l03-nat-* running, network l03-mgmt present |
| 2 | Check IPs and table-selection rules | Step 1 | app has 10.0.2.10/24, default via 10.0.2.1; vpcrtr has 2 rules iif eth2 → 100, iif eth1 → 200 |
| 3 | Break it: private subnet without 0.0.0.0/0 |
Step 2 | curl fails at once, ping reports Destination Net Unreachable |
| 4 | Half fix: route to the NAT, no translation yet | Step 3 | curl hangs for 5 seconds and times out; igw drops 3 packets |
| 5 | Full fix: turn on NAT | Step 4 | curl returns hello from 203.0.113.80; one packet carries 3 different source addresses |
| 6 | Break it another way: NAT in the wrong subnet, then undo | Step 5 | TTL drops 63, 62, 61, the packet loops, curl hangs |
| 7 | Compare cost: 100 MB from S3 through the NAT, then through the endpoint | Step 6 | Through the NAT: the NAT counts about 100 million bytes. Through the endpoint: 0 bytes |
| 8 | The same design in Terraform on Moto | Step 7 | 11 resources; the private route table has a pl-... → vpce-... line |
| 9 | Clean up | Clean up | No l03-nat-* containers left, Terraform reports everything destroyed |
Step 1: Build the lab and check it
What you are doing: reading the main config files, then creating 6 containers and wiring them exactly as in the real lab diagram above. containerlab reads a YAML file describing “which nodes exist and how they connect”, then creates the containers, the virtual cables, and runs a config script on each node.
name: l03-natprefix: __lab-namemgmt: network: l03-mgmt ipv4-subnet: 172.20.103.0/24topology: defaults: kind: linux image: nicolaka/netshoot:latest nodes: app: binds: [cauhinh/app.sh:/cauhinh.sh:ro] exec: [sh /cauhinh.sh] # vpcrtr, natgw, igw, web, s3: same, with their own script links: - endpoints: ["app:eth1", "vpcrtr:eth1"] # private subnet 10.0.2.0/24 - endpoints: ["vpcrtr:eth2", "natgw:eth1"] # public subnet 10.0.1.0/24 - endpoints: ["vpcrtr:eth3", "igw:eth1"] # VPC <-> IGW link - endpoints: ["igw:eth2", "web:eth1"] # "Internet" 203.0.113.0/24 - endpoints: ["igw:eth3", "s3:eth1"] # "Internet" on the S3 side 198.51.100.0/24 - endpoints: ["vpcrtr:eth4", "s3:eth2"] # gateway endpoint private path| Line | Meaning | Why it is set this way |
|---|---|---|
prefix: __lab-name |
Containers are named l03-nat-app, l03-nat-vpcrtr… |
Short names that are easy to type and do not clash with other labs on the same machine. |
mgmt: network: l03-mgmt |
A dedicated management network | containerlab plugs eth0 into a management network. A dedicated one keeps this lab separate from others. |
image: nicolaka/netshoot |
Every node is a Linux container with ip, iptables, tcpdump and curl built in |
One image for every role, nothing extra to install. |
binds + exec |
Mount the config script into the container and run it | The config lives in files on your machine, so destroying and redeploying gives the same result. |
links |
Each line is a virtual cable between two ports | Each subnet is its own cable, so you can break one piece at a time. |
The most important node is the VPC router, which holds the two route tables:
ip route del defaultsysctl -qw net.ipv4.ip_forward=1for i in all default eth1 eth2 eth3 eth4; do sysctl -qw net.ipv4.conf.$i.rp_filter=0; doneip addr add 10.0.2.1/24 dev eth1 # port into the private subnetip addr add 10.0.1.1/24 dev eth2 # port into the public subnetip addr add 10.255.0.1/30 dev eth3 # link to the IGWip addr add 10.255.1.1/30 dev eth4 # link to S3 via the gateway endpoint# Route table rt-public (table 100)ip route add 10.0.1.0/24 dev eth2 table 100ip route add 10.0.2.0/24 dev eth1 table 100ip route add default via 10.255.0.2 table 100# Route table rt-private (table 200): local only, NO 0.0.0.0/0 yetip route add 10.0.1.0/24 dev eth2 table 200ip route add 10.0.2.0/24 dev eth1 table 200# Associate subnets with route tablesip rule add iif eth2 table 100 pref 100 # public subnet -> rt-publicip rule add iif eth1 table 200 pref 200 # private subnet -> rt-private| Line | Meaning | Why it is set this way |
|---|---|---|
ip route del default |
Removes the default route containerlab adds via eth0 |
Keep it and the container has a real way out through the management network, which defeats the lab. Every exit must be one you built. |
ip_forward=1 |
Lets the container forward packets between ports | Without it the container is an ordinary host, not a router. |
rp_filter=0 |
Turns off the “this packet should have come in on another port” check | The router uses several routing tables; that check only looks at the main one and would drop valid packets. |
10.0.2.1, 10.0.1.1 |
The router sits at .1 in each subnet |
Same as AWS: the second address of every subnet belongs to the VPC router. |
... table 100 |
The three routes of rt-public: both VPC ranges go direct, everything else to the IGW | The first two are the local route every AWS route table has. The default line is 0.0.0.0/0 → igw. |
... table 200 |
rt-private only has the two local routes | The deliberate bug: no 0.0.0.0/0. Step 3 adds it. |
ip rule add iif eth2 table 100 |
Packets entering from the public subnet use rt-public | This is AWS’s “subnet association”: each subnet uses exactly one route table. |
The other two nodes worth reading are the NAT gateway and the internet gateway:
ip route del defaultsysctl -qw net.ipv4.ip_forward=1sysctl -qw net.ipv4.conf.all.send_redirects=0 net.ipv4.conf.eth1.send_redirects=0ip addr add 10.0.1.10/24 dev eth1ip route add default via 10.0.1.1iptables -A FORWARD -m comment --comment "dem-nat"| Line | Meaning | Why it is set this way |
|---|---|---|
10.0.1.10/24 |
The NAT gateway has a private IP in the public subnet | On AWS, a NAT gateway is a network interface in the subnet you pick, with a private IP from that subnet. |
default via 10.0.1.1 |
Sends everything outbound to the VPC router | A NAT gateway does not reach the Internet by itself: it hands packets to the router, which uses the route table of the NAT’s subnet. That is why the NAT must sit in a subnet with a route to the IGW. |
send_redirects=0 |
Do not send “take a shortcut” messages | Packets enter and leave the NAT on the same port, so Linux would suggest a shortcut and muddy the results. |
iptables -A FORWARD ... dem-nat |
A rule that blocks nothing and only counts packets and bytes through the NAT (the comment means “NAT counter”) | AWS bills NAT per GB processed. This counter gives the real byte count in step 6. |
| (no MASQUERADE yet) | The NAT does not translate yet | On purpose, so step 3 shows what happens with a route but no translation. |
ip addr add 203.0.113.10/24 dev eth2 # Elastic IP of the NAT gatewayip route add 10.0.0.0/16 via 10.255.0.1 # route back to the VPCiptables -t nat -A POSTROUTING -o eth2 -s 10.0.1.10 -j SNAT --to-source 203.0.113.10iptables -t nat -A PREROUTING -i eth2 -d 203.0.113.10 -j DNAT --to-destination 10.0.1.10iptables -t nat -A POSTROUTING -o eth3 -s 10.0.1.10 -j SNAT --to-source 203.0.113.10iptables -A FORWARD -s 10.0.1.10 -j ACCEPTiptables -A FORWARD -s 10.0.0.0/16 -j DROP| Line | Meaning | Why it is set this way |
|---|---|---|
203.0.113.10 |
The Elastic IP attached to the NAT gateway | On AWS, a public NAT gateway requires an Elastic IP at creation. |
SNAT ... --to-source 203.0.113.10 |
Packets leaving from 10.0.1.10 get the Elastic IP as source |
What the IGW does: one-to-one NAT between private and public IPs. |
DNAT ... --to-destination 10.0.1.10 |
Packets sent to the Elastic IP are delivered to 10.0.1.10 |
The reverse direction of the one-to-one NAT. |
-s 10.0.0.0/16 -j DROP |
Private IPs without a public IP cannot leave | The IGW only translates hosts that have a public IP or Elastic IP. A host with only a private IP cannot reach the Internet through it. |
The last three nodes are simple: app gets 10.0.2.10 and sends everything to 10.0.2.1 (a real EC2 instance also always sends to the VPC router); web and s3 run python3 -m http.server to answer curl and log the caller’s IP.
Start the lab and load the shortcuts:
sudo containerlab deploy -t topo.clab.ymlsource lenh-tat.shCheck that the lab you just built matches the diagram:
docker ps --filter name=l03-nat --format "table {{.Names}}\t{{.Image}}\t{{.Status}}"docker network ls --filter name=l03app ip -br addr show eth1; app ip routevpcrtr ip rule| Command | What it means | Why run it |
|---|---|---|
docker ps --filter name=l03-nat |
Lists containers whose name contains l03-nat, with image and status |
Confirms all 6 nodes exist and are running. |
docker network ls --filter name=l03 |
Lists Docker networks whose name contains l03 |
The management network l03-mgmt must be there. |
app ip -br addr show eth1 |
Shows the IP on port eth1 of app, in brief form |
app.sh must have set 10.0.2.10/24. |
app ip route |
Shows the routing table of app |
The default route must point to the VPC router 10.0.2.1. |
vpcrtr ip rule |
Shows the router’s table-selection rules | This is the “subnet association”: each subnet uses exactly one table. |

How to read the result:
| Line | What it means |
|---|---|
6 lines l03-nat-... nicolaka/netshoot:latest Up |
All 6 containers, one image, running. |
l03-mgmt bridge local |
The management network is a Docker bridge on the lab host. |
eth1@if270 UP 10.0.2.10/24 |
Port eth1 of app is up with IP 10.0.2.10/24. @if270 is the index of the other end of the cable (inside vpcrtr) and can change every time you build the lab. |
default via 10.0.2.1 dev eth1 |
Any destination without a specific line goes to the VPC router through eth1. |
10.0.2.0/24 dev eth1 ... |
Hosts in the private subnet talk directly. |
172.20.103.0/24 dev eth0 ... |
The route to the management network. It covers only 172.20.103.0/24; it is not a way out, because the default route via eth0 was removed. |
100: from all iif eth2 lookup 100 |
Packets arriving on eth2 (public subnet) use table 100 (rt-public). |
200: from all iif eth1 lookup 200 |
Packets arriving on eth1 (private subnet) use table 200 (rt-private). |
32766: from all lookup main |
Packets that match no rule above (for example replies arriving on eth3) use the main table, which only holds directly connected ranges. |
Step 2: The private subnet has no way out
What you are doing: looking at the private subnet’s route table, then trying to reach the web server on the Internet from app.
vpcrtr ip route show table 200app curl -sS -m5 http://203.0.113.80/app ping -c2 -W1 203.0.113.80
How to read it:
| Line | Meaning |
|---|---|
10.0.1.0/24 dev eth2, 10.0.2.0/24 dev eth1 |
The two local routes. There is no default line, meaning no 0.0.0.0/0. |
curl: (7) Failed to connect ... Could not connect to server |
The connection could not be opened. |
From 10.0.2.1 ... Destination Net Unreachable |
The VPC router (10.0.2.1) replies: “I have no route to that network”. |
Step 3: A route to the NAT, but no translation yet
What you are doing: adding 0.0.0.0/0 → NAT gateway to rt-private, the same as “Edit routes” on AWS. The NAT does not translate yet, so you see why translation is required.
vpcrtr ip route add default via 10.0.1.10 table 200app curl -sS -m5 http://203.0.113.80/igw iptables -L FORWARD -v -n| Part | Meaning | Why |
|---|---|---|
default via 10.0.1.10 |
0.0.0.0/0 goes to the NAT gateway |
On AWS the target of this route is the NAT gateway ID (nat-...); in the lab it is its IP. |
table 200 |
Added to rt-private | Only subnets associated with this table go through the NAT. The public subnet is not affected. |
-m5 |
curl waits at most 5 seconds | Long enough to see the hang without waiting forever. |

How to read it: this time curl is not refused right away, it hangs for 5 seconds (Connection timed out). The igw table shows the DROP rule for 10.0.0.0/16 has counted packets: they reached the internet gateway still sourced from 10.0.2.10, a private IP with no matching public IP, so they cannot go to the Internet. A route alone is not enough; something has to rewrite the address.
Step 4: Turn on NAT and watch the address change
What you are doing: letting the NAT gateway replace the private host’s source address with its own, then capturing the same packet at three points to see how the address changes.
natgw iptables -t nat -A POSTROUTING -s 10.0.2.0/24 -o eth1 -j MASQUERADEapp curl -sS -m5 http://203.0.113.80/| Part | Meaning | Why it is set this way |
|---|---|---|
-t nat -A POSTROUTING |
Rewrite the address just before the packet leaves the NAT | It happens after the route is chosen, so the outgoing packet carries the new address. |
-s 10.0.2.0/24 |
Only translate packets from the private subnet | An AWS NAT gateway translates every host that sends to it; limiting by range keeps the lab easy to read. |
-j MASQUERADE |
Replace the source with the outgoing port’s IP (10.0.1.10) and track the connection |
Tracking is what lets the reply be translated back to 10.0.2.10. |
Now capture the SYN packet (the packet that opens a TCP connection) on the NAT’s port and on the web server while app calls the web server:
docker exec -d l03-nat-natgw sh -c "tcpdump -lqtni eth1 -c2 'tcp[13]==2' > /tmp/syn.txt 2>/dev/null"docker exec -d l03-nat-web sh -c "tcpdump -lqtni eth1 -c1 'tcp[13]==2' > /tmp/syn.txt 2>/dev/null"sleep 1; app curl -s -o /dev/null http://203.0.113.80/; sleep 1echo '== natgw eth1:'; natgw cat /tmp/syn.txt; echo '== web eth1:'; web cat /tmp/syn.txtweb tail -1 /tmp/web.log
How to read it:
| Line | Meaning |
|---|---|
hello from 203.0.113.80 |
The private host can now reach the Internet. |
== natgw eth1: IP 10.0.2.10.59256 > 203.0.113.80.80 |
The packet entering the NAT, still sourced from app, source port 59256. |
IP 10.0.1.10.59256 > 203.0.113.80.80 |
The same packet leaving the NAT: the source is now the NAT gateway’s private IP, the source port is still 59256. |
== web eth1: IP 203.0.113.10.59256 > 203.0.113.80.80 |
At the web server the source is the Elastic IP: the internet gateway rewrote it once more. |
203.0.113.10 - - "GET / HTTP/1.1" 200 |
The web log only knows the Elastic IP; it never sees 10.0.2.10. |
This is exactly how AWS describes a public NAT gateway: the NAT gateway maps the host’s private IP to its own private IP, then the internet gateway maps the NAT gateway’s private IP to its Elastic IP.
See the path with traceroute:
app traceroute -n -w1 -q1 203.0.113.80
How to read it: hop 1 is the VPC router (10.0.2.1), hop 2 the NAT gateway (10.0.1.10), hop 4 the internet gateway (10.255.0.2), hop 5 the web server. Hop 3 is the VPC router a second time (the packet from the NAT comes back to the router to look up rt-public). It shows * because the router’s error reply comes back to its own address and is dropped; that is a lab detail and does not affect the connection.
Step 5: Put the NAT gateway in a private subnet by mistake
What you are doing: associating the NAT gateway’s subnet with rt-private instead of rt-public. This is a common mistake when creating a NAT gateway: picking the wrong subnet, or the NAT’s subnet using a route table with no route to the IGW.
vpcrtr ip rule del pref 100; vpcrtr ip rule add iif eth2 table 200 pref 100; vpcrtr ip rule(vpcrtr timeout 7 tcpdump -lnvi eth2 -c3 'tcp[13]==2' 2>/dev/null | grep -oE 'ttl [0-9]+|[0-9.]+ > [0-9.]+' | paste - - &)sleep 1; app curl -sS -m5 http://203.0.113.80/| Part | Meaning | Why |
|---|---|---|
ip rule ... iif eth2 table 200 |
Packets from the NAT’s subnet now use rt-private | Equivalent to a NAT gateway in a subnet whose route table has 0.0.0.0/0 → NAT and no → IGW. |
tcpdump -v + grep ttl |
Print the TTL and addresses of SYN packets on the router’s public-subnet port | The TTL drops by 1 at every router; a falling TTL is the sign of a loop. |

How to read it: the first packet (ttl 63, source 10.0.2.10) is the router sending to the NAT. The second (ttl 62, source 10.0.1.10) is the NAT, translation done, sending it back to the router. The router looks up rt-private, finds 0.0.0.0/0 → NAT again and sends it to the NAT once more (ttl 61). The packet never reaches the internet gateway and curl hangs. That is why a NAT gateway must live in a public subnet, one with a 0.0.0.0/0 → IGW route.
Put it back before the next step:
vpcrtr ip rule del pref 100; vpcrtr ip rule add iif eth2 table 100 pref 100Step 6: Download from S3 through the NAT, then through a gateway endpoint
What you are doing: downloading a 100 MB file from the s3 node (playing Amazon S3) twice: first through the NAT as by default, then after adding the gateway endpoint route. The NAT’s counter tells you how many bytes it processed, which on AWS means how much money.
natgw iptables -Z FORWARDapp curl -s -o /dev/null -w "%{size_download} bytes downloaded\n" http://198.51.100.20/backup-100mb.binnatgw iptables -L FORWARD -v -n -xs3 tail -1 /tmp/s3.log| Part | Meaning | Why |
|---|---|---|
iptables -Z FORWARD |
Reset the NAT’s counter to zero | Measure only this download, not earlier traffic. |
-w "%{size_download} ..." |
curl prints the number of bytes downloaded | To compare with the bytes the NAT counted. |
-L FORWARD -v -n -x |
Print the counter; -x prints exact bytes |
Without -x, iptables rounds to 96M and loses the detail. |

How to read it: the file is 100,000,000 bytes and the NAT counter shows 100,664,409 bytes (12,771 packets). The lab counter includes the TCP/IP headers of every packet in both directions, so it is a little more than the file. The s3 log shows the caller as 203.0.113.10: traffic to S3 took the long way out through the internet gateway.
Now add the gateway endpoint route to rt-private and download again:
vpcrtr ip route add 198.51.100.0/24 via 10.255.1.2 table 200; vpcrtr ip route show table 200natgw iptables -Z FORWARDapp curl -s -o /dev/null -w "%{size_download} bytes downloaded\n" http://198.51.100.20/backup-100mb.binnatgw iptables -L FORWARD -v -n -xs3 tail -1 /tmp/s3.log| Part | Meaning | Why |
|---|---|---|
198.51.100.0/24 via 10.255.1.2 table 200 |
The “S3” range takes the endpoint path | On AWS this route is added by AWS when you associate the route table with the gateway endpoint; its destination is the S3 prefix list (pl-...), not a range you type. |
default via 10.0.1.10 unchanged |
Every other destination still goes through the NAT | A route table uses the most specific match: a /24 beats 0.0.0.0/0. |

How to read it: the full 100,000,000 bytes still arrive, but the NAT counter shows 0 packets, 0 bytes: nothing went through the NAT. The s3 log now shows 10.0.2.10. The AWS documentation describes exactly this: once the endpoint exists, S3 sees the private IP in the VPC as the source instead of a public IP, and open TCP connections are dropped when the routes change.
Turned into money with real AWS prices in the Singapore Region, for 500 GB a month from the private subnet to S3:
| Item | Singapore price (ap-southeast-1) | 500 GB/month |
|---|---|---|
| NAT gateway, per hour | $0.059/hour × 730 hours | $43.07 |
| NAT gateway, data processing | $0.059/GB × 500 GB | $29.50 |
| Public IPv4 (the NAT’s Elastic IP) | $0.005/hour × 730 hours | $3.65 |
| S3 gateway endpoint | no additional charge | $0 |
Step 7: The same design in Terraform
What you are doing: writing the same design in Terraform and running it against Moto (an AWS API mock), to see what the AWS route tables look like, including the route a gateway endpoint adds.
provider "aws" { region = "ap-southeast-1" access_key = "test" # fake key for Moto secret_key = "test" skip_credentials_validation = true skip_metadata_api_check = true skip_requesting_account_id = true endpoints { ec2 = "http://localhost:4566" sts = "http://localhost:4566" }}
resource "aws_vpc" "lab" { cidr_block = "10.0.0.0/16" enable_dns_support = true enable_dns_hostnames = true}# aws_subnet "public" 10.0.1.0/24 and "private" 10.0.2.0/24, both in ap-southeast-1aresource "aws_internet_gateway" "igw" { vpc_id = aws_vpc.lab.id }resource "aws_eip" "nat" { domain = "vpc" }
resource "aws_nat_gateway" "nat" { subnet_id = aws_subnet.public.id allocation_id = aws_eip.nat.id depends_on = [aws_internet_gateway.igw]}
resource "aws_route_table" "private" { vpc_id = aws_vpc.lab.id route { cidr_block = "0.0.0.0/0" nat_gateway_id = aws_nat_gateway.nat.id }}# aws_route_table "public": 0.0.0.0/0 -> gateway_id = aws_internet_gateway.igw.id# + 2 aws_route_table_association, one per subnet
resource "aws_vpc_endpoint" "s3" { vpc_id = aws_vpc.lab.id service_name = "com.amazonaws.ap-southeast-1.s3" vpc_endpoint_type = "Gateway" route_table_ids = [aws_route_table.private.id]}| Line | Meaning | Why it is set this way |
|---|---|---|
region = "ap-southeast-1" |
Singapore | Matches the prices in step 6. A gateway endpoint only works in the Region where it was created, so the S3 bucket must be in that Region too. |
access_key = "test", the skip_* lines, endpoints |
Fake keys and the EC2 API pointed at Moto | Never hits real AWS by mistake. Remove these lines for real AWS. |
enable_dns_support, enable_dns_hostnames |
DNS resolution inside the VPC | AWS requires both when you use the Amazon DNS server with an S3 gateway endpoint; without them S3 names do not resolve correctly. |
aws_eip + allocation_id |
Allocate an Elastic IP and attach it to the NAT gateway | A public NAT gateway requires an Elastic IP at creation. |
subnet_id = aws_subnet.public.id |
Put the NAT gateway in the public subnet | Step 5 showed what happens in the wrong subnet: the packet loops. |
depends_on = [aws_internet_gateway.igw] |
Create the IGW before the NAT gateway | The NAT needs the IGW to reach the Internet; stating the order stops Terraform from creating the NAT before the VPC has a way out. |
nat_gateway_id in rt-private |
0.0.0.0/0 → NAT gateway |
The same route step 3 added by hand. |
vpc_endpoint_type = "Gateway" |
A gateway endpoint, not an interface endpoint | An S3 gateway endpoint has no additional charge; an interface endpoint is billed per hour and per GB. |
route_table_ids = [private] |
Associate the endpoint with rt-private | AWS adds the S3 prefix list → endpoint route to this table. Subnets using other tables keep their old path to S3. |
cd terraformterraform initterraform apply -auto-approve -no-color | tail -9
Inspect the private subnet’s route table with the AWS CLI (awsl is the AWS CLI pointed at Moto):
awsl ec2 describe-route-tables --route-table-ids $(terraform output -raw rt_private) \ --query "RouteTables[0].Routes[].[DestinationCidrBlock||DestinationPrefixListId, GatewayId||NatGatewayId, State]" --output tableawsl ec2 describe-prefix-lists --query "PrefixLists[?PrefixListName=='com.amazonaws.ap-southeast-1.s3']" --output table
How to read it: the private subnet’s route table has three routes. 10.0.0.0/16 → local is the in-VPC route every table has. 0.0.0.0/0 → nat-... is the Internet path through the NAT. pl-... → vpce-... is the route the gateway endpoint added: its destination is not a range you typed but an AWS-managed prefix list, and the second command shows that prefix list is com.amazonaws.ap-southeast-1.s3. On real AWS you can see this route but cannot edit or delete it; to remove it, disassociate the route table from the endpoint.
Applying this on real AWS
- Check along the path when a private host cannot reach the Internet: the route table of the host’s subnet has
0.0.0.0/0 → nat-...; the NAT gateway isAvailableand sits in a public subnet; the route table of the NAT’s subnet has0.0.0.0/0 → igw-...; no other route “steals” part of the traffic to another device. - One NAT gateway per Availability Zone if you need resilience: each NAT gateway lives in a single AZ. Keeping the NAT in the same AZ as the hosts using it also avoids cross-AZ data transfer charges.
- Create gateway endpoints for S3 (and DynamoDB) in every VPC whose private subnets talk to those services. There is no additional charge, but associate them with the right route tables.
- Security groups and network ACLs still have to allow it: the host’s security group needs an outbound rule to S3 (it can reference the S3 prefix list ID). Network ACLs cannot reference prefix lists, so use S3’s IP ranges.
- Network ACLs are stateless (they do not track connections): the NAT gateway’s subnet must allow both directions, including the ephemeral ports
1024-65535the NAT gateway uses as source ports. The guide Security groups, NACLs and ephemeral ports goes deeper into this one. - Adding or changing an endpoint drops open TCP connections to S3: do it when nothing critical is running, or make sure the application reconnects.
- Do not use
aws:SourceIpin bucket policies for traffic through an endpoint: S3 sees the private IP, so useaws:VpcSourceIpor restrict by endpoint. - A gateway endpoint cannot be used from outside the VPC (an office over VPN, Direct Connect, a transit gateway or VPC peering). Those cases need an interface endpoint, which is billed.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
curl: (28) Connection timed out from a private host, ping gets no reply |
The private subnet’s route table lacks 0.0.0.0/0 → nat-..., or the subnet is associated with the wrong route table |
Open the route table of the host’s subnet (the subnet’s Route table tab) and add a route to the NAT gateway |
| A route to the NAT exists but it still hangs | The NAT gateway is in a private subnet, or the NAT’s subnet has no 0.0.0.0/0 → igw-... |
Create the NAT gateway in a public subnet; check the route table of the NAT’s subnet |
| A host in a public subnet with a route to the IGW still cannot get out | The host has no public IP or Elastic IP | Attach an Elastic IP, or move the host to a private subnet behind the NAT |
| The connection opens but replies never arrive, or it is flaky | The network ACL blocks the return traffic (ephemeral ports 1024-65535) | Allow the ephemeral ports inbound/outbound in the NACL; see Security groups, NACLs and ephemeral ports |
| Every route is right but it still cannot get out | The host’s security group outbound rules were narrowed | Add an outbound rule for the port you need (for example TCP 443) |
| The gateway endpoint exists but the NAT bill does not drop | The endpoint is associated with the wrong route table, the bucket is in another Region, or the app goes through a proxy | Check the endpoint’s route_table_ids; the bucket must be in the same Region; the prefix list only holds that Region’s S3 ranges |
| With the endpoint in place, S3 names do not resolve or still take the old path | DNS resolution or DNS hostnames is disabled on the VPC, or a custom DNS server is used | Enable both VPC DNS attributes; a custom DNS server must resolve S3 to AWS’s IP addresses |
| The bucket policy starts denying after adding the endpoint | The policy uses aws:SourceIp |
Switch to aws:VpcSourceIp or aws:SourceVpce |
Clean up
sudo containerlab destroy -t topo.clab.yml --cleanupcd terraform && terraform destroy -auto-approveOn real AWS, delete the NAT gateway and release the Elastic IP when you no longer need them: a NAT gateway is billed for every hour it exists (a partial hour counts as a full hour), and a public IPv4 address still costs $0.005 per hour.
FAQ
What actually makes a subnet public or private? Only the route table. A subnet whose route table has a route to an internet gateway is public; without that route it is private.
Why not put every host in a public subnet with a public IP? Each public IPv4 address costs $0.005 per hour, and a host with a public IP can be reached from the Internet if its security group allows it. A private host going out through a NAT cannot be reached by connections started from the Internet.
Can a gateway endpoint replace a NAT gateway? Only for S3 and DynamoDB in the same Region. If the host needs to reach anything else on the Internet, you still need a NAT gateway.
What is the difference between a gateway endpoint and an interface endpoint for S3? A gateway endpoint is a route in a route table, has no additional charge and only works inside the VPC. An interface endpoint is a network interface with a private IP in your subnet, billed per hour and per GB, but usable from an office over VPN or Direct Connect.
How much does a NAT gateway cost with almost no traffic? In Singapore the hourly charge alone is 0.059 × 730 = $43.07 a month, plus $3.65 for the Elastic IP, even if not a single byte goes through it.
References
- AWS: NAT gateways
- AWS: NAT gateway use cases
- AWS: Internet gateways, public and private subnets
- AWS: Reserved addresses in a subnet
- AWS: Gateway endpoints
- AWS: Gateway endpoints for Amazon S3
- AWS: NAT gateway pricing and reducing data processing charges
- AWS: Amazon VPC pricing (NAT gateway, public IPv4)
- AWS: Network ACLs and ephemeral ports
- AWS re:Post: Why can’t I use a NAT gateway to access the internet from my EC2 instances?
- containerlab · Moto

Cloud & Network Engineer · TrungCLN
Designs and runs AWS infrastructure, hybrid networking (VPN, BGP) and cloud security. Every guide on this site is built and run for real before it is written.
Related guides

AWSBeginner30 min
ALB 502 or 503? Every target is unhealthy, yet the site still returns 200
A real lab with Envoy playing an ALB: all targets unhealthy still returns 200 (fail open), a closed app port gives 502, only an empty target group gives 503.
AWSBeginner35 minutes
IAM AccessDenied with the right action: S3 ARNs, explicit Deny and roles
Your policy allows s3:GetObject, yet you get AccessDenied? A hands-on IAM lab with real authorization: bucket vs object ARNs, Deny beats Allow, STS roles.