Skip to content

Instantly share code, notes, and snippets.

@arcezd
Last active December 23, 2021 02:20
Show Gist options
  • Save arcezd/b9bd6cda753d9f829442c31c99a951e7 to your computer and use it in GitHub Desktop.
Save arcezd/b9bd6cda753d9f829442c31c99a951e7 to your computer and use it in GitHub Desktop.
Terraform AWS Network Templates
locals {
main_vpc_cidr_block = "10.0.0.0/16"
public_subnets = {
"PublicSubnetA" = {
cidr_block = "10.0.1.0/24",
availability_zone_id = "use1-az4"
},
"PublicSubnetB" = {
cidr_block = "10.0.2.0/24",
availability_zone_id = "use1-az6"
},
}
private_subnets = {
"PrivateSubnetA" = {
cidr_block = "10.0.255.0/24",
availability_zone_id = "use1-az4"
},
"PrivateSubnetB" = {
cidr_block = "10.0.254.0/24",
availability_zone_id = "use1-az6"
},
}
}
## Main VPC
resource "aws_vpc" "main" {
cidr_block = local.main_vpc_cidr_block
enable_dns_support = true
enable_dns_hostnames = true
tags = merge(var.tags, {
Name = "Main VPC"
})
}
## Internet gateway for the main VPC
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
tags = merge(var.tags, {
Name = "Main Internet Gateway"
})
}
## Public subnets
resource "aws_subnet" "public" {
for_each = local.public_subnets
vpc_id = aws_vpc.main.id
availability_zone_id = each.value.availability_zone_id
cidr_block = each.value.cidr_block
map_public_ip_on_launch = true
tags = merge(var.tags, {
Name = each.key
})
}
## Private subnets
resource "aws_subnet" "private" {
for_each = local.private_subnets
vpc_id = aws_vpc.main.id
availability_zone_id = each.value.availability_zone_id
cidr_block = each.value.cidr_block
map_public_ip_on_launch = false
tags = merge(var.tags, {
Name = each.key
})
}
## Public NAT gateway elastic ip
resource "aws_eip" "natgw" {
vpc = true
tags = merge(var.tags, {
Name = "ElasticIP for NAT gateway"
})
}
## Public NAT Gateway
resource "aws_nat_gateway" "main" {
allocation_id = aws_eip.natgw.id
subnet_id = aws_subnet.public["PublicSubnetA"].id
tags = merge(var.tags, {
Name = "Public NAT GW"
})
# To ensure proper ordering, it is recommended to add an explicit dependency
# on the Internet Gateway for the VPC.
depends_on = [aws_internet_gateway.main]
}
## Route table for public subnets
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
tags = merge(var.tags, {
Name = "Public Subnets route table"
})
}
## Route table for private subnets
resource "aws_route_table" "private" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_nat_gateway.main.id
}
tags = merge(var.tags, {
Name = "Private Subnets route table"
})
}
## Main route table association
resource "aws_main_route_table_association" "main" {
vpc_id = aws_vpc.main.id
route_table_id = aws_route_table.private.id
}
## Route table association for public subnets
resource "aws_route_table_association" "public" {
for_each = local.public_subnets
subnet_id = aws_subnet.public["${each.key}"].id
route_table_id = aws_route_table.public.id
}
locals {
main_vpc_cidr_block = "10.0.0.0/16"
public_subnets = {
"PublicSubnetA" = {
cidr_block = "10.0.1.0/24",
availability_zone_id = "use1-az4"
},
"PublicSubnetB" = {
cidr_block = "10.0.2.0/24",
availability_zone_id = "use1-az6"
},
}
private_subnets = {
"PrivateSubnetA" = {
cidr_block = "10.0.255.0/24",
availability_zone_id = "use1-az4"
},
"PrivateSubnetB" = {
cidr_block = "10.0.254.0/24",
availability_zone_id = "use1-az6"
},
}
}
## Main VPC
resource "aws_vpc" "main" {
cidr_block = local.main_vpc_cidr_block
enable_dns_support = true
enable_dns_hostnames = true
assign_generated_ipv6_cidr_block = true
tags = merge(var.tags, {
Name = "Main VPC"
})
}
## Internet gateway for the main VPC
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
tags = merge(var.tags, {
Name = "Main Internet Gateway"
})
}
## Egress-only Internet gateway for the main VPC [IPv6 only]
resource "aws_egress_only_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
tags = merge(var.tags, {
Name = "Main Egress-only Internet Gateway"
})
}
## Public subnets
resource "aws_subnet" "public" {
for_each = local.public_subnets
vpc_id = aws_vpc.main.id
availability_zone_id = each.value.availability_zone_id
cidr_block = each.value.cidr_block
map_public_ip_on_launch = true
tags = merge(var.tags, {
Name = each.key
})
}
## Private subnets
resource "aws_subnet" "private" {
for_each = local.private_subnets
vpc_id = aws_vpc.main.id
availability_zone_id = each.value.availability_zone_id
cidr_block = each.value.cidr_block
map_public_ip_on_launch = false
tags = merge(var.tags, {
Name = each.key
})
}
## Public NAT gateway elastic ip
resource "aws_eip" "natgw" {
vpc = true
tags = merge(var.tags, {
Name = "ElasticIP for NAT gateway"
})
}
## Public NAT Gateway
resource "aws_nat_gateway" "main" {
allocation_id = aws_eip.natgw.id
subnet_id = aws_subnet.public["PublicSubnetA"].id
tags = merge(var.tags, {
Name = "Public NAT GW"
})
# To ensure proper ordering, it is recommended to add an explicit dependency
# on the Internet Gateway for the VPC.
depends_on = [aws_internet_gateway.main]
}
## Route table for public subnets
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
route {
ipv6_cidr_block = "::/0"
egress_only_gateway_id = aws_egress_only_internet_gateway.main.id
}
tags = merge(var.tags, {
Name = "Public Subnets route table"
})
}
## Route table for private subnets
resource "aws_route_table" "private" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_nat_gateway.main.id
}
route {
ipv6_cidr_block = "::/0"
egress_only_gateway_id = aws_egress_only_internet_gateway.main.id
}
tags = merge(var.tags, {
Name = "Private Subnets route table"
})
}
## Main route table association
resource "aws_main_route_table_association" "main" {
vpc_id = aws_vpc.main.id
route_table_id = aws_route_table.private.id
}
## Route table association for public subnets
resource "aws_route_table_association" "public" {
for_each = local.public_subnets
subnet_id = aws_subnet.public["${each.key}"].id
route_table_id = aws_route_table.public.id
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment