AWS VPC with public and private subnets and a NAT gateway
A network across two availability zones where servers and databases sit in private subnets that the internet cannot reach directly, and still have a way out for updates and API calls.
Internet
Public subnet
NAT gateway
Private subnet
How it works
Why not put everything in the default VPC
The default VPC has only public subnets, so anything launched there can be given a public address and reached from the internet. Most of what you run, such as application servers and databases, never needs to accept a connection from outside. Private subnets remove that path altogether instead of relying on every security group being right.
Choose an address range
The VPC takes one range, 10.0.0.0/16 by default, and the pattern cuts it into four subnets with room for more. The mistake people make is reusing the same range in every VPC. Two networks with overlapping ranges cannot be connected later, so pick a different range for each environment now.
Public subnets for what faces the internet
Each zone gets a public subnet with a route to the internet gateway. Only load balancers and the NAT gateway belong here. Instances launched in these subnets do not receive a public address automatically.
Private subnets for everything else
Each zone also gets a private subnet with no route to the internet gateway. Put application servers, containers and databases here. Users reach them through a load balancer in the public subnets.
One NAT gateway for outbound traffic
Private servers still need to download packages and call outside APIs. The NAT gateway lets them start connections out, and nothing can start a connection in through it. This pattern uses one gateway to keep the bill down: if its zone has an outage, the other zone loses outbound access until it recovers. Add a second gateway when that matters more than the extra charge.
Use the outputs
Pass private_subnet_ids to your database and compute configurations and public_subnet_ids to your load balancer. All outbound traffic leaves from the address in nat_public_ip, which is the one to give a partner who allow-lists by IP.
What you set
name
Short name used to tag the VPC and everything in it For example acme-prod
vpc_cidr
Address range for the VPC, different for each environment For example 10.0.0.0/16
region
Region to build the network in For example us-east-1
What it costs to run
About 35 to 50 dollars a month. Nearly all of it is the NAT gateway: an hourly charge plus a charge per GB of traffic that passes through it. The VPC, subnets and route tables are free. Estimate.
The configuration
# A VPC across two availability zones with public and private subnets.
#
# What this creates:
# - a VPC with an internet gateway
# - two public subnets (for load balancers and the NAT gateway)
# - two private subnets (for application servers and databases)
# - one NAT gateway so private subnets can reach the internet for outbound calls
# - route tables that tie it together
terraform {
required_version = ">= 1.6.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = var.region
}
variable "region" {
description = "AWS region to deploy into."
type = string
default = "us-east-1"
}
variable "name" {
description = "Short name used to tag every resource, for example the product or environment."
type = string
}
variable "vpc_cidr" {
description = "Address range for the VPC. Pick one that does not overlap your office network or any VPC you may connect to later."
type = string
default = "10.0.0.0/16"
}
data "aws_availability_zones" "available" {
state = "available"
}
locals {
# Two zones: enough to survive the loss of one, without paying for a third.
azs = slice(data.aws_availability_zones.available.names, 0, 2)
# A /16 VPC is cut into /20 subnets (4,096 addresses each). Public subnets
# take the first two slots and private subnets start at slot 8, which leaves
# room to add more of either kind later.
public_subnet_cidrs = [for i in range(2) : cidrsubnet(var.vpc_cidr, 4, i)]
private_subnet_cidrs = [for i in range(2) : cidrsubnet(var.vpc_cidr, 4, i + 8)]
}
resource "aws_vpc" "this" {
cidr_block = var.vpc_cidr
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = var.name
}
}
# The default security group allows all traffic between its members. Taking it
# over with no rules means nothing can use it by accident.
resource "aws_default_security_group" "this" {
vpc_id = aws_vpc.this.id
tags = {
Name = "${var.name}-default-unused"
}
}
resource "aws_internet_gateway" "this" {
vpc_id = aws_vpc.this.id
tags = {
Name = var.name
}
}
resource "aws_subnet" "public" {
count = 2
vpc_id = aws_vpc.this.id
cidr_block = local.public_subnet_cidrs[count.index]
availability_zone = local.azs[count.index]
# Nothing gets a public address just by being launched here. Load balancers
# and the NAT gateway bring their own.
map_public_ip_on_launch = false
tags = {
Name = "${var.name}-public-${local.azs[count.index]}"
Tier = "public"
}
}
resource "aws_subnet" "private" {
count = 2
vpc_id = aws_vpc.this.id
cidr_block = local.private_subnet_cidrs[count.index]
availability_zone = local.azs[count.index]
tags = {
Name = "${var.name}-private-${local.azs[count.index]}"
Tier = "private"
}
}
resource "aws_eip" "nat" {
domain = "vpc"
tags = {
Name = "${var.name}-nat"
}
}
# One NAT gateway, in the first zone. Each NAT gateway has an hourly charge, so
# one per zone doubles the bill. The trade-off: if the first zone has an
# outage, private subnets in the second zone lose outbound internet access
# until it recovers. Inbound traffic through a load balancer is not affected.
# For one per zone, create a second gateway in public[1] and point private[1]
# at it.
resource "aws_nat_gateway" "this" {
allocation_id = aws_eip.nat.id
subnet_id = aws_subnet.public[0].id
# The gateway needs the internet gateway to exist before it can work.
depends_on = [aws_internet_gateway.this]
tags = {
Name = var.name
}
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.this.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.this.id
}
tags = {
Name = "${var.name}-public"
}
}
resource "aws_route_table_association" "public" {
count = 2
subnet_id = aws_subnet.public[count.index].id
route_table_id = aws_route_table.public.id
}
# Private subnets have no route to the internet gateway, so nothing on the
# internet can open a connection to them. Outbound traffic leaves through NAT.
resource "aws_route_table" "private" {
vpc_id = aws_vpc.this.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.this.id
}
tags = {
Name = "${var.name}-private"
}
}
resource "aws_route_table_association" "private" {
count = 2
subnet_id = aws_subnet.private[count.index].id
route_table_id = aws_route_table.private.id
}
output "vpc_id" {
description = "ID of the VPC."
value = aws_vpc.this.id
}
output "vpc_cidr" {
description = "Address range of the VPC."
value = aws_vpc.this.cidr_block
}
output "public_subnet_ids" {
description = "Put load balancers here."
value = aws_subnet.public[*].id
}
output "private_subnet_ids" {
description = "Put application servers and databases here."
value = aws_subnet.private[*].id
}
output "nat_public_ip" {
description = "The address outbound traffic from private subnets comes from. Give this to partners who allow-list by IP."
value = aws_eip.nat.public_ip
}
Works with OpenTofu 1.6 or later, and with Terraform. Read it before you apply it: it creates real resources in your account.