# Variable Values That Am using AWS_REGION = "us-east-2" VPC_NAME = "SomeVPC" VPC_CIDR = "10.0.0.0/16" PUBLIC_SUBNET_CIDR = ["10.0.1.0/24", "10.0.2.0/24","10.0.3.0/24"] PRIVATE_SUBNET_CIDR = ["10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24"] AVAILABILITY_ZONE = ["us-east-2a", "us-east-2b", "us-east-2c"] IPV4_CIDR_BLOCK = "0.0.0.0/0" IPV6_CIDR_BLOCK = "::/0" ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ # Create a VPC resource "aws_vpc" "VPC" { cidr_block = var.VPC_CIDR enable_dns_support = true enable_dns_hostnames = true assign_generated_ipv6_cidr_block = true tags = { Name = var.VPC_NAME CreatedBy = "Terraform" } } # Create Public Subnets resource "aws_subnet" "public_subnets" { count = length(var.PUBLIC_SUBNET_CIDR) vpc_id = aws_vpc.VPC.id cidr_block = element(var.PUBLIC_SUBNET_CIDR, count.index) ipv6_cidr_block = cidrsubnet(aws_vpc.VPC.ipv6_cidr_block, 8, count.index) availability_zone = element(var.AVAILABILITY_ZONE, count.index) map_public_ip_on_launch = true assign_ipv6_address_on_creation = true tags = { Name = "Public Subnet ${count.index + 1}" CreatedBy = "Terraform" } } # Create Private Subnets resource "aws_subnet" "private_subnets" { count = length(var.PRIVATE_SUBNET_CIDR) vpc_id = aws_vpc.VPC.id cidr_block = element(var.PRIVATE_SUBNET_CIDR, count.index) availability_zone = element(var.AVAILABILITY_ZONE, count.index) tags = { Name = "Private Subnet ${count.index + 1}" CreatedBy = "Terraform" } } # Create Internet Gateway resource "aws_internet_gateway" "gw" { vpc_id = aws_vpc.VPC.id tags = { Name = "Internet Gateway" CreatedBy = "Terraform" } } # Create Routing Table for Public Subnets resource "aws_route_table" "public_routing_table" { vpc_id = aws_vpc.VPC.id route { cidr_block = var.IPV4_CIDR_BLOCK gateway_id = aws_internet_gateway.gw.id } route { ipv6_cidr_block = var.IPV6_CIDR_BLOCK gateway_id = aws_internet_gateway.gw.id } tags = { Name = "Public Route Table" CreatedBy = "Terraform" } } # Create Routing Table for Private Subnets resource "aws_route_table" "private_routing_table" { count = length(var.PRIVATE_SUBNET_CIDR) vpc_id = aws_vpc.VPC.id route { cidr_block = var.IPV4_CIDR_BLOCK gateway_id = aws_internet_gateway.gw.id } route { ipv6_cidr_block = var.IPV6_CIDR_BLOCK gateway_id = aws_internet_gateway.gw.id } tags = { Name = "Private Route Table" CreatedBy = "Terraform" } } # Associate Routing Table for Public Subnets resource "aws_route_table_association" "public_subnet_asso" { count = length(var.PUBLIC_SUBNET_CIDR) subnet_id = element(aws_subnet.public_subnets[*].id, count.index) route_table_id = aws_route_table.public_routing_table.id } # Associate each subnet with the corresponding routing table resource "aws_route_table_association" "private_subnet_asso" { count = length(var.PRIVATE_SUBNET_CIDR) subnet_id = element(aws_subnet.private_subnets[*].id, count.index) route_table_id = element(aws_route_table.private_routing_table.*.id, count.index) } # Create a Subnet Group for RDS resource "aws_db_subnet_group" "subnet_group" { name = "db_subnet" description = "Subnet group for RDS instance" subnet_ids = concat(aws_subnet.public_subnets.*.id, aws_subnet.private_subnets.*.id) tags = { Name = "DB Subnet" CreatedBy = "Terraform" } }