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
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).
π 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 β β
β βββββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββ β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
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) |
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
}| 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.
| 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.
| 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.
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.
| 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."
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.
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.tffor simplicity and learning. As projects grow, resources are typically split intomain.tf,variables.tf,outputs.tf, andprovider.tf.
Before running this project, ensure you have:
- β
Terraform
>= 1.0installed - β AWS CLI installed and configured
- β An active AWS Account
- β
IAM user with
AmazonVPCFullAccesspermissions (or Admin for learning)
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: jsongit clone https://github.com/Muhammadgi/aws-vpc-terraform
cd aws-vpc-terraformterraform initDownloads 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!
terraform validateChecks aws_vpc.tf for syntax errors and configuration issues before touching any AWS resources.
β
Success! The configuration is valid.
terraform planShows 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
terraform applyType 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.
terraform destroyβ
Destroy complete! Resources: 6 destroyed.
π° Important: Always run
terraform destroywhen done experimenting to avoid unexpected AWS charges.
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
| 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 |
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
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.