Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

Ask two questions for any private resource: "Does it need to start conversations with the internet? Does the internet need to start them with it?" Out only → NAT. In as well → it belongs in a public subnet, or behind a load balancer.

Deciding for the image jobStep 1 of 3
  1. Does it call out?

    Yes: it downloads updates.

The classic mistake is creating the NAT gateway inside the private subnet. There it has no route to the internet gateway, so it has nowhere to send traffic.

Drills

Answer fast — trust the rule.

quick drill

Where must a public NAT gateway be created?

quick drill

Which route sends a private subnet's internet traffic through the NAT gateway?

quick drill

A stranger on the internet tries to connect to the image job through the NAT gateway. What happens?

quick drill

Inkwell has private subnets in two zones and one NAT gateway in ap-south-1a. What is the risk?

Remember

  • NAT lets private servers call out; nobody can call in.
  • NAT gateway in a public subnet, with an Elastic IP; private route table sends 0.0.0.0/0 to it.
  • Traps: never place it in the private subnet, and remember it costs per hour, per GB, and lives in one zone.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainUpdates for a private server

Inkwell's nightly image job runs in private subnet 10.0.11.0/24 and needs to download software updates from the internet. Describe where to put the NAT gateway, what route to add and to which route table, what the NAT gateway will and will not allow, and one cost or resilience point Inkwell should consider.