Step-by-step guide to building a custom VPC and its components using Terraform.

Step-by-step guide to building a custom VPC and its components using Terraform.

Introduction.

When managing cloud infrastructure, creating a custom Virtual Private Cloud (VPC) is one of the first and most crucial steps. A VPC allows you to define your network environment in the cloud, including subnets, IP ranges, routing tables, and more. By using Infrastructure as Code (IaC) tools like Terraform, you can automate the process of building and managing a VPC, ensuring consistency, scalability, and easy reproducibility.

In this guide, we’ll walk you through the process of building a custom VPC and configuring its essential components, such as subnets, security groups, and routing, using Terraform. Whether you’re new to Terraform or looking to expand your cloud infrastructure knowledge, this step-by-step tutorial will help you get up and running with a secure and efficient VPC setup.

STEP 1: Open the folder and create the variable.tf file.

  • Enter the following command and save the file.
variable "access_key" {
    description = "Access key to AWS console"           
}
variable "secret_key" {
    description = "Secret key to AWS console"           
}
variable "region" {
    description = "AWS region"          
}
Screenshot 2025 03 03 232850

STEP 2: Next Create terraform.tfvars file.

  • Enter the following terrform script and click on save.
  • Enter the access key and secret key.
region = "us-east-1"
access_key = "YOUR_ACCESS_KEY"
secret_key = "YOUR_SECRET_KEY"
Screenshot 2025 03 03 233120

STEP 3: Create main.tf files.

provider "aws" {
region = "${var.region}"
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
}
resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"    
  tags = {
    Name = "MyVPC"      
  }         
}
resource "aws_subnet" "public-subnet" {
    vpc_id = "${aws_vpc.main.id}"
    cidr_block = "10.0.1.0/24"
    map_public_ip_on_launch = "true"
    availability_zone = "us-east-1a"
    tags = {
      Name = "PublicSubnet"
  }
}
resource "aws_subnet" "private-subnet" {
    vpc_id = "${aws_vpc.main.id}"
    cidr_block = "10.0.2.0/24"
    availability_zone = "us-east-1b"
    tags =  {
        Name = "Private Subnet"
    }
}
resource "aws_internet_gateway" "MyIGW" {
    vpc_id = "${aws_vpc.main.id}"
    tags =  {
        Name = "MyInternetGateway"
    }
}
resource "aws_route_table" "publicrt" {
    vpc_id = "${aws_vpc.main.id}"
    route {
        cidr_block = "0.0.0.0/0"
        gateway_id = "${aws_internet_gateway.MyIGW.id}"
    }
    tags = {
        Name = "PublicRouteTable"
    }
}
resource "aws_route_table" "publicrt" {
    vpc_id = "${aws_vpc.main.id}"
    route {
        cidr_block = "0.0.0.0/0"
        gateway_id = "${aws_internet_gateway.MyIGW.id}"
    }
    tags = {
        Name = "PublicRouteTable"
    }
}
resource "aws_route_table" "privatert" {
    vpc_id = "${aws_vpc.main.id}"
    tags = {
        Name = "PrivateRouteTable"
    }
}
resource "aws_route_table_association" "public-association"{
    subnet_id = "${aws_subnet.public-subnet.id}"
    route_table_id = "${aws_route_table.publicrt.id}"
}
resource "aws_route_table_association" "private-association"{
    subnet_id = "${aws_subnet.private-subnet.id}"
    route_table_id = "${aws_route_table.privatert.id}"
}
Screenshot 2025 03 03 233701
Screenshot 2025 03 03 233725 1

STEP 4: Create an output file.

  • Paste the below content into the output.tf file.
output "vpc_id" {
    value= aws_vpc.main.id
}
output "public_subnet"{
     value = aws_subnet.public-subnet.id
}
output "private_subnet"{
     value = aws_subnet.private-subnet.id
}    
Screenshot 2025 03 03 233816

STEP 5: Applying terraform configurations.

  • Enter terraform init command.
Screenshot 2025 03 03 234245

STEP 6: Next enter the terraform plan command.

Screenshot 2025 03 03 234600

STEP 7: Enter the terraform apply command.

Screenshot 2025 03 03 234705
Screenshot 2025 03 03 234719

STEP 8: Check the resources in the AWS Console.

  • Navigate the vpc.
  • You can view that the VPC is created successfully.
Screenshot 2025 03 03 234832

STEP 9: Click on Subnet and verify the new one.

Screenshot 2025 03 03 234858

STEP 10: Select routetable and verify the new routetable.

Screenshot 2025 03 03 234925

STEP 11: Next, Verify the Internet gateway.

Screenshot 2025 03 03 234958

Conclusion.

Building a custom VPC with Terraform is an essential skill for managing cloud infrastructure efficiently. By automating the creation of VPCs and their components, you ensure that your network environment is consistent, secure, and scalable. In this guide, we’ve walked through the key steps to configure subnets, security groups, and routing tables, helping you set up a robust VPC tailored to your needs.

With Terraform, you can easily modify, version control, and scale your infrastructure, making it easier to manage in the long run. Whether you’re working on a small project or managing complex cloud architectures, mastering Terraform for VPC creation is a valuable step toward optimizing your cloud infrastructure management.

Tags: No tags

Add a Comment

Your email address will not be published. Required fields are marked *