sondehub-infra/vpc.tf

160 lines
5.2 KiB
Terraform
Raw Normal View History

2021-10-09 04:56:26 +00:00
resource "aws_vpc" "main" {
2021-10-09 06:23:00 +00:00
cidr_block = "172.31.0.0/16"
2021-10-09 04:56:26 +00:00
assign_generated_ipv6_cidr_block = true
}
resource "aws_egress_only_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
}
locals {
private_subnets = {
2023-08-04 07:10:41 +00:00
"us-east-1a" = ["172.31.128.0/24", cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 13)],
"us-east-1b" = ["172.31.131.0/24", cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 14)],
"us-east-1c" = ["172.31.130.0/24", cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 15)],
"us-east-1d" = ["172.31.133.0/24", cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 16)],
"us-east-1e" = ["172.31.129.0/24", cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 17)],
"us-east-1f" = ["172.31.132.0/24", cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 18)]
}
private_v6 = {
"us-east-1a" = cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 7),
"us-east-1b" = cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 8),
"us-east-1c" = cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 9),
"us-east-1d" = cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 10),
"us-east-1e" = cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 11),
"us-east-1f" = cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 12)
2021-10-09 04:56:26 +00:00
}
public_subnets = {
2023-08-04 07:10:41 +00:00
"us-east-1a" = ["172.31.80.0/20",cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 19)],
"us-east-1b" = ["172.31.16.0/20",cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 20)],
"us-east-1c" = ["172.31.32.0/20",cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 21)],
"us-east-1d" = ["172.31.0.0/20",cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 22)],
"us-east-1e" = ["172.31.48.0/20",cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 23)],
"us-east-1f" = ["172.31.64.0/20",cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 24)]
}
public_v6 = {
"us-east-1a" = cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 1),
"us-east-1b" = cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 2),
"us-east-1c" = cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 3),
"us-east-1d" = cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 4),
"us-east-1e" = cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 5),
"us-east-1f" = cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, 6)
2021-10-09 04:56:26 +00:00
}
}
resource "aws_subnet" "private" {
for_each = local.private_subnets
map_public_ip_on_launch = false
2021-10-09 06:23:00 +00:00
vpc_id = aws_vpc.main.id
2023-08-04 07:10:41 +00:00
cidr_block = each.value[0]
ipv6_cidr_block = each.value[1]
assign_ipv6_address_on_creation = true
2021-10-09 04:56:26 +00:00
tags = {
Name = "${each.key} - private"
}
}
resource "aws_subnet" "public" {
for_each = local.public_subnets
map_public_ip_on_launch = false
2021-10-09 06:23:00 +00:00
vpc_id = aws_vpc.main.id
2023-08-04 07:10:41 +00:00
cidr_block = each.value[0]
ipv6_cidr_block = each.value[1]
assign_ipv6_address_on_creation = true
2021-10-09 06:23:00 +00:00
2021-10-09 04:56:26 +00:00
tags = {
Name = "${each.key} - public"
}
}
2023-08-04 07:10:41 +00:00
resource "aws_subnet" "public_v6_only" {
for_each = local.public_v6
availability_zone = each.key
enable_resource_name_dns_aaaa_record_on_launch = true
assign_ipv6_address_on_creation = true
vpc_id = aws_vpc.main.id
ipv6_native = true
ipv6_cidr_block = each.value
tags = {
Name = "${each.key} - public v6 only"
}
}
resource "aws_subnet" "private_v6_only" {
for_each = local.private_v6
availability_zone = each.key
enable_resource_name_dns_aaaa_record_on_launch = true
assign_ipv6_address_on_creation = true
vpc_id = aws_vpc.main.id
ipv6_native = true
ipv6_cidr_block = each.value
tags = {
Name = "${each.key} - private v6 only"
}
}
2021-10-09 04:56:26 +00:00
resource "aws_route_table" "main" {
vpc_id = aws_vpc.main.id
}
2023-08-04 07:10:41 +00:00
resource "aws_route_table" "public_v6" {
vpc_id = aws_vpc.main.id
}
2021-10-09 04:56:26 +00:00
resource "aws_route_table_association" "public" {
2021-10-09 06:23:00 +00:00
for_each = local.public_subnets
2021-10-09 04:56:26 +00:00
subnet_id = aws_subnet.public[each.key].id
2023-08-04 07:10:41 +00:00
route_table_id = aws_route_table.public_v6.id
}
resource "aws_route_table_association" "public_v6_only" {
for_each = local.public_v6
subnet_id = aws_subnet.public_v6_only[each.key].id
route_table_id = aws_route_table.public_v6.id
}
resource "aws_route_table_association" "private_v6_only" {
for_each = local.private_v6
subnet_id = aws_subnet.private_v6_only[each.key].id
2021-10-09 04:56:26 +00:00
route_table_id = aws_route_table.main.id
}
2023-08-04 07:10:41 +00:00
2021-10-09 04:56:26 +00:00
resource "aws_route_table_association" "private" {
2021-10-09 06:23:00 +00:00
for_each = local.private_subnets
2021-10-09 04:56:26 +00:00
subnet_id = aws_subnet.private[each.key].id
route_table_id = aws_route_table.main.id
}
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.main.id
}
resource "aws_route" "main6" {
route_table_id = aws_route_table.main.id
destination_ipv6_cidr_block = "::/0"
egress_only_gateway_id = aws_egress_only_internet_gateway.main.id
}
resource "aws_route" "main" {
2021-10-09 06:23:00 +00:00
route_table_id = aws_route_table.main.id
2021-10-09 04:56:26 +00:00
destination_cidr_block = "0.0.0.0/0"
2021-10-09 06:23:00 +00:00
gateway_id = aws_internet_gateway.gw.id
2023-08-04 07:10:41 +00:00
}
resource "aws_route" "public" {
route_table_id = aws_route_table.public_v6.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
resource "aws_route" "public_v6" {
route_table_id = aws_route_table.public_v6.id
destination_ipv6_cidr_block = "::/0"
gateway_id = aws_internet_gateway.gw.id
2021-10-09 04:56:26 +00:00
}