Download and install Terraform from terraform.io.
Verify installation:
terraform --version
Step 2: Create Terraform Files
Create a new directory for Terraform:
mkdir terraform_project && cd terraform_project
Create main.tf:
nano main.tf
Paste the following configuration:
HCL
provider "aws" {
region = "us-east-1"
}
# Fetch Availability Zones
data "aws_availability_zones" "available" {}
# Create VPC
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "main-vpc"
}
}
# Create Public Subnets
resource "aws_subnet" "public" {
count = 3
vpc_id = aws_vpc.main.id
cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 8, count.index)
availability_zone = element(data.aws_availability_zones.available.names, count.index)
map_public_ip_on_launch = true
tags = {
Name = "public-subnet-${count.index + 1}"
}
}
# Create Private Subnets
resource "aws_subnet" "private" {
count = 3
vpc_id = aws_vpc.main.id
cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 8, count.index + 3)
availability_zone = element(data.aws_availability_zones.available.names, count.index)
tags = {
Name = "private-subnet-${count.index + 1}"
}
}
# Create Internet Gateway
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
tags = {
Name = "main-internet-gateway"
}
}
# Create Public Route Table
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
tags = {
Name = "public-route-table"
}
}
# Add Route to Internet Gateway in Public Route Table
resource "aws_route" "public_internet_access" {
route_table_id = aws_route_table.public.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
# Associate Public Subnets with Public Route Table
resource "aws_route_table_association" "public" {
count = 3
subnet_id = aws_subnet.public[count.index].id
route_table_id = aws_route_table.public.id
}
# Create Elastic IPs for NAT Gateways
resource "aws_eip" "nat" {
count = 3
vpc = true
tags = {
Name = "nat-eip-${count.index + 1}"
}
}
# Create NAT Gateways
resource "aws_nat_gateway" "nat" {
count = 3
allocation_id = aws_eip.nat[count.index].id
subnet_id = aws_subnet.public[count.index].id
tags = {
Name = "nat-gateway-${count.index + 1}"
}
}
# Create Private Route Tables
resource "aws_route_table" "private" {
count = 3
vpc_id = aws_vpc.main.id
tags = {
Name = "private-route-table-${count.index + 1}"
}
}
# Add Routes to NAT Gateways in Private Route Tables
resource "aws_route" "private_nat_access" {
count = 3
route_table_id = aws_route_table.private[count.index].id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat[count.index].id
}
# Associate Private Subnets with Private Route Tables
resource "aws_route_table_association" "private" {
count = 3
subnet_id = aws_subnet.private[count.index].id
route_table_id = aws_route_table.private[count.index].id
}
Initialize and apply:
bash
terraform init
terraform apply