In this task, we will create a Virtual Private Cloud (VPC) in AWS with the following components:
Public-Route-Table
MyCustomVPC
0.0.0.0/0
Internet Gateway (MyInternetGateway)
Public-Subnet-1
and Public-Subnet-2
Private-Route-Table
MyCustomVPC
Private-Subnet-1
and Private-Subnet-2
If private instances need internet access, use a NAT Gateway:.
If you want to automate the VPC creation, you can use Terraform to provision this infrastructure.
terraform-vpc/
│-- main.tf # Main Terraform configuration
│-- variables.tf # Variables for customization
│-- outputs.tf # Outputs for the created resources
│-- provider.tf # AWS Provider configuration
│-- terraform.tfvars # Variable values
This file configures the AWS provider.
provider "aws" {
region = var.aws_region
}
variables.tf
This file defines the variables.
variable "aws_region" {
description = "AWS region"
default = "us-east-1"
}
variable "vpc_cidr" {
description = "VPC CIDR block"
default = "10.0.0.0/16"
}
variable "public_subnet_cidrs" {
description = "Public subnet CIDRs"
type = list(string)
default = ["10.0.1.0/24", "10.0.2.0/24"]
}
variable "private_subnet_cidrs" {
description = "Private subnet CIDRs"
type = list(string)
default = ["10.0.3.0/24", "10.0.4.0/24"]
}
main.tf
This file contains the main Terraform resources.
# Create VPC
resource "aws_vpc" "my_vpc" {
cidr_block = var.vpc_cidr
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "MyCustomVPC"
}
}
# Create Public Subnets
resource "aws_subnet" "public_subnets" {
count = length(var.public_subnet_cidrs)
vpc_id = aws_vpc.my_vpc.id
cidr_block = var.public_subnet_cidrs[count.index]
map_public_ip_on_launch = true
availability_zone = element(["us-east-1a", "us-east-1b"], count.index)
tags = {
Name = "Public-Subnet-${count.index + 1}"
}
}
# Create Private Subnets
resource "aws_subnet" "private_subnets" {
count = length(var.private_subnet_cidrs)
vpc_id = aws_vpc.my_vpc.id
cidr_block = var.private_subnet_cidrs[count.index]
availability_zone = element(["us-east-1a", "us-east-1b"], count.index)
tags = {
Name = "Private-Subnet-${count.index + 1}"
}
}
# Create Internet Gateway
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.my_vpc.id
tags = {
Name = "MyInternetGateway"
}
}
# Create Public Route Table
resource "aws_route_table" "public_rt" {
vpc_id = aws_vpc.my_vpc.id
tags = {
Name = "Public-Route-Table"
}
}
# Add Route to Internet Gateway in Public Route Table
resource "aws_route" "public_internet_route" {
route_table_id = aws_route_table.public_rt.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
# Associate Public Subnets with Public Route Table
resource "aws_route_table_association" "public_association" {
count = length(var.public_subnet_cidrs)
subnet_id = aws_subnet.public_subnets[count.index].id
route_table_id = aws_route_table.public_rt.id
}
# Create NAT Gateway (for private subnet internet access)
resource "aws_eip" "nat_eip" {
domain = "vpc"
}
resource "aws_nat_gateway" "nat" {
allocation_id = aws_eip.nat_eip.id
subnet_id = aws_subnet.public_subnets[0].id
tags = {
Name = "MyNATGateway"
}
depends_on = [aws_internet_gateway.igw]
}
# Create Private Route Table
resource "aws_route_table" "private_rt" {
vpc_id = aws_vpc.my_vpc.id
tags = {
Name = "Private-Route-Table"
}
}
# Add Route to NAT Gateway in Private Route Table
resource "aws_route" "private_nat_route" {
route_table_id = aws_route_table.private_rt.id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat.id
}
# Associate Private Subnets with Private Route Table
resource "aws_route_table_association" "private_association" {
count = length(var.private_subnet_cidrs)
subnet_id = aws_subnet.private_subnets[count.index].id
route_table_id = aws_route_table.private_rt.id
}
outputs.tf
This file provides output values.
output "vpc_id" {
value = aws_vpc.my_vpc.id
}
output "public_subnet_ids" {
value = aws_subnet.public_subnets[*].id
}
output "private_subnet_ids" {
value = aws_subnet.private_subnets[*].id
}
output "internet_gateway_id" {
value = aws_internet_gateway.igw.id
}
output "nat_gateway_id" {
value = aws_nat_gateway.nat.id
}
Define variable values (optional).
aws_region = "us-east-1"
vpc_cidr = "10.0.0.0/16"
public_subnet_cidrs = ["10.0.1.0/24", "10.0.2.0/24"]
private_subnet_cidrs = ["10.0.3.0/24", "10.0.4.0/24"]
Run the following commands to deploy the infrastructure:
# Initialize Terraform
terraform init
# Preview the changes
terraform plan
# Apply the configuration
terraform apply -auto-approve
If you want to remove the infrastructure:
terraform destroy -auto-approve