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"
}

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"

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}"
}


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
}

STEP 5: Applying terraform configurations.
- Enter terraform init command.

STEP 6: Next enter the terraform plan command.

STEP 7: Enter the terraform apply command.


STEP 8: Check the resources in the AWS Console.
- Navigate the vpc.
- You can view that the VPC is created successfully.

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

STEP 10: Select routetable and verify the new routetable.

STEP 11: Next, Verify the Internet gateway.

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.
Add a Comment