Skip to content
shiporbreak

No streak yet

Sign in

November 29, 2026 路 #74

TerraformHardOpen write-up

Keys Terraform cannot know yet

On a fresh environment, terraform plan fails with an error that the for_each set includes values that cannot be determined until apply. Which line triggers it?

variable "azs" {
  default = ["us-east-1a", "us-east-1b"]
}

resource "aws_subnet" "app" {
  for_each          = toset(var.azs)
  vpc_id            = aws_vpc.main.id
  cidr_block        = cidrsubnet(aws_vpc.main.cidr_block, 8, index(var.azs, each.key))
  availability_zone = each.key
}

resource "aws_route_table_association" "app" {
  for_each       = toset([for s in aws_subnet.app : s.id])
  subnet_id      = each.value
  route_table_id = aws_route_table.private.id
}

Next puzzle in UTC

One try. The first answer sticks.

How to fix it

for_each keys must be known during planning. Subnet IDs are assigned by AWS at apply, so a set of them is unknown. Use for_each = aws_subnet.app and read each.value.id, so keys are the known AZ names.

Terraform decides which instances of a resource exist during plan, before anything is created. For for_each, that means the keys must be known at plan time (values may be unknown). A subnet's id is only known after AWS creates it, so toset([for s in aws_subnet.app : s.id]) has unknown members. Terraform reports "The for_each set includes values derived from resource attributes that cannot be determined until apply". It only bites when the subnets don't exist yet, which is why it shows up on a fresh environment but passes in existing ones.

Fix

Iterate over the resource map itself. Its keys are the AZ names from var.azs, which Terraform knows at plan time:

resource "aws_route_table_association" "app" {
  for_each       = aws_subnet.app
  subnet_id      = each.value.id
  route_table_id = aws_route_table.private.id
}

The general rule is to build keys from input variables, locals, or other static data, and pull unknown attributes in through each.value. Avoid -target workarounds that apply the subnets first, because they leave your configuration unable to create the environment in one run.

Sponsor this spot

Put your tool on the puzzle
One labeled card, in front of engineers who show up every day from YouTube, TikTok, and Instagram.