Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 

Repository files navigation

🌐 terraform-aws-vpc

Terraform AWS Region Linux IaC Status License

Production-ready AWS VPC networking infrastructure for an e-commerce platform β€” provisioned with Terraform in a single clean file.

πŸš€ Quick Start Β· πŸ“ Architecture Β· βš™οΈ Resources Β· πŸ“– Workflow Β· πŸ‘¨β€πŸ’» Author


πŸ“Œ Project Overview

terraform-aws-vpc provisions a complete AWS Virtual Private Cloud (VPC) networking environment tailored for an e-commerce application. The entire infrastructure is defined in a single Terraform file β€” aws_vpc.tf β€” making it clean, readable, and easy to understand for beginners while being practical enough for real-world use.

The project creates an isolated network with a public subnet for internet-facing resources and a private subnet for backend services, connected through an Internet Gateway and managed via a Route Table.

πŸ’‘ Region: All resources are deployed in us-east-2 (Ohio).


πŸ“ Architecture

                          🌍 Internet
                               β”‚
               β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
               β”‚     Internet Gateway (IGW)      β”‚
               β”‚        ecommerce-igw            β”‚
               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚
               β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
               β”‚          Route Table            β”‚
               β”‚       ecommerce-public-rtb      β”‚
               β”‚       0.0.0.0/0  β†’  IGW         β”‚
               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚  (Route Table Association)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      VPC  β€”  ecommerce-vpc                          β”‚
β”‚                         CIDR: 10.0.0.0/16                           β”‚
β”‚                                                                      β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚   β”‚      Public Subnet        β”‚   β”‚       Private Subnet          β”‚  β”‚
β”‚   β”‚  ecommerce-public-subnet  β”‚   β”‚  ecommerce-private-subnet     β”‚  β”‚
β”‚   β”‚      10.0.1.0/24          β”‚   β”‚       10.0.2.0/24             β”‚  β”‚
β”‚   β”‚  map_public_ip = true βœ…  β”‚   β”‚   map_public_ip = false ❌    β”‚  β”‚
β”‚   β”‚                           β”‚   β”‚                               β”‚  β”‚
β”‚   β”‚  Web Servers / LB /       β”‚   β”‚  Databases / App Servers /    β”‚  β”‚
β”‚   β”‚  Bastion Hosts            β”‚   β”‚  Internal Services            β”‚  β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

βš™οΈ Resources Provisioned

All 6 resources are defined inside the single file aws_vpc.tf:

# Resource Type Terraform Name AWS Name Tag
1 aws_vpc main ecommerce-vpc
2 aws_subnet (public) public_subnet ecommerce-public-subnet
3 aws_subnet (private) private_subnet ecommerce-private-subnet
4 aws_internet_gateway gw ecommerce-igw
5 aws_route_table ecommerce_rtb ecommerce-public-rtb
6 aws_route_table_association rtb_association (no tag needed)

πŸ“„ The Terraform File β€” aws_vpc.tf

provider "aws" {
  region = "us-east-2"
}

resource "aws_vpc" "main" {
  cidr_block       = "10.0.0.0/16"
  instance_tenancy = "default"
  tags = {
    Name = "ecommerce-vpc"
  }
}

resource "aws_subnet" "public_subnet" {
  vpc_id                  = aws_vpc.main.id
  cidr_block              = "10.0.1.0/24"
  map_public_ip_on_launch = true
  tags = {
    Name = "ecommerce-public-subnet"
  }
}

resource "aws_subnet" "private_subnet" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.2.0/24"
  tags = {
    Name = "ecommerce-private-subnet"
  }
}

resource "aws_internet_gateway" "gw" {
  vpc_id = aws_vpc.main.id
  tags = {
    Name = "ecommerce-igw"
  }
}

resource "aws_route_table" "ecommerce_rtb" {
  vpc_id = aws_vpc.main.id
  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.gw.id
  }
  tags = {
    Name = "ecommerce-public-rtb"
  }
}

resource "aws_route_table_association" "rtb_association" {
  subnet_id      = aws_subnet.public_subnet.id
  route_table_id = aws_route_table.ecommerce_rtb.id
}

🌐 Networking Deep Dive

🏠 VPC β€” ecommerce-vpc

Property Value
CIDR Block 10.0.0.0/16
Total IP Addresses 65,536
Tenancy default (shared hardware)
Region us-east-2 (Ohio)

The VPC is the root of all networking in this project. Every other resource β€” subnets, gateways, route tables β€” lives inside it. The /16 CIDR gives plenty of room to carve out multiple subnets as the project grows.


🌍 Public Subnet β€” ecommerce-public-subnet

Property Value
CIDR Block 10.0.1.0/24
Usable IPs 251 (AWS reserves 5 per subnet)
Auto-Assign Public IP βœ… true
Internet Access βœ… Yes, via IGW + Route Table

map_public_ip_on_launch = true means any EC2 instance launched here automatically gets a public IP β€” no manual assignment needed. This is the right subnet for web servers, load balancers, and bastion hosts.


πŸ”’ Private Subnet β€” ecommerce-private-subnet

Property Value
CIDR Block 10.0.2.0/24
Usable IPs 251 (AWS reserves 5 per subnet)
Auto-Assign Public IP ❌ false (default)
Internet Access ❌ No (NAT Gateway needed for outbound)

Resources here are not reachable from the internet β€” ideal for databases (RDS), internal APIs, and application servers that should never be publicly exposed.


πŸ” Internet Gateway β€” ecommerce-igw

The IGW is attached directly to ecommerce-vpc and acts as the bridge between the VPC and the public internet. Without it, no traffic can flow in or out β€” even from the public subnet.


πŸ›€οΈ Route Table β€” ecommerce-public-rtb

Destination Target Purpose
10.0.0.0/16 local Internal VPC traffic (auto-added by AWS)
0.0.0.0/0 ecommerce-igw All outbound internet traffic

The route 0.0.0.0/0 β†’ IGW tells AWS: "send any traffic not destined for the local VPC out through the Internet Gateway."


πŸ”— Route Table Association β€” rtb_association

This resource links ecommerce-public-subnet to ecommerce-public-rtb. Without this association, the subnet falls back to the VPC's default route table and loses internet access.

⚠️ Note: The private subnet intentionally has no route table association β€” it uses the VPC's default route table, which has no internet route. This is by design.


πŸ“ File Structure

terraform-aws-vpc/
β”‚
β”œβ”€β”€ πŸ“„ aws_vpc.tf      # All infrastructure: provider, VPC, subnets, IGW, route table
└── πŸ“„ README.md       # Project documentation (you are here!)

This project uses a single-file structure intentionally β€” everything lives in aws_vpc.tf for simplicity and learning. As projects grow, resources are typically split into main.tf, variables.tf, outputs.tf, and provider.tf.


πŸ› οΈ Prerequisites

Before running this project, ensure you have:

  • βœ… Terraform >= 1.0 installed
  • βœ… AWS CLI installed and configured
  • βœ… An active AWS Account
  • βœ… IAM user with AmazonVPCFullAccess permissions (or Admin for learning)

Configure AWS CLI

aws configure
# AWS Access Key ID:     <your-access-key>
# AWS Secret Access Key: <your-secret-key>
# Default region name:   us-east-2
# Default output format: json

πŸš€ Quick Start

1. Clone the Repository

git clone https://github.com/Muhammadgi/aws-vpc-terraform
cd aws-vpc-terraform

πŸ“– Terraform Workflow

β‘  Initialize β€” Download AWS Provider

terraform init

Downloads the HashiCorp AWS provider plugin required to communicate with AWS APIs.

Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions...
- Installing hashicorp/aws v5.x.x...

βœ… Terraform has been successfully initialized!

β‘‘ Validate β€” Check for Errors

terraform validate

Checks aws_vpc.tf for syntax errors and configuration issues before touching any AWS resources.

βœ… Success! The configuration is valid.

β‘’ Plan β€” Preview What Will Be Created

terraform plan

Shows exactly what Terraform will do β€” safely, without making any changes to AWS.

Plan: 6 to add, 0 to change, 0 to destroy.

  + aws_vpc.main
  + aws_subnet.public_subnet
  + aws_subnet.private_subnet
  + aws_internet_gateway.gw
  + aws_route_table.ecommerce_rtb
  + aws_route_table_association.rtb_association

β‘£ Apply β€” Deploy to AWS

terraform apply

Type yes when prompted. Terraform creates all 6 resources in us-east-2.

aws_vpc.main: Creating...
aws_vpc.main: Creation complete [id=vpc-xxxxxxxxxxxxxxxxx]
aws_subnet.public_subnet: Creating...
aws_subnet.private_subnet: Creating...
aws_internet_gateway.gw: Creating...
aws_route_table.ecommerce_rtb: Creating...
aws_route_table_association.rtb_association: Creating...

βœ… Apply complete! Resources: 6 added, 0 changed, 0 destroyed.

β‘€ Destroy β€” Clean Up (Avoid AWS Charges)

terraform destroy
βœ… Destroy complete! Resources: 6 destroyed.

πŸ’° Important: Always run terraform destroy when done experimenting to avoid unexpected AWS charges.


πŸŽ“ Learning Outcomes

By studying and running this project, you will:

  • 🧠 Understand how AWS VPC components connect to form a complete network
  • 🌐 Know the difference between public and private subnets and when to use each
  • πŸ” Understand how Internet Gateways and Route Tables enable internet connectivity
  • πŸ“¦ Write real Terraform HCL code with providers, resources, and tags
  • πŸ”„ Execute the full Terraform lifecycle: init β†’ validate β†’ plan β†’ apply β†’ destroy
  • 🏷️ Apply AWS resource tagging best practices for organized cloud infrastructure
  • πŸ—οΈ Understand single-file vs. multi-file Terraform project structures

πŸ—ΊοΈ Future Improvements

Feature Description Priority
πŸ”„ NAT Gateway Allow private subnet to reach the internet securely (outbound only) πŸ”΄ High
πŸ–₯️ EC2 Instances Deploy web servers in public subnet, app servers in private πŸ”΄ High
πŸ” Security Groups Firewall rules to control inbound/outbound traffic per resource 🟑 Medium
🌍 Multi-AZ Setup Replicate subnets across Availability Zones for high availability 🟑 Medium
πŸ“Š VPC Flow Logs Enable network traffic monitoring via CloudWatch 🟒 Planned
🧩 Terraform Modules Refactor into reusable modules for multi-environment deployments 🟒 Planned
πŸ“ Split File Structure Separate into main.tf, variables.tf, outputs.tf, provider.tf 🟒 Planned

🏷️ Recommended GitHub Topics

Add these to your repository settings under Topics for better discoverability:

terraform  aws  vpc  devops  infrastructure-as-code  cloud-computing
aws-vpc  terraform-aws  networking  iac  ecommerce-infrastructure
us-east-2  public-subnet  private-subnet  internet-gateway  route-table
hashicorp  beginner-friendly  portfolio-project

πŸ‘¨β€πŸ’» Author

Muhammad Haris

CodebyHarris

DevOps Engineer Β· Cloud Practitioner Β· IaC Enthusiast

GitHub LinkedIn


πŸ“„ License

This project is licensed under the MIT License β€” free to use, modify, and share.


⭐ Found this helpful? Drop a star β€” it helps other learners find this project!


Built with ❀️ by CodebyHarris β€” Turning cloud concepts into real infrastructure, one terraform apply at a time.

Made with Terraform Powered by AWS Region

Releases

Packages

Contributors

Languages