How to fix Error: The for_each value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created. in Terraform

TerraformINTERMEDIATEHIGHUpdated Dec 31, 2025ยท 127 views

This error occurs when for_each tries to iterate over resource attributes that are not known until apply time. Terraform requires all for_each keys to be known during the planning phase to predict resource creation.

Quick fix

Fixes Error: The for_each value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created.

# BAD - depends on resource ID (unknown at plan time)
for_each = toset(aws_instance.example[*].id)

# GOOD - uses static names from configuration
variable "instance_names" {
  type = list(string)
  default = ["web-1", "web-2", "web-3"]
}

for_each = toset(var.instance_names)
  1. Use configuration-based keys instead of resource attributes
  2. Use count instead of for_each when appropriate
  3. Split configuration into separate apply steps
  4. Move values to the map values instead of keys
terraform โ€” error
# BAD - depends on resource ID (unknown at plan time)
for_each = toset(aws_instance.example[*].id)
# GOOD - uses static names from configuration
variable "instance_names" {
type = list(string)
default = ["web-1", "web-2", "web-3"]
}
for_each = toset(var.instance_names)
Error: The for_each value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created.

What this error means

Terraform uses two distinct phases: plan and apply. During the plan phase, Terraform must determine exactly how many resources will be created or modified. The for_each meta-argument requires all its keys to be known at this point. When your for_each expression depends on values that Terraform cannot determine until the actual apply phase (such as resource IDs, outputs, or computed attributes), Terraform cannot calculate the full set of instances and fails with this error.

Typical symptoms

  • Error message appears during terraform plan or terraform apply
  • Cannot proceed with plan execution
  • Terraform cannot predict how many instances will be created
  • Configuration that previously worked now fails

Common causes

  1. Using resource IDs as for_each keys (e.g., aws_instance.example[*].id) which are only assigned during apply
  2. Referencing computed attributes from other resources as for_each keys
  3. Using outputs or data source attributes that depend on resources not yet created
  4. Passing unknown values across module boundaries as for_each arguments
  5. Using dynamic values like random_string results or timestamp functions as for_each keys

How to fix "Error: The for_each value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created."

1Use configuration-based keys instead of resource attributes

Replace for_each keys that depend on resource attributes with static values defined in your configuration. For example:

hcl
# BAD - depends on resource ID (unknown at plan time)
for_each = toset(aws_instance.example[*].id)

# GOOD - uses static names from configuration
variable "instance_names" {
  type = list(string)
  default = ["web-1", "web-2", "web-3"]
}

for_each = toset(var.instance_names)

This way, the for_each keys are known during planning.

2Use count instead of for_each when appropriate

If you need to create a fixed number of resources, use count instead:

hcl
resource "aws_instance" "example" {
  count = 3
  ami   = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

With count, you reference resources by index (aws_instance.example[0].id) rather than by key, avoiding the for_each planning issue.

3Split configuration into separate apply steps

Create the dependent resources first, then create the resources that depend on them:

1. First apply only the resources your for_each depends on:

bash
terraform apply -target=aws_instance.example

2. Then run the full apply:

bash
terraform apply

This ensures the resource attributes are known before the for_each evaluation.

4Move values to the map values instead of keys

If you need to use dynamic values in your iteration, place them as map values, not keys:

hcl
# BAD - dynamic key
for_each = {
  for name, config in var.configs :
  aws_subnet.example[name].id => config  # ID unknown at plan time
}

# GOOD - dynamic value, static key
for_each = var.configs

resource "aws_route_table_association" "example" {
  for_each = each.value
  
  subnet_id      = aws_subnet.example[each.key].id  # Referenced in resource, not for_each
  route_table_id = aws_route_table.example.id
}
5Avoid passing computed values across module boundaries

When using modules, avoid passing values that depend on resource attributes as input variables for for_each:

hcl
# BAD
module "monitoring" {
  for_each = toset(aws_instance.example[*].id)
  ...
}

# GOOD - pass instance names, derive dependencies in module
module "monitoring" {
  for_each = toset(var.instance_names)
  instance_id = aws_instance.example[each.value].id
}

This keeps the for_each logic based on known values.

6Use tomap() with conditional logic for dynamic blocks

If using dynamic blocks, ensure the iterator is known at plan time:

hcl
# Instead of conditional iteration that fails in Terraform 1.6+
dynamic "rule" {
  for_each = var.enabled ? var.rules : []
  content {
    ...
  }
}

# Use tomap() to make it work
dynamic "rule" {
  for_each = var.enabled ? tomap({for r in var.rules : r.name => r}) : tomap({})
  content {
    ...
  }
}

Advanced notes

This is a fundamental Terraform limitation by design. Terraform separates planning and applying to show users what will change before making changes. The for_each meta-argument requires knowing all keys upfront to create the execution plan. This limitation applies to both regular for_each loops and dynamic blocks.

If you truly need to create resources based on apply-time values, consider using Terraform data sources, external data sources, or post-apply automation with other tools. In some cases, using local-exec provisioners with scripts can work around this, but this approach should be used cautiously as it reduces Terraform's visibility into state changes.

For AWS specifically, using -target as a workaround is generally acceptable for initial setup, but it should not be a permanent solution to your infrastructure code design.

Frequently asked questions

What does "Error: The for_each value depends on resource attributes that cannot b" mean?
Terraform uses two distinct phases: plan and apply. During the plan phase, Terraform must determine exactly how many resources will be created or modified. The for_each meta-argument requires all its keys to be known at this point.
How do I fix "Error: The for_each value depends on resource attributes that cannot b"?
This error occurs when for_each tries to iterate over resource attributes that are not known until apply time. Terraform requires all for_each keys to be known during the planning phase to predict resource creation.
What causes "Error: The for_each value depends on resource attributes that cannot b"?
Using resource IDs as for_each keys (e.g., aws_instance.example[*].id) which are only assigned during apply Referencing computed attributes from other resources as for_each keys Using outputs or data source attributes that depend on resources not yet created
Is the "Error: The for_each value depends on resource attributes that cannot b" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading