Commit 9634c069 authored by nimrod's avatar nimrod
Browse files

Allow deploying the VPC from scratch.

Terraform can't deploy resource with for_each if some of the information
is missing when building the plan. A workaround is to avoid having a
resource with for_each depend on a resource witha a count.
parent 2638eac3
Loading
Loading
Loading
Loading
+27 −20
Original line number Diff line number Diff line
@@ -88,6 +88,7 @@ locals {
    8 = "h"
    9 = "i"
  }
  subnet_mapping = { for i in range(var.subnet_count) : i => local.az_mapping[i + 1] }
}

resource "aws_subnet" "private" {
@@ -124,21 +125,21 @@ output "private_subnet_names" {
}

resource "aws_subnet" "public" {
  count                   = var.subnet_count
  availability_zone       = "${var.region}${local.az_mapping[count.index + 1]}"
  cidr_block              = cidrsubnet(var.cidr_block, 8, var.subnet_count + count.index)
  for_each                = local.subnet_mapping
  availability_zone       = "${var.region}${each.value}"
  cidr_block              = cidrsubnet(var.cidr_block, 8, var.subnet_count + each.key)
  map_public_ip_on_launch = true
  vpc_id                  = local.vpc_id
  tags = {
    Name = "${local.env}-public-${local.az_mapping[count.index + 1]}"
    Name = "${local.env}-public-${each.value}"
    Type = "public"
  }
}

locals {
  public_subnet_arns  = aws_subnet.public.*.arn
  public_subnet_ids   = aws_subnet.public.*.id
  public_subnet_names = [for i in aws_subnet.public.*.tags : i["Name"]]
  public_subnet_arns  = [for i in aws_subnet.public : i.arn]
  public_subnet_ids   = [for i in aws_subnet.public : i.id]
  public_subnet_names = [for i in aws_subnet.public : i.tags["Name"]]
}

output "public_subnet_arns" {
@@ -183,17 +184,23 @@ output "nat_gateway_eip_names" {
}

resource "aws_nat_gateway" "gateway" {
  count         = var.subnet_count
  allocation_id = local.nat_gateway_eip_ids[count.index]
  subnet_id     = local.public_subnet_ids[count.index]
  for_each = {
    for i in range(var.subnet_count) : i => {
      nat_eip_id = local.nat_gateway_eip_ids[i]
      subnet_id  = local.public_subnet_ids[i]
      az         = local.az_mapping[i + 1]
    }
  }
  allocation_id = each.value.nat_eip_id
  subnet_id     = each.value.subnet_id
  tags = {
    Name = "${local.env}-${local.az_mapping[count.index + 1]}"
    Name = "${local.env}-${each.value.az}"
  }
}

locals {
  nat_gateway_ids   = aws_nat_gateway.gateway.*.id
  nat_gateway_names = [for i in aws_nat_gateway.gateway.*.tags : i["Name"]]
  nat_gateway_ids   = [for i in aws_nat_gateway.gateway : i.id]
  nat_gateway_names = [for i in aws_nat_gateway.gateway : i.tags["Name"]]
}

output "nat_gateway_ids" {
@@ -240,9 +247,9 @@ output "public_route_table_name" {
}

resource "aws_route_table_association" "public" {
  for_each       = toset(local.public_subnet_ids)
  for_each       = aws_subnet.public
  route_table_id = local.public_route_table_id
  subnet_id      = each.key
  subnet_id      = each.value.id
}

locals {
@@ -255,10 +262,10 @@ output "public_route_table_association_ids" {
}

resource "aws_route_table" "private" {
  for_each = toset(local.nat_gateway_ids)
  for_each = aws_nat_gateway.gateway
  vpc_id   = local.vpc_id
  tags = {
    Name = "${local.env}-private-${local.az_mapping[index(local.nat_gateway_ids, each.key) + 1]}"
    Name = "${local.env}-private-${local.az_mapping[index(local.nat_gateway_ids, each.value.id) + 1]}"
  }

  route {
@@ -289,9 +296,9 @@ output "private_route_table_names" {
}

resource "aws_route_table_association" "private" {
  for_each       = zipmap(local.private_subnet_ids, local.private_route_table_ids)
  route_table_id = each.value
  subnet_id      = each.key
  for_each       = aws_route_table.private
  route_table_id = each.value.id
  subnet_id      = local.private_subnet_ids[index(local.private_route_table_ids, each.value.id)]
}

locals {