resource "aws_internet_gateway" "this" { count = var.create_vpc ? 1 : 0 vpc_id = aws_vpc.this[0].id tags = merge( local.common_tags, { Name = "${var.name}-igw" } ) } resource "aws_eip" "nat" { for_each = var.create_vpc ? (var.single_nat_gateway ? { shared = local.azs[0] } : { for az in local.azs : az => az }) : {} domain = "vpc" tags = merge( local.common_tags, { Name = "${var.name}-nat-eip-${each.key}" } ) } resource "aws_nat_gateway" "this" { for_each = var.create_vpc ? (var.single_nat_gateway ? { shared = local.azs[0] } : { for az in local.azs : az => az }) : {} allocation_id = aws_eip.nat[each.key].id subnet_id = aws_subnet.public[each.value].id tags = merge( local.common_tags, { Name = "${var.name}-nat-${each.key}" } ) depends_on = [aws_internet_gateway.this] } resource "aws_route_table" "public" { count = var.create_vpc ? 1 : 0 vpc_id = aws_vpc.this[0].id route { cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.this[0].id } tags = merge( local.common_tags, { Name = "${var.name}-public-rt" } ) } resource "aws_route_table_association" "public" { for_each = var.create_vpc ? aws_subnet.public : {} subnet_id = each.value.id route_table_id = aws_route_table.public[0].id } resource "aws_route_table" "private" { for_each = var.create_vpc ? aws_subnet.private : {} vpc_id = aws_vpc.this[0].id route { cidr_block = "0.0.0.0/0" nat_gateway_id = aws_nat_gateway.this[var.single_nat_gateway ? "shared" : each.key].id } tags = merge( local.common_tags, { Name = "${var.name}-private-rt-${each.key}" } ) } resource "aws_route_table_association" "private" { for_each = var.create_vpc ? aws_subnet.private : {} subnet_id = each.value.id route_table_id = aws_route_table.private[each.key].id }