Skip to the workspace
Pebellslearn by building
VPC — Your Private Network  ›  Lesson

NAT Gateway — Out but Not In

NAT Gateway 12 minA NAT gateway sits in a public subnet and lets instances in private subnets start connections to the internet, while still blocking connections that the internet tries to start.

aws / NAT Gateway — Out but Not In

Explore each scene at your own pace.

01 · The rule

The rule

NAT Gateway — Out but Not In

Inkwell's nightly image job lives in a private subnet, safely unreachable from the internet. Then it needs a security update from the Linux package servers, and the download fails: a private subnet has no route out. Moving the job to a public subnet would fix the download but expose the server. There is a middle path.

The rule

NAT lets private servers call out; nobody can call in.

A NAT gateway (Network Address Translation) is a managed AWS service that acts on behalf of private instances:

  1. A private instance sends a request to an internet address.
  2. The private subnet's route table sends it to the NAT gateway.
  3. The NAT gateway swaps the private source address for its own public address and forwards the request through the internet gateway.
  4. The reply comes back to the NAT gateway, which hands it to the instance that asked.

Because the NAT gateway only forwards replies to conversations that were started from inside, the internet cannot start a connection to the private instance.

Setting it up has three parts:

  • Create the NAT gateway in a public subnet, with an Elastic IP. It needs the public subnet's route to the internet gateway to do its job.
  • In the private subnet's route table, add 0.0.0.0/0 → nat-inkwell.
  • Leave the internet gateway where it is; the NAT gateway uses it.
Private route table
10.0.0.0/16   local
0.0.0.0/0     nat-inkwell

Two practical points. A NAT gateway is charged per hour and per gigabyte processed, so it is not free to leave running. And it lives in one zone: for resilience, run one per zone and point each private subnet at the NAT gateway in its own zone. For traffic to S3, a gateway endpoint avoids NAT charges.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.