Stop 07 · Networking With Amazon VPCTopics · 5/9

NAT Gateways

Part of the AWS Roadmap

In short

Your private app servers need to download security updates and call payment APIs, but nothing on the internet should be able to start a connection to them. A NAT gateway gives exactly that one-way door, and it's also one of the most common surprise lines on a learner's bill.

A NAT gateway lets instances in private subnets start outbound connections to the internet, for example to download updates, while blocking inbound connections. It sits in a public subnet and is charged per hour and per GB processed, so delete it after labs.

The jaadu sits at an office reception switchboard plugging cables to connect calls going out, while a locked inner door stays shut behind it.

Think of it like this

An office reception line

Employees inside can dial out through the office's main number, and the person they call sees only that number. Outsiders can't dial an employee's desk directly. The NAT gateway is the reception line for your private subnets.

Watch it happen

A private server downloads an update

App server (private)
Private route table
NAT gateway (public subnet)
Package mirror

Request to internet

1/5dnf update needs to reach a package repository.

The big ideas

What you need to know

Outbound only

Private instances can start connections out. The internet cannot start connections in.

Lives in a public subnet

A public NAT gateway sits in a public subnet with an Elastic IP, and uses the VPC's internet gateway.

One per AZ for HA

A NAT gateway is tied to one AZ. For resilience, run one per AZ and point each private subnet at the one in its own AZ.

Hourly + per GB

You pay per hour it exists and per GB processed. Delete it after labs, and use gateway endpoints for S3 traffic.

See it in code

Walk through the code

bash
1PUB_A=subnet-0aaa00000000000012RT_PRIV_A=rtb-0bbb00000000000023 4EIP=$(aws ec2 allocate-address --domain vpc --query AllocationId --output text)5 6NAT_ID=$(aws ec2 create-nat-gateway --subnet-id "$PUB_A" --allocation-id "$EIP" \7  --query NatGateway.NatGatewayId --output text)8aws ec2 wait nat-gateway-available --nat-gateway-ids "$NAT_ID"9 10aws ec2 create-route --route-table-id "$RT_PRIV_A" \11  --destination-cidr-block 0.0.0.0/0 --nat-gateway-id "$NAT_ID"12 13# After the lab: delete the NAT gateway, then release the Elastic IP14# aws ec2 delete-nat-gateway --nat-gateway-id "$NAT_ID"15# aws ec2 release-address --allocation-id "$EIP"

1/4Allocate an Elastic IP for the NAT gateway's public address.

Create a NAT gateway in the public subnet, route the private subnet through it, and remember the clean-up lines.

Save yourself the pain

Common mistakes

Oops

Placing the NAT gateway in the private subnet it serves.

Do this instead

It must live in a public subnet whose route table points to the internet gateway.

Oops

Leaving a lab NAT gateway running for a month.

Do this instead

Set a budget alert and delete NAT gateways and Elastic IPs as part of every clean-up.

Oops

Sending large S3 downloads through the NAT gateway.

Do this instead

Add a free S3 gateway endpoint so that traffic skips the NAT gateway's per-GB charge.

Your mission

One-way door

~35 min

Quick check

An instance in a private subnet with a NAT gateway route runs a web server on port 80. Can people on the internet reach it?

Go deeper

  1. 1Add a NAT gateway and a route from the private subnet, then run a package update on the private instance.
  2. 2Delete the NAT gateway and release its Elastic IP straight after the lab.
  3. 3Learn why production setups use one NAT gateway per Availability Zone.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge