Terraform plan refuses a for_each built from values made during apply
Fix it fast
Most common cause, 1 of 3Map keys come from an ID or name the provider returns
Key the map by something written in your configuration, and move the generated attribute into the value.
# Before: keys are VPC IDs, unknown until the VPCs exist
# for_each = { for v in aws_vpc.example : v.id => v }
resource "aws_vpc" "example" {
for_each = var.vpcs # map(object({ cidr_block = string }))
cidr_block = each.value.cidr_block
}
resource "aws_internet_gateway" "example" {
for_each = aws_vpc.example # same static keys as var.vpcs
vpc_id = each.value.id
}Short answer
Terraform stops with Invalid for_each argument when the map keys, or set values, depend on resource attributes that only exist after apply, such as generated IDs. Terraform 1.17.2 still demands known keys; the deferral experiment ships only in alpha builds. Take keys from variables or an upstream for_each, keep IDs in the values, or apply dependencies first with -target.
Which cause do you have?
Run a plan and keep the full diagnostic. It names the file and line, says whether the problem is a map or a set, and quotes the expression that pulled in the unknown value.
terraform plan -no-color 2>&1 | grep -B2 -A16 'Invalid for_each argument'Then find the line you see in this list and open its fix:
- 1Map keys come from an ID or name the provider returns
The diagnostic says the for_each map includes keys, and the key expression reads an attribute such as .id, .arn or .email of another resource - 2A set of strings is built from apply-time attributes
The diagnostic says the for_each set includes values, and the expression wraps IDs in toset(), for example toset(aws_instance.web[*].id) - 3A data source feeding for_each is read only at apply
The for_each expression starts with data., and the plan reports that data source as read during apply instead of during planning
What does the error look like?
Error: Invalid for_each argumentThe "for_each" map includes keys derived from resource attributes that cannot be determined until apply, and so Terraform cannot determine the full set of keys that will identify the instances of this resource.The "for_each" set includes values derived from resource attributes that cannot be determined until apply
Cause 1 of 3
Map keys come from an ID or name the provider returns
You seeThe diagnostic says the for_each map includes keys, and the key expression reads an attribute such as .id, .arn or .email of another resource
Check
The for_each map includes keys derived from resource attributes when a for expression uses a value the API creates, like a service account email, as the key. Terraform needs every key during plan to name each instance, so a key that is only known after creation blocks the whole plan.
grep -rn 'for_each' --include=*.tf . | grep -E '\.(id|arn|email|name) *=>'Fix
Key the map by something written in your configuration, and move the generated attribute into the value. When the upstream resource already uses for_each over a variable, pass that resource itself as the for_each; its keys are the static ones you declared.
# Before: keys are VPC IDs, unknown until the VPCs exist
# for_each = { for v in aws_vpc.example : v.id => v }
resource "aws_vpc" "example" {
for_each = var.vpcs # map(object({ cidr_block = string }))
cidr_block = each.value.cidr_block
}
resource "aws_internet_gateway" "example" {
for_each = aws_vpc.example # same static keys as var.vpcs
vpc_id = each.value.id
}Verify
terraform plan lists one gateway per key of var.vpcs, each shown as aws_internet_gateway.example["<key>"], and the VPC ID sits only inside the value.
terraform plan -no-color | grep 'aws_internet_gateway.example\['Confirmed by developer.hashicorp.com
Cause 2 of 3
A set of strings is built from apply-time attributes
You seeThe diagnostic says the for_each set includes values, and the expression wraps IDs in toset(), for example toset(aws_instance.web[*].id)
Check
With a set, the values themselves become the instance keys, so toset() over resource IDs has the same problem as an unknown map key. Terraform's source gives this case its own message and suggests a map whose keys are fixed in configuration, with only the values holding apply-time results.
grep -rn 'toset(' --include=*.tf .Fix
Replace the set with a map. Take the keys from the upstream resource's own for_each, which come from your variables, and carry each ID as the value that the downstream block reads through each.value.
# Before: the set values are IDs created during apply
# for_each = toset([for v in aws_vpc.example : v.id])
locals {
vpc_ids = { for k, v in aws_vpc.example : k => v.id } # keys from config
}
resource "aws_internet_gateway" "example" {
for_each = local.vpc_ids
vpc_id = each.value
}Verify
The plan finishes and the count of set diagnostics drops to zero; the IDs are still filled in during apply, but only inside the values.
terraform plan -no-color 2>&1 | grep -c 'set includes values'Confirmed by raw.githubusercontent.com
Cause 3 of 3
A data source feeding for_each is read only at apply
You seeThe for_each expression starts with data., and the plan reports that data source as read during apply instead of during planning
Check
HashiCorp's data source page explains that Terraform defers reading a data block when it depends on a managed resource that changes in the same plan, or when its arguments need computed values. Until then every attribute is unknown, so a for_each over its result fails even though the data would be readable.
grep -rn -A8 '^data ' --include=*.tf . | grep -E 'depends_on|\.id\b'Fix
Remove a depends_on from the data block if it only orders things, or feed its arguments from variables instead of from resources in the same run. If the data truly depends on objects that do not exist yet, apply those objects once with -target, then run a normal plan; the plan docs reserve -target for exceptional cases like this one.
terraform apply -target=module.network
terraform planVerify
The second plan reads the data source during planning, the for_each instances appear with their keys, and no -target is needed afterwards.
terraform plan -no-color | grep -c 'Invalid for_each argument'Confirmed by developer.hashicorp.com
How do I stop it coming back?
- Treat for_each keys as names you write: declare them in a variable or local, never derive them from IDs, ARNs or emails.
- Chain resources with for_each = <upstream resource> when the relationship is one-to-one, so both sides share the same static keys.
- Run terraform plan in CI against an empty workspace once, because a key that is known in an existing state can still be unknown on a first deployment.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.