Terraform Project AWS VPC
Every cloud project starts with a private neighborhood for its servers. AWS calls that neighborhood a Virtual Private Cloud, or VPC. Roads connect the streets, a gate leads to the outside world, and each street holds its own houses. This project builds that neighborhood with Terraform. The result becomes the foundation for later projects.
What You Will Build
Diagram: The Finished Network
Internet
|
+--------+--------+
| Internet Gate |
+--------+--------+
|
+-----------------------+-----------------------+
| VPC 10.0.0.0/16 | |
| | |
| +--------------------+-------------------+ |
| | Public Route Table | |
| +--------+-----------------------+-------+ |
| | | |
| +--------+--------+ +--------+--------+ |
| | Public Subnet A | | Public Subnet B | |
| | 10.0.0.0/24 | | 10.0.1.0/24 | |
| | Zone A | | Zone B | |
| +-----------------+ +-----------------+ |
+-----------------------------------------------+Real-World Pieces in Plain Words
| Piece | Layman Meaning |
|---|---|
| VPC | The whole private neighborhood |
| Subnet | One street inside the neighborhood |
| Internet gateway | The main gate to the outside world |
| Route table | The road signs that tell traffic where to go |
| Availability zone | A separate building location inside a region |
Step 1: Project Files
vpc-project/
providers.tf
variables.tf
main.tf
outputs.tfStep 2: Provider and Variables
providers.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = var.region
}variables.tf
variable "region" {
type = string
default = "us-east-1"
}
variable "vpc_cidr" {
type = string
default = "10.0.0.0/16"
}
variable "public_subnet_count" {
type = number
default = 2
}Step 3: Build the Network
main.tf
data "aws_availability_zones" "available" {
state = "available"
}
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
enable_dns_hostnames = true
tags = {
Name = "project-vpc"
}
}
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.main.id
tags = {
Name = "project-gateway"
}
}
resource "aws_subnet" "public" {
count = var.public_subnet_count
vpc_id = aws_vpc.main.id
cidr_block = cidrsubnet(var.vpc_cidr, 8, count.index)
availability_zone = data.aws_availability_zones.available.names[count.index]
map_public_ip_on_launch = true
tags = {
Name = "public-subnet-${count.index}"
}
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
tags = {
Name = "public-routes"
}
}
resource "aws_route_table_association" "public" {
count = var.public_subnet_count
subnet_id = aws_subnet.public[count.index].id
route_table_id = aws_route_table.public.id
}How the Code Works
- The data source asks AWS for the zones available in your region.
- The
cidrsubnetfunction slices the large address range into smaller blocks. With10.0.0.0/16, index 0 produces10.0.0.0/24and index 1 produces10.0.1.0/24. - The route table sends all outside traffic (
0.0.0.0/0) to the internet gateway. - Each association connects one subnet to the route table. A subnet without that link stays private.
Step 4: Outputs
outputs.tf
output "vpc_id" {
value = aws_vpc.main.id
}
output "public_subnet_ids" {
value = aws_subnet.public[*].id
}Step 5: Run the Project
terraform init
terraform fmt
terraform validate
terraform plan
terraform applyThe plan shows 7 resources to add: one VPC, one gateway, two subnets, one route table, and two associations. Type yes to apply.
Step 6: Verify in the Console
- Open the VPC service in the AWS console.
- Find the VPC named
project-vpc. - Check that two subnets sit in two different zones.
- Open the route table and confirm the
0.0.0.0/0route points to the gateway.
Step 7: Clean Up
terraform destroyA VPC with no running servers costs nothing. Destroy it anyway when you finish practicing to keep the account tidy.
Ideas to Extend the Project
- Add private subnets with no route to the gateway.
- Add a NAT gateway so private servers reach the internet outbound.
- Turn the whole configuration into a reusable module.
- Add tags from a map variable to every resource.
Key Points
- A VPC is a private network that holds your cloud resources.
- Subnets split the network and sit in different availability zones.
- An internet gateway and a route table make a subnet public.
- The
countargument andcidrsubnet()create subnets from one block of code. - Outputs expose the VPC and subnet IDs for other configurations.
