AWS Builder Center
Terraform on AWS: Build a Production-Style VPC with Public and Private Subnets (Step by Step)

Terraform on AWS: Build a Production-Style VPC with Public and Private Subnets (Step by Step)

Stop clicking through the AWS console. This beginner-friendly guide shows how to build a multi-AZ VPC with public and private subnets, a bastion host, NAT gateways, and remote state using Terraform. Includes the code structure, common mistakes, and a cleanup checklist.

Aspiring Devops , Cloud And Mlops | B.E in Gujrat Technological University

Why build your VPC with Terraform?

Clicking through the console works for a demo. It breaks down when you need to repeat the setup, review changes, or rebuild after a mistake. Infrastructure as Code fixes this: your network lives in version control, changes are reviewed before they run, and the same code builds dev, test, and prod.

The architecture

The diagram above shows what we’re building:
  • A VPC spread across two Availability Zones for resilience
  • Public subnets for the bastion host and NAT gateways, with a route to the Internet Gateway
  • Private subnets for application servers and the database, with no direct inbound internet access
  • Remote state in S3, so your team shares one source of truth

Project structure

vpc-project/
├── provider.tf # AWS provider and backend
├── variables.tf # region, CIDR blocks, AZs
├── vpc.tf # VPC, subnets, gateways, routes
├── security.tf # security groups
├── bastion.tf # bastion host + key pair
└── outputs.tf # VPC ID, subnet IDs
Splitting files by purpose keeps each one short and easy to review.

Step 1: Configure the provider and remote state

terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}

backend "s3" {
bucket = "my-terraform-state-bucket"
key = "vpc/terraform.tfstate"
region = "ap-south-1"
use_lockfile = true
}
}

provider "aws" {
region = var.region
}
Create the S3 bucket first, and enable versioning on it. State files are precious. Check the current Terraform docs for the locking option your version supports.

Step 2: Create the VPC and subnets

resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
tags = { Name = "demo-vpc" }
}

resource "aws_subnet" "public" {
count = 2
vpc_id = aws_vpc.main.id
cidr_block = cidrsubnet("10.0.0.0/16", 8, count.index)
availability_zone = var.azs[count.index]
map_public_ip_on_launch = true
tags = { Name = "public-${count.index + 1}" }
}

resource "aws_subnet" "private" {
count = 2
vpc_id = aws_vpc.main.id
cidr_block = cidrsubnet("10.0.0.0/16", 8, count.index + 10)
availability_zone = var.azs[count.index]
tags = { Name = "private-${count.index + 1}" }
}
Using  count  and  cidrsubnet()  avoids copy-pasting and keeps your CIDR math consistent.

Step 3: Add the gateways and routes

  • An Internet Gateway attached to the VPC
  • A public route table with  0.0.0.0/0 → IGW 
  • A NAT Gateway in each public subnet (with an Elastic IP)
  • Private route tables with  0.0.0.0/0 → NAT 
Private servers can then download updates, but nothing on the internet can start a connection to them.

Step 4: Lock down access

  • The bastion security group allows SSH only from your own IP, never  0.0.0.0/0 
  • The app security group allows traffic only from the bastion or the load balancer
  • The database security group allows the database port only from the app security group
Chaining security groups this way is the simplest, most reliable pattern.

Step 5: Run it

terraform init
terraform fmt
terraform validate
terraform plan
terraform apply
Always read the  plan  output before you apply.

Mistakes to avoid

  1. Committing state files or secrets to Git. Use a remote backend and a  .gitignore .
  1. Opening SSH to the world. Restrict it to your IP, or use AWS Systems Manager Session Manager instead.
  1. Forgetting NAT Gateway costs. NAT gateways bill hourly plus data processing. Destroy lab environments when you finish, and check current pricing.
  1. Hardcoding values. Put regions, CIDRs, and names in variables.
  1. Skipping  terraform plan . One wrong change can replace a resource you meant to keep.

Cleanup

terraform destroy
Confirm in the console that NAT gateways and Elastic IPs are gone. These are the most common surprise charges.

Key takeaways

  • Treat your network as code: reviewable, repeatable, and versioned
  • Use public subnets only for what must face the internet
  • Keep app servers and databases in private subnets
  • Use remote state from day one
  • Always clean up lab resources

What should I cover next? 👇

Should I build on this VPC with Terraform modules, EKS on this network, or a CI/CD pipeline that runs  terraform plan  on every pull request? Tell me in the comments, and share the one Terraform mistake that cost you the most time.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article