AWS Builder Center
Stop Clicking in the Console: Build a Production-Style AWS VPC with Terraform and Safe Remote State

Stop Clicking in the Console: Build a Production-Style AWS VPC with Terraform and Safe Remote State

A beginner-friendly guide to creating a multi-AZ VPC with public and private subnets using Terraform, storing state safely in Amazon S3 so you and your team never overwrite each other’s work. Includes the full code and the mistakes to avoid.

Aspiring Devops , Cloud And Mlops | B.E in Gujrat Technological University
The first time I built a VPC by clicking through the console, it took 40 minutes. When I had to rebuild it for a second environment, I couldn’t remember half of what I’d clicked. Terraform fixed that: the whole network is now code I can review, version and recreate in minutes.
What we’re building
The layout in the image above:
  • One VPC ( 10.0.0.0/16 )
  • Two Availability Zones for high availability
  • A public subnet in each AZ for load balancers and NAT gateways
  • A private subnet in each AZ for app servers and databases
  • An internet gateway for public traffic
Step 1: Remote state in S3
By default, Terraform stores state in a local file. That breaks the moment a second person (or a CI job) runs it. Create an S3 bucket first, with versioning and encryption turned on, then point Terraform at it:
hcl
terraform {
required_version = ">= 1.10"

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

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

provider "aws" {
region = "ap-south-1"
}
 use_lockfile  gives you state locking directly in S3 on recent Terraform versions. On older versions you would add a DynamoDB table for locking instead.
Step 2: The VPC
You can write every route table and association by hand, but the community module handles that correctly:
hcl
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 5.0"

name = "demo-vpc"
cidr = "10.0.0.0/16"

azs = ["ap-south-1a", "ap-south-1b"]
public_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
private_subnets = ["10.0.11.0/24", "10.0.12.0/24"]

enable_nat_gateway = true
single_nat_gateway = true

tags = {
Environment = "dev"
ManagedBy = "terraform"
}
}
Step 3: Run it
bash
terraform init
terraform plan
terraform apply
Always read the  plan  output before you apply. It shows exactly what will be created, changed or destroyed.
Why this matters
  • Repeatable: the same code builds dev, staging and prod.
  • Reviewable: network changes go through pull requests, not memory.
  • Recoverable: if something breaks,  terraform apply  brings it back.
  • Documented: the code is the documentation.
Mistakes I made so you don’t have to
  1. Keeping state locally. I lost a state file once and Terraform tried to recreate resources that already existed.
  1. Committing  .tfstate  to Git. State can contain sensitive values. Add it to  .gitignore  and use the S3 backend.
  1. One NAT gateway per AZ in a dev environment. Each NAT gateway has an hourly charge plus data processing fees.  single_nat_gateway = true  is fine for dev, but use one per AZ in production for resilience.
  1. Forgetting to destroy test environments. Run  terraform destroy  when you finish practicing so you don’t pay for idle NAT gateways.
Next steps
  • Add an EC2 bastion host or use Systems Manager Session Manager for private access
  • Add RDS in the private subnets
  • Split the code into modules for network, compute and database
If this was useful, drop a like and tell me: do you write Terraform from scratch or use community modules? I’d love to hear your approach in the comments.
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