AWS understand · networking · 21

NAT Gateway

A hand-drawn animated video with a voice and subtitles. Every fact in it comes from the official AWS documentation (listed at the end).

TL;DR
  • A NAT gateway lets servers in a private subnet connect to things outside the VPC, but outside services cannot start a connection to those servers.
  • NAT = network address translation = swapping the sender address on the way out, and swapping it back on the reply.
  • A public NAT gateway lives in a public subnet and must get an Elastic IP address (a fixed, public IPv4 address) when you create it.
  • Routes: private subnet 0.0.0.0/0 → nat-gateway-id; public subnet 0.0.0.0/0 → internet-gateway-id.
  • A standard (zonal) NAT gateway lives in one Availability Zone. For resilience, make one in each zone and route each zone to its own one. A regional NAT gateway also exists and spreads across zones by itself.
0:00

Video file: aws-understand/06-networking/21-nat-gateway/video.mp4

The ideas

01The picture, part by part

Same picture as the earlier videos AWS docs

AWS wordIn the pictureWhat AWS docs say
VPCGated societyA virtual network dedicated to your AWS account.
Private subnetA building with no road to the main gateDoes not have a direct route to an internet gateway. Needs a NAT device to access the public internet.
Public subnetA building with a road to the main gateHas a direct route to an internet gateway.
Internet gatewayThe society's main gateLets instances connect to the internet through the Amazon EC2 network edge.
NAT gatewayA reception / post desk in the public buildingA Network Address Translation service: private instances can connect out, outside services cannot start a connection in.
Elastic IP addressThe desk's fixed public return addressA static, public IPv4 address.

Source: how-it-works.html, configure-subnets.html, vpc-nat-gateway.html, vpc-eips.html

02Out yes, in no

The one rule to remember AWS docs

AWS docs: "You can use a NAT gateway so that instances in a private subnet can connect to services outside your VPC but external services can't initiate a connection with those instances." Also: "Connections must always be initiated from within the VPC containing the NAT gateway."

In everyday words
Residents can post letters out through the desk and get replies. A stranger outside cannot send a letter that starts a new conversation with a resident.

Note from AWS: the word NAT follows common IT use; the real job of a NAT device is both address translation and port address translation (PAT).

Source: vpc-nat-gateway.html, vpc-nat.html

03Where it lives and the routes

Public subnet, Elastic IP, two route tables AWS docs

"You create a public NAT gateway in a public subnet and must associate an Elastic IP address with the NAT gateway at creation." A route table is a set of rules (routes) that decide where network traffic from your VPC goes.

Route table ofDestinationTargetMeaning
Private subnetVPC CIDRlocalTalk to other instances in the VPC
Private subnet0.0.0.0/0nat-gateway-idAll other traffic goes to the NAT gateway
Public subnetVPC CIDRlocalTalk to other instances in the VPC
Public subnet0.0.0.0/0internet-gateway-idAll other traffic goes to the internet gateway, so the NAT gateway can reach the internet

Source: vpc-nat-gateway.html, nat-gateway-scenarios.html ("Routing"), how-it-works.html ("Route tables")

04The two address swaps

NAT gateway first, then the internet gateway AWS docs

StepWhoWhat changes
1NAT gatewayInstance's private IPv4 address → NAT gateway's private IPv4 address
2Internet gatewayNAT gateway's private IPv4 address → the Elastic IP address of the NAT gateway
3NAT gateway (reply)Translates the address back to the original source IP address

The example addresses in the video (10.0.2.15, 10.0.1.20) are made up for the picture.

Source: vpc-nat-gateway.html

05One per zone

Zonal vs regional AWS docs

"Each NAT gateway is created in a specific Availability Zone and implemented with redundancy in that zone." "If you have resources in multiple Availability Zones and they share one NAT gateway, and if the NAT gateway's Availability Zone is down, resources in the other Availability Zones lose internet access. To improve resiliency, create a NAT gateway in each Availability Zone, and configure your routing to ensure that resources use the NAT gateway in the same Availability Zone."

AWS also offers regional NAT gateways: "Unlike standard NAT gateways (referred to as zonal NAT gateways), which operate in a single Availability Zone, regional NAT gateways follow your workloads." A regional NAT gateway does not need a public subnet, and does not support private NAT.

Source: nat-gateway-basics.html, nat-gateways-regional.html

06Public vs private NAT gateway

Connectivity type AWS docs

TypeLets private instances reachElastic IP?Note
Public (default)The internet (through the internet gateway)Yes, required at creationLives in a public subnet
PrivateOther VPCs or your on-premises network (your own data centre)No, cannot have oneIf you route its traffic to the internet gateway, the internet gateway drops it

Source: vpc-nat-gateway.html

07Guards, speed and cost

Security groups, network ACLs, bandwidth, pricing AWS docs

FactWhat AWS docs say
Security group (guard at the flat door)No security group on a NAT gateway. Put security groups on your instances.
Network ACL (guard at the building gate)You can use a network ACL to control traffic to and from the NAT gateway's subnet. NAT gateways use ports 1024-65535.
Speed (bandwidth = how much data can flow)5 Gbps, automatically scales up to 100 Gbps.
CostCharged for each hour the NAT gateway is available and each gigabyte of data it processes.

Source: nat-gateway-basics.html, nat-gateway-pricing.html

08NAT gateway vs NAT instance

AWS recommends the gateway AWS docs

AttributeNAT gatewayNAT instance (your own EC2 server)
MaintenanceManaged by AWS, no maintenance for youManaged by you, for example software updates and OS patches
BandwidthUp to 100 GbpsDepends on the instance type
AvailabilityBuilt with redundancy in each zoneYou use a script to manage failover
Security groupsNoYes

AWS: "We recommend that you use NAT gateways because they provide better availability and bandwidth and require less effort on your part to administer."

Source: vpc-nat-comparison.html, vpc-nat.html

Reference

—Sources

WhatWhere (official AWS docs)
NAT devices: what NAT does, gateway vs instancevpc/latest/userguide/vpc-nat.html
NAT gateways: public and private, address mappingvpc/latest/userguide/vpc-nat-gateway.html
NAT gateway basics: zones, bandwidth, security groups, ACLsvpc/latest/userguide/nat-gateway-basics.html
Use cases: route tablesvpc/latest/userguide/nat-gateway-scenarios.html
Regional NAT gatewaysvpc/latest/userguide/nat-gateways-regional.html
Compare NAT gateways and NAT instancesvpc/latest/userguide/vpc-nat-comparison.html
NAT gateway pricingvpc/latest/userguide/nat-gateway-pricing.html
Elastic IP addressesvpc/latest/userguide/vpc-eips.html
How Amazon VPC works: route tables, internet accessvpc/latest/userguide/how-it-works.html
Subnet types: public and privatevpc/latest/userguide/configure-subnets.html