Engineering Notes
Home/DevOps & Cloud/AWS/Internet Gateway and NAT Gateway
DevOps & Cloud · AWS

Internet Gateway and NAT Gateway

Internet gateways let traffic in and out of public subnets; NAT gateways let private instances reach out without being reachable. Compare NAT layouts for availability and cost, and cut NAT bills with VPC endpoints.

☁️ DevOps & Cloud● Beginner⏱ 4 min read▶ 2 interactive🛠️ 1 exercise🎤 5 interview Q&As

🎯 What you'll learn

  • Internet gateway vs NAT gateway: two directions
  • The standard public/private layout
  • How NAT translates addresses
  • Costs, alternatives (VPC endpoints), Elastic IPs

📚 Before you start

1Two Directions, Two Gateways

Internet Gateway (IGW)NAT Gateway
purposetwo-way internet access for resources with public IPsoutbound-only internet access for resources in private subnets
inbound from internetyes (if SGs/NACLs allow)no — unsolicited inbound connections are impossible
attached tothe VPC (one per VPC)a public subnet in one AZ, with an Elastic IP
route that uses itpublic subnet: 0.0.0.0/0 → igw-…private subnet: 0.0.0.0/0 → nat-…
costfree (data transfer still billed)hourly charge plus per-GB processing
scaling/HAhorizontally scaled, redundant by AWSredundant within its AZ; deploy one per AZ

What makes a subnet "public" is only its route table having a route to an IGW (02-vpc_subnets_route_tables §3).

2The Standard Layout

text
VPC 10.0.0.0/16                                         IGW
┌───────────────────────── AZ a ─────────┐ ┌──────── AZ b ─────────┐ ▲
│ public 10.0.0.0/24   [ALB node] [NAT-a]│ │ public 10.0.1.0/24 [NAT-b]│ │  0.0.0.0/0 → igw
│ private 10.0.10.0/24 [app] [app]       │ │ private 10.0.11.0/24 [app]│   0.0.0.0/0 → nat-a (AZ a) / nat-b (AZ b)
│ data 10.0.20.0/24    [RDS primary]     │ │ data 10.0.21.0/24 [standby]│  no internet route at all
└────────────────────────────────────────┘ └──────────────────────────┘
  • Public subnets: load balancers, NAT gateways (and almost nothing else).
  • Private app subnets: route 0.0.0.0/0 to the NAT in the same AZ — if one AZ fails, the others keep outbound access, and you avoid cross-AZ data charges.
  • Data subnets: no route to the internet at all.

3How NAT Works Here

An app instance 10.0.10.25 calls api.stripe.com:

text
10.0.10.25:51514 → (route 0.0.0.0/0 → nat-a) → NAT rewrites source to its Elastic IP 3.120.4.9:40123
→ IGW → internet → reply to 3.120.4.9:40123 → NAT looks up its translation table → 10.0.10.25:51514

The whole private fleet appears to the internet as the NAT's Elastic IP — useful for partner allow-lists ("allow our egress IP") — and nobody on the internet can initiate a connection to the private instances.

4Costs and Alternatives

NAT gateway data processing charges add up fast when private workloads pull lots of data from AWS services:

  • Gateway VPC endpoints for S3 and DynamoDB are free and route that traffic privately — always add them.
  • Interface VPC endpoints (PrivateLink) for ECR, CloudWatch Logs, SSM, Secrets Manager, STS, SQS… keep traffic off the NAT (hourly + per-GB, but often cheaper than NAT for heavy users, and no internet path at all).
  • IPv6: public IPv6 addresses + an egress-only internet gateway give outbound-only behaviour without NAT.
  • Dev/test: one NAT for all AZs (accepting the AZ dependency) to save cost.

5Elastic IPs and Public IPs

  • Auto-assigned public IPv4 addresses change on stop/start; Elastic IPs are static and can be moved between resources. AWS charges for every public IPv4 address (in use or idle).
  • Only resources that genuinely need to be reachable (load balancers, NAT gateways) should have public IPs.
Under the HoodWhy an IGW Isn't a Box▾

An internet gateway isn't a device sitting in your VPC that traffic queues through — it's a logical attachment to AWS's horizontally scaled edge, so it has no bandwidth limit and no availability-zone failure mode. It performs 1:1 NAT between an instance's private IP and its public IPv4 address: instances never see their public IP on their own interface (ip addr shows only 10.0.x.x); the mapping is applied by the VPC's virtual routing at the edge. A NAT gateway, by contrast, is a managed many-to-one (port address translation) service deployed into one subnet: each gateway supports up to ~55,000 simultaneous connections per destination (IP, port, protocol) per assigned IP — exceeded when thousands of workers hammer the same API endpoint, surfacing as ErrorPortAllocation; adding secondary IPs to the NAT or spreading destinations fixes it.

A VPC with public subnets holding the ALB and one NAT gateway per availability zone routed to the internet gateway, private app subnets routing outbound traffic to their own AZ's NAT, data subnets with no internet route, and VPC endpoints for S3 and other AWS services
A VPC with public subnets holding the ALB and one NAT gateway per availability zone routed to the internet gateway, private app subnets routing outbound traffic to their own AZ's NAT, data subnets with no internet route, and VPC endpoints for S3 and other AWS services

⚠️ Common Pitfalls

  1. One NAT for all AZs in production — an AZ outage cuts outbound access everywhere, plus cross-AZ charges.
  2. Putting app servers in public subnets "to reach the internet".
  3. No S3/DynamoDB gateway endpoints — paying NAT processing for internal AWS traffic.
  4. Expecting inbound connections through a NAT — impossible by design.
  5. Data tier with a route to the internet.
  6. NAT port exhaustion for high-concurrency calls to one destination.

📋 Quick Reference

NeedUse
internet-facing resourcepublic subnet + route to IGW + public IP
private outbound internetNAT gateway per AZ, route from private subnets
private access to S3/DynamoDBgateway VPC endpoint (free)
private access to other AWS APIsinterface endpoints (PrivateLink)
IPv6 outbound onlyegress-only internet gateway
static egress IP for partnersNAT gateway Elastic IP

🧠 Key takeaways

  • An IGW enables two-way internet access for resources with public IPs in public subnets.
  • A NAT gateway (in a public subnet) gives private resources outbound-only access.
  • Production: one NAT gateway per AZ; NAT charges per hour and per GB processed.
  • Use VPC endpoints (S3/DynamoDB gateway endpoints are free) to keep AWS traffic off NAT.

🎤 Interview Q&A

Questions interviewers ask about this topic. Answer out loud first, then open a card to compare.

Q1Internet gateway vs NAT gateway?core▾

An internet gateway enables two-way internet access for resources with public IPs in public subnets. A NAT gateway (in a public subnet) lets private-subnet resources make outbound connections without being reachable from the internet.

Follow-ups interviewers ask
  • Does an IGW cost money? No; NAT gateways charge hourly plus per GB processed.
Q2How do you make NAT highly available?core▾

A NAT gateway lives in one AZ; deploy one per AZ and route each private subnet to its own AZ's NAT gateway so an AZ failure doesn't cut other AZs' egress.

Follow-ups interviewers ask
  • Cost trade-off? One NAT saves money but creates a single point of failure and cross-AZ charges.
Q3How do you reduce NAT gateway costs?senior▾

Use VPC gateway endpoints for S3/DynamoDB and interface endpoints for heavy AWS API traffic, cache dependencies (container images via ECR pull-through), and avoid routing large data through NAT.

Follow-ups interviewers ask
  • Typical surprise bill? Containers pulling images or data from the internet through NAT.
Q4What's an egress-only internet gateway?core▾

The IPv6 equivalent of NAT for outbound-only access: IPv6 addresses are globally routable, so it allows outbound connections while blocking inbound ones.

Follow-ups interviewers ask
  • Why no NAT for IPv6? Address space is plentiful; filtering replaces translation.
Q5How do instances in private subnets get updates without NAT?core▾

Via VPC endpoints (SSM, S3 for repositories), a proxy, or baked AMIs/container images built elsewhere.

Follow-ups interviewers ask
  • Why avoid internet access entirely? Smaller attack surface for sensitive workloads.

🛠️ Practice exercises

Each exercise is a file in this topic folder. Code exercises are self-checking: run them and make the checks pass.

✅ Check yourself

Answer the questions — feedback is instant.

1. Can the internet initiate a connection to an instance behind a NAT gateway?
Inbound needs an IGW + public IP or a load balancer.
2. Why one NAT gateway per AZ?
Availability.
3. Cheapest way to let private subnets reach S3?
Free and private.
4. Where does a NAT gateway live?
It needs an IGW route.