A NAT Gateway (Network Address Translation Gateway) is a managed AWS service that allows resources in a private subnet to connect out to the internet (for OS updates, patches, or external APIs) while blocking anyone on the internet from initiating connections back to them.
How It Works (Step-by-Step)
Because servers in a private subnet only have private IPs (like
10.0.11.25), the public internet cannot route traffic back to them directly. The NAT Gateway acts as a middleman:- Outbound Request: Your private server (
10.0.11.25) sends a request (e.g.,apt-get update) to0.0.0.0/0, which its route table points to the NAT Gateway. - Translation: The NAT Gateway sits inside a Public Subnet and holds an Elastic IP (e.g.,
54.12.34.56). It swaps your server's private IP for its own Elastic IP. - Internet Access: It sends the request through the Internet Gateway to the internet.
- Return Traffic: When the response comes back to
54.12.34.56, the NAT Gateway remembers who asked for it and forwards the data back to10.0.11.25. - Inbound Block: If an external hacker tries to connect to
54.12.34.56out of the blue, the NAT Gateway drops it because no private server started that conversation.
NAT Gateway Placement
A working NAT Gateway setup requires two separate route tables:
A working NAT Gateway setup requires two separate route tables:
- NAT Gateway Placement: Must be created inside a Public Subnet and attached to an Elastic IP.
- Private Subnet Route Table:
10.0.0.0/16->local0.0.0.0/0->nat-xxxx(NAT Gateway)
- Public Subnet Route Table (where the NAT Gateway lives):
10.0.0.0/16->local0.0.0.0/0->igw-xxxx(Internet Gateway)
Key Things to Know
- One-Way Street: Outbound connections are allowed; unsolicited inbound connections are blocked.
- Availability Zone Specific: A NAT Gateway lives in a single Availability Zone (AZ). For high availability in production, deploy one NAT Gateway per AZ so an outage in one zone doesn't cut internet access for private servers in another.
- Cost Consideration: AWS charges an hourly rate (around $0.045/hr depending on region) plus per-GB data processing fees—making it one of the most common surprise charges in small or test AWS accounts if left running.
