FAQ | This is a LIVE service | Changelog

Refactor module.webapp_http_load_balancer for_each statement

The following error is thrown on first run with terraform >= 1.0.

│ Error: Invalid for_each argument



│   on /terraform_data/modules/webauth_shim/load_balancer.tf line 23, in module "webapp_http_load_balancer":

│   23:   for_each = toset([for neg in google_compute_region_network_endpoint_group.webapp : neg.id])

│     ├────────────────

│     │ google_compute_region_network_endpoint_group.webapp is tuple with 1 element



│ 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.

We've seen this before when keys in a for_each loop are referencing unknown values. The best way around this is to refactor this to supply a map to the for_each where the keys are always static (the array index for example) and only the values are variable/unknown.