Skip to content
TrungCLN

AWSBeginner

Private subnet can't reach the Internet: route tables, NAT Gateway and VPC endpoints

By Cao Le Nam TrungPublished Updated 44 min read

VPC 10.0.0.0/16Public subnetPrivate subnetinternet via NATInternet GWNAT gatewayApp server
Diagram drawn with the official AWS Architecture Icons.

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

Video thumbnail: Private subnet can't reach the Internet: route tables, NAT Gateway and VPC endpoints

🎬 Goes live 12 Dec 2026, 20:00 (UTC+7)

Subscribe to YouTube @trungcln to get notified

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:

10.0.0.0/16 → localinside the VPC: direct0.0.0.0/0 → igweverything else: internet gateway10.0.0.0/16 → localinside the VPC: direct0.0.0.0/0 → nateverything else: via the NAT gatewayPublic subnetPrivate subnetOnly hosts with a public IP or Elastic IPcan talk to the internetCan go out through the NAT gatewaythe internet cannot start a connection in
A route table is the signpost at the gate of each neighbourhood. The public one has a "to the highway" sign (the internet gateway). The private one has no exit of its own: to get out, traffic has to pass through the NAT checkpoint.

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

VPC 10.0.0.0/16Private subnetPublic subnetGateway endpointAmazon S3198.51.100.20app · 10.0.2.10NAT · 10.0.1.10Internet gatewayweb 203.0.113.80rt-private0.0.0.0/0 → NATS3 → endpointrt-public0.0.0.0/0 → IGWElastic IP 203.0.113.10Orange: internet via NAT (billed per GB) · Green: S3 via gateway endpoint (no charge)
The orange path goes to the internet: it has to borrow the NAT gateway, and AWS bills every GB. The green path goes to S3 through the gateway endpoint: no NAT, no processing charge.

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.

l03-nat-appEC2 · 10.0.2.10l03-nat-vpcrtrVPC routerl03-nat-natgwNAT · 10.0.1.10l03-nat-igwIGW · EIP 203.0.113.10l03-nat-webInternet · 203.0.113.80l03-nat-s3S3 · 198.51.100.20eth1 ↔ eth110.0.2.0/24.10 ↔ .1eth2 ↔ eth110.0.1.0/24.1 ↔ .10eth3 ↔ eth110.255.0.0/30 · .1 ↔ .2eth2 ↔ eth1203.0.113.0/24.1 ↔ .80eth3 ↔ eth1198.51.100.0/24.1 ↔ .20eth4 ↔ eth2 · 10.255.1.0/30gateway endpoint pathEvery container: image nicolaka/netshoot · eth0 on management network l03-mgmt 172.20.103.0/24 (default route removed)
Each box is a real container (container name, role, IP). Each line is a virtual cable pair (veth) created by containerlab, labelled with the port at each end and the IP range. The dashed green line is the private path to S3, playing the gateway endpoint.

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:

appsends to .1vpcrtrlooks up rt-privatenatgwrewrites sourcevpcrtrlooks up rt-publicigwrewrites to EIPwebreceives123456. The reply goes back: igw maps 203.0.113.10 → 10.0.1.10, natgw maps 10.0.1.10 → 10.0.2.10Packet source: 10.0.2.10 (steps 1–2) → 10.0.1.10 (steps 3–4) → 203.0.113.10 (step 5)The destination is always 203.0.113.80:80; only the source changes
The packet crosses the VPC router twice: first it uses the private subnet table (send to the NAT), then the public subnet table (send to the internet gateway). The numbers on the arrows match the numbered steps in the text.
  1. app runs curl http://203.0.113.80/. The destination is not in 10.0.2.0/24, so app sends the packet to its default gateway 10.0.2.1, which is port eth1 of vpcrtr. The source of the packet is 10.0.2.10.
  2. vpcrtr sees the packet arrive on eth1 (private subnet), so the ip rule picks table 200 (rt-private). The line 0.0.0.0/0 via 10.0.1.10 matches, and the packet leaves on eth2 towards natgw.
  3. natgw receives the packet on eth1. The MASQUERADE rule rewrites the source 10.0.2.10 to 10.0.1.10 and remembers the pair. The packet is sent to the default gateway 10.0.1.1, back to vpcrtr on the same cable.
  4. vpcrtr sees the packet arrive on eth2 (public subnet), so the rule picks table 100 (rt-public). The line default via 10.255.0.2 matches, and the packet leaves on eth3 towards igw.
  5. igw rewrites the source 10.0.1.10 to the Elastic IP 203.0.113.10 and sends it out of eth2 to web. The web server sees the caller as 203.0.113.10.
  6. web replies to 203.0.113.10. igw rewrites the destination back to 10.0.1.10 and sends it through vpcrtr (the packet arrives on eth3, which has no rule of its own, so the main table is used and 10.0.1.0/24 is on eth2) to natgw. natgw looks up the remembered pair, rewrites the destination back to 10.0.2.10, hands it to vpcrtr, and vpcrtr delivers it to app on eth1.

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 summary

When you run sudo containerlab deploy -t topo.clab.yml, containerlab does this, in order:

  1. Creates the Docker network l03-mgmt (bridge, 172.20.103.0/24).
  2. Creates 6 containers l03-nat-app, l03-nat-vpcrtr, l03-nat-natgw, l03-nat-igw, l03-nat-web, l03-nat-s3 from the nicolaka/netshoot image, with eth0 plugged into l03-mgmt.
  3. Creates 6 veth pairs from the links block and plugs each end into the right container under the right port name.
  4. Mounts cauhinh/<name>.sh into the container at /cauhinh.sh (read-only) and runs sh /cauhinh.sh: IPs, routes, iptables, web servers.
  5. Writes the container names into /etc/hosts on 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.

topo.clab.yml
name: l03-nat
prefix: __lab-name
mgmt:
network: l03-mgmt
ipv4-subnet: 172.20.103.0/24
topology:
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:

cauhinh/vpcrtr.sh
ip route del default
sysctl -qw net.ipv4.ip_forward=1
for i in all default eth1 eth2 eth3 eth4; do sysctl -qw net.ipv4.conf.$i.rp_filter=0; done
ip addr add 10.0.2.1/24 dev eth1 # port into the private subnet
ip addr add 10.0.1.1/24 dev eth2 # port into the public subnet
ip addr add 10.255.0.1/30 dev eth3 # link to the IGW
ip 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 100
ip route add 10.0.2.0/24 dev eth1 table 100
ip route add default via 10.255.0.2 table 100
# Route table rt-private (table 200): local only, NO 0.0.0.0/0 yet
ip route add 10.0.1.0/24 dev eth2 table 200
ip route add 10.0.2.0/24 dev eth1 table 200
# Associate subnets with route tables
ip rule add iif eth2 table 100 pref 100 # public subnet -> rt-public
ip 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:

cauhinh/natgw.sh
ip route del default
sysctl -qw net.ipv4.ip_forward=1
sysctl -qw net.ipv4.conf.all.send_redirects=0 net.ipv4.conf.eth1.send_redirects=0
ip addr add 10.0.1.10/24 dev eth1
ip route add default via 10.0.1.1
iptables -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.
cauhinh/igw.sh
ip addr add 203.0.113.10/24 dev eth2 # Elastic IP of the NAT gateway
ip route add 10.0.0.0/16 via 10.255.0.1 # route back to the VPC
iptables -t nat -A POSTROUTING -o eth2 -s 10.0.1.10 -j SNAT --to-source 203.0.113.10
iptables -t nat -A PREROUTING -i eth2 -d 203.0.113.10 -j DNAT --to-destination 10.0.1.10
iptables -t nat -A POSTROUTING -o eth3 -s 10.0.1.10 -j SNAT --to-source 203.0.113.10
iptables -A FORWARD -s 10.0.1.10 -j ACCEPT
iptables -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:

Terminal window
sudo containerlab deploy -t topo.clab.yml
source lenh-tat.sh

Check that the lab you just built matches the diagram:

Terminal window
docker ps --filter name=l03-nat --format "table {{.Names}}\t{{.Image}}\t{{.Status}}"
docker network ls --filter name=l03
app ip -br addr show eth1; app ip route
vpcrtr 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.
Terminal: docker ps lists 6 containers l03-nat-s3, l03-nat-igw, l03-nat-app, l03-nat-vpcrtr, l03-nat-natgw, l03-nat-web, all image nicolaka/netshoot:latest, status Up 24 seconds; docker network ls shows l03-mgmt with driver bridge; app has eth1 10.0.2.10/24, default via 10.0.2.1 dev eth1; vpcrtr ip rule shows 100: from all iif eth2 lookup 100 and 200: from all iif eth1 lookup 200.
The freshly built lab: 6 containers, the l03-mgmt network, app's IP and routes, and the VPC router's two table-selection rules.

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.

Terminal window
vpcrtr ip route show table 200
app curl -sS -m5 http://203.0.113.80/
app ping -c2 -W1 203.0.113.80
Route table rt-private has only two routes, 10.0.1.0/24 dev eth2 and 10.0.2.0/24 dev eth1. curl reports Failed to connect to 203.0.113.80:80 after 0 ms: Could not connect to server. ping reports From 10.0.2.1 Destination Net Unreachable with 100% packet loss.
Real run: the private subnet's route table only knows the VPC, so the VPC router has nowhere to send packets for 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.

Terminal window
vpcrtr ip route add default via 10.0.1.10 table 200
app 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.
curl reports Connection timed out after 5001 milliseconds. The igw FORWARD table: the ACCEPT rule for 10.0.1.10 has counted 0 packets, the DROP rule for 10.0.0.0/16 has counted the dropped SYN packets.
Real run: the packets reach the internet gateway but still carry the private IP 10.0.2.10, so they are dropped at the Internet door.

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.

Terminal window
natgw iptables -t nat -A POSTROUTING -s 10.0.2.0/24 -o eth1 -j MASQUERADE
app 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:

Terminal window
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 1
echo '== natgw eth1:'; natgw cat /tmp/syn.txt; echo '== web eth1:'; web cat /tmp/syn.txt
web tail -1 /tmp/web.log
curl returns hello from 203.0.113.80. Under natgw eth1 there are two lines, IP 10.0.2.10.59256 and IP 10.0.1.10.59256 to 203.0.113.80.80; under web eth1 there is IP 203.0.113.10.59256 to 203.0.113.80.80; the web log shows 203.0.113.10 GET / 200.
Real run: one SYN packet, one source port, three different source addresses at three points. The web server only ever sees the Elastic IP.
appNAT gatewayInternet gatewaywebsource 10.0.2.10→ 203.0.113.80:80source 10.0.1.10→ 203.0.113.80:80source 203.0.113.10→ 203.0.113.80:80rewritten by the NAT gatewayrewritten again by the internet gatewayThe source port stays the same, only the source address changes. The reply goes back the same way and each hop reverses its change.
Think of mail sent through a front desk: the receptionist (NAT gateway) replaces the sender's name with their own, then the post office (internet gateway) stamps the public address. Replies reach the right person because the receptionist keeps a 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:

Terminal window
app traceroute -n -w1 -q1 203.0.113.80
traceroute to 203.0.113.80: hop 1 10.0.2.1, hop 2 10.0.1.10, hop 3 an asterisk, hop 4 10.255.0.2, hop 5 203.0.113.80.
Real run: the packet goes from the VPC router to the NAT gateway, back to the VPC router, and only then out to the internet gateway.

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

VPC routerlooks up rt-privateNAT gatewaysends it back to the routerInternet gateway0.0.0.0/0 → NATTTL 63 → 62 → 61 ...never reachedThe NAT's subnet has no0.0.0.0/0 → IGW route
Like asking two people for directions who keep pointing at each other: each says "ask the other one", and nobody points to the highway.

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.

Terminal window
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.
ip rule shows iif eth2 lookup 200. tcpdump sees three SYN packets: ttl 63 from 10.0.2.10, ttl 62 from 10.0.1.10, ttl 61 from 10.0.1.10, all to 203.0.113.80.80. curl reports Connection timed out after 5001 milliseconds.
Real run: the packet bounces between the VPC router and the NAT gateway, its TTL dropping, and never reaches the internet gateway.

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:

Terminal window
vpcrtr ip rule del pref 100; vpcrtr ip rule add iif eth2 table 100 pref 100

Step 6: Download from S3 through the NAT, then through a gateway endpoint

appNAT gatewaybilled per GBInternet gatewayAmazon S3sees 203.0.113.10appGateway endpointS3 prefix list → vpce · no chargeAmazon S3sees 10.0.2.10Without an endpointWith a gateway endpointA destination in S3's ranges matches the prefix list, more specific than 0.0.0.0/0, so it uses the endpoint
A route table always uses the most specific matching route. The S3 ranges (the prefix list) are more specific than 0.0.0.0/0, so once the endpoint exists every packet to S3 takes the endpoint path; everything else still goes through the NAT.

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.

Terminal window
natgw iptables -Z FORWARD
app curl -s -o /dev/null -w "%{size_download} bytes downloaded\n" http://198.51.100.20/backup-100mb.bin
natgw iptables -L FORWARD -v -n -x
s3 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.
curl downloads 100000000 bytes. The FORWARD counter on natgw: 12771 packets, 100664409 bytes on the dem-nat rule. The s3 log shows 203.0.113.10 GET /backup-100mb.bin 200.
Real run: downloading 100 MB from S3 through the NAT means the NAT processes over 100 million bytes, and S3 sees the caller as the Elastic IP.

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:

Terminal window
vpcrtr ip route add 198.51.100.0/24 via 10.255.1.2 table 200; vpcrtr ip route show table 200
natgw iptables -Z FORWARD
app curl -s -o /dev/null -w "%{size_download} bytes downloaded\n" http://198.51.100.20/backup-100mb.bin
natgw iptables -L FORWARD -v -n -x
s3 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.
rt-private now has 198.51.100.0/24 via 10.255.1.2 dev eth4 next to default via 10.0.1.10. curl downloads 100000000 bytes. The natgw counter: 0 packets, 0 bytes. The s3 log shows 10.0.2.10 GET /backup-100mb.bin 200.
Real run: the same 100 MB file, the NAT processes 0 bytes, and S3 sees the private IP 10.0.2.10 of the host in the VPC.

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:

Monthly cost: 500 GB to S3, Singapore Region$0$20$40$60$80Everything through the NAT gatewayincluding the 500 GB to S3$76.22S3 through a gateway endpointNAT kept for internet traffic$46.72Only S3, no internet neededno NAT, endpoint only$0.00NAT hours: 730 × 0.059 = $43.07NAT data processing: 500 GB × 0.059 = $29.50Elastic IP public IPv4: 730 × 0.005 = $3.65Gateway endpoint: $0AWS list prices (price list published 2026-11-30), 1 month = 730 hours, 1 NAT gateway, other data transfer charges not included.
Calculated from real AWS list prices in Singapore. The orange part is what the NAT gateway charges for every GB it processes; move those 500 GB to a gateway endpoint and the orange part disappears.
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.

terraform/main.tf (main parts)
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-1a
resource "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.
Terminal window
cd terraform
terraform init
terraform apply -auto-approve -no-color | tail -9
terraform apply ends with Apply complete! Resources: 11 added, 0 changed, 0 destroyed, followed by the outputs nat_gateway_id, rt_private, rt_public, s3_endpoint_id and vpc_id.
Real run on Moto: 11 resources created (VPC, 2 subnets, IGW, EIP, NAT gateway, 2 route tables, 2 subnet associations, 1 endpoint).

Inspect the private subnet’s route table with the AWS CLI (awsl is the AWS CLI pointed at Moto):

Terminal window
awsl ec2 describe-route-tables --route-table-ids $(terraform output -raw rt_private) \
--query "RouteTables[0].Routes[].[DestinationCidrBlock||DestinationPrefixListId, GatewayId||NatGatewayId, State]" --output table
awsl ec2 describe-prefix-lists --query "PrefixLists[?PrefixListName=='com.amazonaws.ap-southeast-1.s3']" --output table
The private route table has three routes: 10.0.0.0/16 local active, 0.0.0.0/0 to nat-... active, pl-... to vpce-... active. describe-prefix-lists shows that prefix list is named com.amazonaws.ap-southeast-1.s3.
Real run on Moto: the gateway endpoint adds a route whose destination is the S3 prefix list, next to the 0.0.0.0/0 route to the NAT gateway.

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 is Available and sits in a public subnet; the route table of the NAT’s subnet has 0.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-65535 the 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:SourceIp in bucket policies for traffic through an endpoint: S3 sees the private IP, so use aws:VpcSourceIp or 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

Terminal window
sudo containerlab destroy -t topo.clab.yml --cleanup
cd terraform && terraform destroy -auto-approve

On 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

Cao Le Nam Trung

Cao Le Nam Trung

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.

LinkedIn YouTube Facebook

Related guides