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

PieceLayman Meaning
VPCThe whole private neighborhood
SubnetOne street inside the neighborhood
Internet gatewayThe main gate to the outside world
Route tableThe road signs that tell traffic where to go
Availability zoneA separate building location inside a region

Step 1: Project Files

vpc-project/
  providers.tf
  variables.tf
  main.tf
  outputs.tf

Step 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 cidrsubnet function slices the large address range into smaller blocks. With 10.0.0.0/16, index 0 produces 10.0.0.0/24 and index 1 produces 10.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 apply

The 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

  1. Open the VPC service in the AWS console.
  2. Find the VPC named project-vpc.
  3. Check that two subnets sit in two different zones.
  4. Open the route table and confirm the 0.0.0.0/0 route points to the gateway.

Step 7: Clean Up

terraform destroy

A 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 count argument and cidrsubnet() create subnets from one block of code.
  • Outputs expose the VPC and subnet IDs for other configurations.