Skip to main content

Internet Gateway and NAT Gateway

Internet Gateway (IGW)​

What is an Internet Gateway?​

An Internet Gateway (IGW) is the entry/exit point between your VPC and the Internet. Without an IGW, your VPC is completely isolated from the Internet.

┌─────────────────────────────────────────────────────────────┐
│ INTERNET GATEWAY │
├─────────────────────────────────────────────────────────────┤
│ │
│ INTERNET │
│ │ │
│ │ │
│ ┌──────▼──────┐ │
│ │ Internet │ │
│ │ Gateway │ │
│ │ (IGW) │ │
│ └──────┬──────┘ │
│ │ │
│ ┌──────────────────────┼──────────────────────┐ │
│ │ VPC │ │ │
│ │ │ │ │
│ │ ┌──────────────────▼───────────────────┐ │ │
│ │ │ │ │ │
│ │ │ Resources with Public IP │ │ │
│ │ │ ┌─────────┐ │ │ │
│ │ │ │ EC2 │ │ │ │
│ │ │ │ (public)│ │ │ │
│ │ │ └─────────┘ │ │ │
│ │ │ │ │ │
│ │ └───────────────────────────────────────┘ │ │
│ │ │ │
│ └──────────────────────────────────────────────┘ │
│ │
│ Characteristics: │
│ • Highly available (managed by AWS) │
│ • No bandwidth limit │
│ • 1 IGW per VPC maximum │
│ • Free (no charge for the IGW itself) │
│ │
└─────────────────────────────────────────────────────────────┘

How IGW Works​

┌─────────────────────────────────────────────────────────────┐
│ IGW OPERATION │
├─────────────────────────────────────────────────────────────┤
│ │
│ OUTBOUND TRAFFIC (Instance → Internet) │
│ ──────────────────────────────────── │
│ │
│ ┌─────────┐ ┌─────┐ ┌─────────┐ │
│ │Instance │ ──────► │ IGW │ ──────► │Internet │ │
│ │10.0.1.5 │ │ │ │ │ │
│ └─────────┘ └─────┘ └─────────┘ │
│ │ │
│ NAT Translation │
│ 10.0.1.5 → 52.1.2.3 │
│ (Private IP → Public IP) │
│ │
│ INBOUND TRAFFIC (Internet → Instance) │
│ ──────────────────────────────────── │
│ │
│ ┌─────────┐ ┌─────┐ ┌─────────┐ │
│ │Instance │ ◄────── │ IGW │ ◄────── │Internet │ │
│ │10.0.1.5 │ │ │ │52.1.2.3 │ │
│ └─────────┘ └─────┘ └─────────┘ │
│ │ │
│ NAT Translation │
│ 52.1.2.3 → 10.0.1.5 │
│ (Public IP → Private IP) │
│ │
└─────────────────────────────────────────────────────────────┘

Create and Attach an IGW​

# 1. Create the Internet Gateway
aws ec2 create-internet-gateway \
--tag-specifications 'ResourceType=internet-gateway,Tags=[{Key=Name,Value=my-igw}]'

# 2. Attach to the VPC
aws ec2 attach-internet-gateway \
--internet-gateway-id igw-xxx \
--vpc-id vpc-xxx

# 3. Add the route in the public Route Table
aws ec2 create-route \
--route-table-id rtb-xxx \
--destination-cidr-block 0.0.0.0/0 \
--gateway-id igw-xxx

Conditions for Internet Access (Instance)​

┌─────────────────────────────────────────────────────────────┐
│ CONDITIONS FOR INTERNET ACCESS │
├─────────────────────────────────────────────────────────────┤
│ │
│ For an instance to access the Internet, it needs: │
│ │
│ ☐ 1. VPC has an attached Internet Gateway │
│ │
│ ☐ 2. Subnet's Route Table has a route to IGW │
│ Destination: 0.0.0.0/0 → Target: igw-xxx │
│ │
│ ☐ 3. Instance has a public IP or Elastic IP │
│ (otherwise no return possible) │
│ │
│ ☐ 4. Security Group allows outbound traffic │
│ (default: all allowed) │
│ │
│ ☐ 5. NACL allows traffic (inbound AND outbound) │
│ (default: all allowed) │
│ │
│ If ANY ONE is missing = No Internet access! │
│ │
└─────────────────────────────────────────────────────────────┘

NAT Gateway​

What is a NAT Gateway?​

A NAT Gateway allows instances in private subnets to access the Internet (outbound only) without being accessible from the Internet.

┌─────────────────────────────────────────────────────────────┐
│ NAT GATEWAY │
├─────────────────────────────────────────────────────────────┤
│ │
│ INTERNET │
│ │ │
│ ┌──────▼──────┐ │
│ │ IGW │ │
│ └──────┬──────┘ │
│ │ │
│ ┌──────────────────────┼──────────────────────┐ │
│ │ VPC │ │ │
│ │ │ │ │
│ │ ┌──────────────────▼───────────────────┐ │ │
│ │ │ PUBLIC SUBNET │ │ │
│ │ │ │ │ │
│ │ │ ┌─────────────┐ │ │ │
│ │ │ │ NAT Gateway │ │ │ │
│ │ │ │ (52.1.2.99) │ │ │ │
│ │ │ └──────┬──────┘ │ │ │
│ │ │ │ │ │ │
│ │ └──────────────────┼────────────────────┘ │ │
│ │ │ │ │
│ │ ┌──────────────────▼───────────────────┐ │ │
│ │ │ PRIVATE SUBNET │ │ │
│ │ │ │ │ │
│ │ │ ┌─────────┐ │ │ │
│ │ │ │ EC2 │ │ │ │
│ │ │ │(10.0.11.5) │ │ │
│ │ │ └─────────┘ │ │ │
│ │ │ │ │ │
│ │ │ Route: 0.0.0.0/0 → nat-xxx │ │ │
│ │ │ │ │ │
│ │ └───────────────────────────────────────┘ │ │
│ │ │ │
│ └──────────────────────────────────────────────┘ │
│ │
│ Flow: Private EC2 → NAT GW → IGW → Internet │
│ No initiated return from the Internet! │
│ │
└─────────────────────────────────────────────────────────────┘

NAT Gateway vs NAT Instance​

CharacteristicNAT GatewayNAT Instance
AvailabilityHigh (AWS managed)You manage
BandwidthUp to 45 GbpsDepends on type
MaintenanceAWSYou
CostMore expensiveLess expensive
Recommendation✅ Production❌ Legacy

Create a NAT Gateway​

# 1. Allocate an Elastic IP
aws ec2 allocate-address --domain vpc

# 2. Create the NAT Gateway (in PUBLIC subnet)
aws ec2 create-nat-gateway \
--subnet-id subnet-public-xxx \
--allocation-id eipalloc-xxx \
--tag-specifications 'ResourceType=natgateway,Tags=[{Key=Name,Value=nat-gw-1a}]'

# 3. Add route in PRIVATE Route Table
aws ec2 create-route \
--route-table-id rtb-private-xxx \
--destination-cidr-block 0.0.0.0/0 \
--nat-gateway-id nat-xxx

NAT Gateway Cost​

┌─────────────────────────────────────────────────────────────┐
│ NAT GATEWAY COST │
├─────────────────────────────────────────────────────────────┤
│ │
│ HOURLY CHARGES │
│ • ~$0.045/hour per NAT Gateway │
│ • ~$32/month if active 24/7 │
│ │
│ DATA CHARGES │
│ • ~$0.045/GB processed │
│ │
│ Monthly example: │
│ • 1 NAT Gateway: $32 │
│ • 100 GB transfer: $4.50 │
│ • Total: $36.50/month │
│ │
│ ⚠️ SAVING TIP: │
│ • For dev/test: use NAT Instance (cheaper) │
│ • Or: VPC Endpoints for S3/DynamoDB (free) │
│ • Or: Public subnet for instances needing Internet │
│ │
└─────────────────────────────────────────────────────────────┘

NAT Gateway High Availability​

┌─────────────────────────────────────────────────────────────┐
│ NAT GATEWAY HIGH AVAILABILITY │
├─────────────────────────────────────────────────────────────┤
│ │
│ One NAT Gateway = One AZ │
│ For HA, create one NAT GW per AZ │
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ VPC │ │
│ │ │ │
│ │ ┌───────────────┐ ┌───────────────┐ │ │
│ │ │ AZ 1a │ │ AZ 1b │ │ │
│ │ │ │ │ │ │ │
│ │ │ ┌───────────┐ │ │ ┌───────────┐ │ │ │
│ │ │ │ NAT GW 1a │ │ │ │ NAT GW 1b │ │ │ │
│ │ │ └─────┬─────┘ │ │ └─────┬─────┘ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ ┌─────▼─────┐ │ │ ┌─────▼─────┐ │ │ │
│ │ │ │ Private │ │ │ │ Private │ │ │ │
│ │ │ │ Subnet 1a │ │ │ │ Subnet 1b │ │ │ │
│ │ │ │ │ │ │ │ │ │ │ │
│ │ │ │ rtb: → │ │ │ │ rtb: → │ │ │ │
│ │ │ │ nat-1a │ │ │ │ nat-1b │ │ │ │
│ │ │ └───────────┘ │ │ └───────────┘ │ │ │
│ │ │ │ │ │ │ │
│ │ └───────────────┘ └───────────────┘ │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
│ If NAT GW 1a fails → Private subnets 1a lose Internet │
│ But private subnets 1b continue to function │
│ │
└─────────────────────────────────────────────────────────────┘

Elastic IP​

What is an Elastic IP?​

An Elastic IP is a static public IPv4 address that you can associate with your resources.

┌─────────────────────────────────────────────────────────────┐
│ ELASTIC IP │
├─────────────────────────────────────────────────────────────┤
│ │
│ Normal Public IP Elastic IP │
│ ──────────────────── ────────── │
│ │
│ • Assigned by AWS • You allocate │
│ • Changes if Stop/Start • Remains fixed │
│ • Free if used • Free if associated │
│ • Lost if terminated • Retained after terminate │
│ │
│ Elastic IP use cases: │
│ • Server that needs to keep the same IP │
│ • NAT Gateway (mandatory) │
│ • Quick failover between instances │
│ │
│ ⚠️ COST: Unassociated EIP = ~$3.60/month │
│ (AWS charges for unused EIPs!) │
│ │
└─────────────────────────────────────────────────────────────┘

Complete Diagram​

┌─────────────────────────────────────────────────────────────────────────┐
│ COMPLETE NETWORK ARCHITECTURE │
├─────────────────────────────────────────────────────────────────────────┤
│ │
│ INTERNET │
│ │ │
│ ┌──────▼──────┐ │
│ │ IGW │ │
│ └──────┬──────┘ │
│ │ │
│ ┌──────────────────────────────┼──────────────────────────────┐ │
│ │ VPC │ (10.0.0.0/16) │ │
│ │ │ │ │
│ │ ┌──────────────────────────┴────────────────────────────┐ │ │
│ │ │ PUBLIC SUBNETS │ │ │
│ │ │ Route: 0.0.0.0/0 → IGW │ │ │
│ │ │ │ │ │
│ │ │ ┌─────────────────┐ ┌─────────────────┐ │ │ │
│ │ │ │ Public 1a │ │ Public 1b │ │ │ │
│ │ │ │ 10.0.1.0/24 │ │ 10.0.2.0/24 │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ ┌───────────┐ │ │ ┌───────────┐ │ │ │ │
│ │ │ │ │ NAT GW │ │ │ │ NAT GW │ │ │ │ │
│ │ │ │ │ 1a │ │ │ │ 1b │ │ │ │ │
│ │ │ │ └─────┬─────┘ │ │ └─────┬─────┘ │ │ │ │
│ │ │ └────────┼────────┘ └────────┼────────┘ │ │ │
│ │ │ │ │ │ │ │
│ │ └───────────┼──────────────────────────┼─────────────────┘ │ │
│ │ │ │ │ │
│ │ ┌───────────┼──────────────────────────┼─────────────────┐ │ │
│ │ │ │ PRIVATE SUBNETS │ │ │ │
│ │ │ │ Route: 0.0.0.0/0 → NAT │ │ │ │
│ │ │ │ │ │ │ │
│ │ │ ┌────────▼────────┐ ┌────────▼────────┐ │ │ │
│ │ │ │ Private 1a │ │ Private 1b │ │ │ │
│ │ │ │ 10.0.11.0/24 │ │ 10.0.12.0/24 │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ ┌───────────┐ │ │ ┌───────────┐ │ │ │ │
│ │ │ │ │ EC2 │ │ │ │ EC2 │ │ │ │ │
│ │ │ │ │ App │ │ │ │ App │ │ │ │ │
│ │ │ │ └───────────┘ │ │ └───────────┘ │ │ │ │
│ │ │ └─────────────────┘ └─────────────────┘ │ │ │
│ │ │ │ │ │
│ │ └────────────────────────────────────────────────────────┘ │ │
│ │ │ │
│ └───────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────┘

Summary​

ComponentFunctionCost
IGWBidirectional Internet accessFree
NAT GatewayInternet egress for private~$32/month + data
Elastic IPFixed public IPFree if used
NAT InstanceNAT alternative (legacy)EC2 cost

Validation Quiz​

  1. What is the purpose of an Internet Gateway?
  2. Why is a NAT Gateway necessary for private subnets?
  3. In what type of subnet should the NAT Gateway be?
  4. How much does an unused Elastic IP cost?
  5. How to ensure HA for NAT Gateways?

Next section: Lab: VPC Diagram