Skip to content

Erroneous 'Invalid for_each argument' when interpolating const map #22735

@OJFord

Description

@OJFord

Terraform Version

0.12.8

Terraform Configuration Files

locals {
  server_count = {
    location1 = {
        type1 = 3
    }
  }

  server_set = toset(flatten([
    for loc in keys(local.server_count) : [
      for type in keys(local.server_count[loc]) : [
        for i in range(local.server_count[loc][type]) : "${loc}_${type}_${i}"
      ]
    ]
  ]))
}

resource "" "" {
  for_each = local.server_set

  # ...
}

Expected Behavior

Successful plan.

Actual Behavior

Error: Invalid for_each argument

The "for_each" value depends on resource attributes that cannot be determined
until apply, so Terraform cannot predict how many instances will be created.
To work around this, use the -target argument to first apply only the
resources that the for_each depends on.

Steps to Reproduce

  1. Config as described
  2. terraform plan

Additional Context

As expected, terraform apply -target=locals.server_set does not help:

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions