Day 03 – Building a VPC with Public & Private Subnets in Terraform


Yesterday, I built my first custom VPC with a single subnet. Today, I went deeper into AWS networking by creating a VPC with both a public and private subnet, plus an Internet Gateway and a Route Table.
By the end of the day, I had a network ready to host internet-facing resources (in the public subnet) and secure resources (in the private subnet).
🔹 What is a VPC?
A VPC (Virtual Private Cloud) is your private, isolated network inside AWS. You can create subnets, attach gateways, and control access to your infrastructure.
🔹 Terraform Code
Here’s the code I used for today’s setup:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "6.9.0"
}
}
}
provider "aws" {
region = "ap-south-1"
}
# VPC
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "my_vpc"
}
}
# Private Subnet
resource "aws_subnet" "private-subnet" {
cidr_block = "10.0.1.0/24"
vpc_id = aws_vpc.my_vpc.id
tags = {
Name = "private-subnet"
}
}
# Public Subnet
resource "aws_subnet" "public-subnet" {
cidr_block = "10.0.2.0/24"
vpc_id = aws_vpc.my_vpc.id
map_public_ip_on_launch = true
tags = {
Name = "public-subnet"
}
}
# Internet Gateway
resource "aws_internet_gateway" "my-igw" {
vpc_id = aws_vpc.my_vpc.id
tags = {
Name = "my-igw"
}
}
# Route Table
resource "aws_route_table" "my-rt" {
vpc_id = aws_vpc.my_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.my-igw.id
}
}
# Route Table Association (Public Subnet)
resource "aws_route_table_association" "public-sub" {
route_table_id = aws_route_table.my-rt.id
subnet_id = aws_subnet.public-subnet.id
}
🔹 What’s Happening Here?
VPC → Main network (
10.0.0.0/16
)Private Subnet → Internal-only resources (
10.0.1.0/24
)Public Subnet → Internet-facing resources (
10.0.2.0/24
)Internet Gateway → Allows internet access
Route Table → Sends outbound traffic from public subnet to internet
Route Table Association → Connects the public subnet with the route table
▶️ Terraform Commands I Used
terraform init # Initialize project
terraform validate # Validate code
terraform plan # Preview resources
terraform apply # Deploy VPC + Subnets + IGW + Route Table
terraform destroy # Tear it all down
💡 Key Takeaways
A VPC is the foundation of AWS networking.
Public subnets are for internet-facing resources like web servers.
Private subnets are for secure resources like databases.
Internet Gateway + Route Table is what gives your public subnet internet access.
🔗 Follow My Journey
📖 Blog Series: Terraform with AWS
💻 Code: GitHub
🐦 Updates: X (Twitter)
Subscribe to my newsletter
Read articles from Abdul Raheem directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Abdul Raheem
Abdul Raheem
Cloud DevOps | AWS | Terraform | CI/CD | Obsessed with clean infrastructure. Cloud DevOps Engineer 🚀 | Automating Infrastructure & Securing Pipelines | Bridging Gaps Between Code and Cloud ☁️ I’m on a mission to master DevOps from the ground up—building scalable systems, automating workflows, and integrating security into every phase of the SDLC. Currently working with AWS, Terraform, Docker, CI/CD, and learning the art of cloud-native development.