0Pricing
AWS Solutions Architect · Lesson

NAT Gateway and Private Subnets

Allow private subnet resources to reach the internet without being directly reachable using a managed NAT gateway.

NAT Gateway and Private Subnets is a free AWS Solutions Architect lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AWS Solutions Architect learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Private Subnet Internet Problem

Resources in private subnets have only private IP addresses and no route to an Internet Gateway, so they cannot initiate outbound connections to the internet. But they often need to reach the internet: to download OS patches, pull Docker images, call third-party APIs, or update software. The solution is to route private subnet traffic through a device that has internet access and performs NAT (Network Address Translation), hiding the private IPs behind a public IP. AWS provides two options: NAT Gateway (managed) and a self-managed NAT Instance (legacy).

NAT Gateway: Managed Outbound NAT

A NAT Gateway is a fully managed, highly available service that enables instances in private subnets to initiate outbound connections to the internet while blocking unsolicited inbound traffic. It lives in a public subnet, has an Elastic IP, and automatically scales up to 100 Gbps bandwidth. You never need to patch, scale, or manage the underlying infrastructure. The NAT Gateway translates the private source IP of outbound packets to its own EIP, then returns responses to the original private IP.

# Create a NAT Gateway in the public subnet
aws ec2 allocate-address --domain vpc  # Get an EIP
aws ec2 create-nat-gateway \
  --subnet-id subnet-public-1a \
  --allocation-id eipalloc-12345678 \
  --tag-specifications 'ResourceType=natgateway,Tags=[{Key=Name,Value=nat-gw-1a}]'

Routing Private Subnet Traffic Through NAT

After creating a NAT Gateway, add a route to the private subnet's route table: destination 0.0.0.0/0 → NAT Gateway ID. This tells all internet-bound traffic from private subnet instances to go to the NAT Gateway, which then forwards it through the Internet Gateway to the internet. The route table still has the local VPC route (e.g., 10.0.0.0/16 → local) for internal traffic. Without this route entry, private subnet instances still have no internet access even with a NAT Gateway deployed.

# Add route in private subnet's route table pointing to NAT gateway
aws ec2 create-route \
  --route-table-id rtb-private-app-1a \
  --destination-cidr-block 0.0.0.0/0 \
  --nat-gateway-id nat-12345678

Multi-AZ NAT Gateway Architecture

A NAT Gateway is AZ-specific—it only handles traffic from subnets in the same AZ. For high availability, deploy one NAT Gateway per AZ and create AZ-specific private route tables: the private route table in AZ-a routes 0.0.0.0/0 to the NAT-GW in AZ-a; the private route table in AZ-b routes to the NAT-GW in AZ-b. This avoids inter-AZ data transfer charges and ensures that if AZ-a fails, the instances in AZ-b (with their own NAT) continue to have internet access.

# Architecture: two NAT gateways, two private route tables
# NAT-GW-1a in subnet-public-1a  --> rtb-private-1a
# NAT-GW-1b in subnet-public-1b  --> rtb-private-1b

# Associate private-1a subnets with rtb-private-1a (NAT-GW-1a)
aws ec2 associate-route-table \
  --route-table-id rtb-private-1a \
  --subnet-id subnet-private-app-1a

NAT Gateway vs NAT Instance

A NAT Instance is a self-managed EC2 instance running a NAT AMI—the legacy approach before NAT Gateways existed. NAT Instances require you to: disable source/destination check on the EC2 instance, manage bandwidth and scaling, patch the OS, and handle failures. They are cheaper for very low-bandwidth use cases and can be used as bastion hosts. NAT Gateways are the recommended choice for production: fully managed, automatically scaled, 99.99% availability SLA within an AZ, and no patching required. The exam has historically tested this comparison.

# For NAT Instance: disable source/dest check (required!)
aws ec2 modify-instance-attribute \
  --instance-id i-nat-instance-id \
  --no-source-dest-check

NAT Gateway Pricing

NAT Gateway pricing has two components: an hourly charge per NAT Gateway (approximately $0.045/hour in us-east-1, around $32/month), and a per-GB data processing charge (~$0.045/GB in us-east-1). For workloads that transfer large amounts of data through NAT (e.g., pulling large container images, bulk downloads), the data processing cost can dominate. Optimise NAT costs by: routing S3 and DynamoDB traffic through VPC Gateway Endpoints (bypasses NAT entirely, free), and routing AWS API calls through VPC Interface Endpoints instead of through NAT.

Private Subnet Design for RDS

Database instances in private subnets should not route through the NAT Gateway—they have no business initiating connections to the internet. Databases only need to receive connections from the application tier (within the VPC) and potentially reach AWS services like S3 (for exports/imports) via a VPC endpoint. For database patching and updates, RDS is a managed service so you don't apply OS patches directly. For self-managed databases on EC2, use Systems Manager Patch Manager, which can patch via SSM without internet access if you have the SSM VPC endpoint configured.

NAT Gateway and Security

NAT Gateway provides stateful outbound-only NAT: it allows initiated outbound connections and their responses, but blocks all unsolicited inbound traffic. It is not a firewall—it does not inspect or filter traffic content. For outbound traffic filtering (e.g., allow only specific destination domains or IPs from private subnets), you need additional controls: AWS Network Firewall deployed in the VPC, or a third-party firewall appliance behind a Gateway Load Balancer. Security groups and NACLs still apply to instances in private subnets regardless of NAT.

Private NAT Gateway

In addition to public NAT Gateways (with EIPs for internet access), AWS supports private NAT Gateways without an EIP. Private NAT Gateways are used to translate private IP addresses for traffic between VPCs or between a VPC and on-premises networks when CIDR ranges overlap. For example, if VPC-A (10.0.0.0/16) needs to communicate with an on-premises network that also uses 10.0.0.0/16, a private NAT Gateway in VPC-A translates the source IPs to a non-overlapping range before routing through a Transit Gateway to on-premises.

Monitoring NAT Gateway Metrics

Monitor NAT Gateway health and performance through CloudWatch metrics. Key metrics: ActiveConnectionCount (current active TCP connections through the NAT), BytesInFromDestination / BytesInFromSource (data transfer in both directions), PacketDropCount (dropped packets—indicates bandwidth limits or errors), and ErrorPortAllocation (the NAT exhausted port allocations—may indicate port exhaustion from too many connections). Each NAT Gateway supports up to 55,000 simultaneous connections per unique destination; beyond that, connections may be dropped.

# Get NAT Gateway active connection count
aws cloudwatch get-metric-statistics \
  --namespace AWS/NATGateway \
  --metric-name ActiveConnectionCount \
  --dimensions Name=NatGatewayId,Value=nat-12345678 \
  --start-time 2024-01-01T00:00:00Z \
  --end-time 2024-01-01T01:00:00Z \
  --period 300 --statistics Maximum

When Private Subnets Do Not Need NAT

Not all private subnet workloads need a NAT Gateway. Consider the access pattern: if instances only need to communicate with other VPC resources and AWS services accessible via VPC endpoints (S3, DynamoDB, SSM, ECR, Secrets Manager), you can eliminate the NAT Gateway entirely—saving ~$32/month per AZ plus data processing charges. This is the ideal pattern for fully private microservices that use VPC Interface Endpoints for all AWS service access. Evaluate each service your instances call and check if a free Gateway Endpoint or paid Interface Endpoint can replace NAT.

Quick Check

Test your understanding of AWS Solutions Architect (SAA-C03) concepts from this lesson.

Lesson Recap

In this lesson you learned: NAT Gateways enable outbound internet access for private subnet instances by translating their private IPs to an Elastic IP, you must add a 0.0.0.0/0 route in the private subnet route table pointing to the NAT Gateway, and deploy one NAT Gateway per AZ to achieve high availability and avoid cross-AZ data charges. Next up we compare Network ACLs and Security Groups for layered VPC defence.

Frequently asked questions

Is the “NAT Gateway and Private Subnets” lesson free?

Yes — the full text of “NAT Gateway and Private Subnets” is free to read here on the web, and the AWS Solutions Architect course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AWS Solutions Architect course, upgrade to CoddyKit PRO.

What will I learn in “NAT Gateway and Private Subnets”?

Allow private subnet resources to reach the internet without being directly reachable using a managed NAT gateway. You practise AWS Solutions Architect with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start AWS Solutions Architect?

No prior experience is required. AWS Solutions Architect on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “NAT Gateway and Private Subnets” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this AWS Solutions Architect lesson?

Yes. Every AWS Solutions Architect lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. VPC Architecture and CIDR Blocks
  2. Internet Gateway and Route Tables
  3. NAT Gateway and Private Subnets
  4. Network ACLs vs Security Groups
← Back to AWS Solutions Architect