The "for_each" value depends on resource attributes that cannot be determined until apply when used as a module

│ on …/…/module/ecs-base/ecs-lb.tf line 26, in module “ecs”:
983 │ 26: for_each = var.ecs
984 │ ├────────────────
985 │ │ var.ecs is a map of object, known only after apply
986 │
987 │ The “for_each” value depends on resource attributes that cannot be
988 │ determined until apply, so Terraform cannot predict how many instances will
989 │ be created. To work around this, use the -target argument to first apply
990 │ only the resources that the for_each depends on.

The terraform plan stage fails. Tried using different versions of terraform still the same happens. Any suggestions please?

Also, This works when we run terraform plan from local but throws error when run in CI. Any idea why?

Hi @suryabharathi,

As the error message suggests, you seem to be populating that variable from a value that won’t be known until the apply step, and so it isn’t a suitable value to use for for_each.

Your two main options here will be:

  • Change how the caller defines that variable so that only the map values are unknown until apply, with the map keys always defined starically in your configuration.
  • Determine which resources are providing the data that will decide this map value and first run Terraform with the -target option to create those first, and then run Terraform normally to create everything else.

The second of these options can avoid the need to modify the configuration, but it does mean you’ll need to repeat that special extra step each time you recreate this infrastructure. If you expect to be recreating it regularly in future (for example, if it’s an ephemeral development environment) then the first option would be better to make sure you can always apply this configuration in a normal way, without any special extra steps.

Hey @apparentlymart ,

Thanks for your suggestions. :smiley: Will try it out.

Also, I tried running in local it works. Curious how would this work when run from local but not in CI? Do you have any idea?

1 Like