An invalid for_ach argument error occurred in the recursive call of the module(two depths).
my test code is below (dir: ./
)
resource "random_id" "this" {
byte_length = 8
}
module "module1_wrapper" {
source = "./module_1"
module_1_input = {
module_2_input = {
name = random_id.this.id
}
}
}
module_1(dir: ./module_1
)
module "wrapper" {
source = "../module_2"
for_each = var.module_1_input
module_2_input = try(each.value, null)
}
variable "module_1_input" {
type = any
default = {}
}
module_2(dir: ./module_2
)
resource "null_resource" "this" {
for_each = var.module_2_input
}
variable "module_2_input" {
type = any
default = {}
}
terraform error meesage
$ terraform plan
â•·
│ Error: Invalid for_each argument
│
│ on module_2/main.tf line 2, in resource "null_resource" "this":
│ 2: for_each = var.module_2_input
│ ├────────────────
│ │ var.module_2_input will be known only after apply
│
│ The "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.
│
│ When working with unknown values in for_each, it's better to define the map keys statically in your configuration and place apply-time results only in the map values.
│
│ Alternatively, you could use the -target planning option to first apply only the resources that the for_each value depends on, and then apply a second time to fully converge.