Loading...
Loading...
### Terraform Version ```shell Terraform v1.16.0-rc1 on linux_amd64 ``` ### Terraform Configuration Files Since #38352, `import` blocks can be declared in child modules. In practice they only work there if their arguments are literals. Two cases, both failing at `terraform plan`, both passing when moved to the root module. **Case A - `id` referencing a variable of the module** `main.tf`: ```terraform module "child" { source = "./mod" name = "example" } ``` `mod/main.tf`: ```terraform variable "name" { type = string } resource "terraform_data" "this" { input = var.name } import { to = terraform_data.this id = var.name } ``` **Case B - `for_each` referencing a data source of the module** `main.tf`: ```terraform module "child" { source = "./mod" } ``` `mod/ids.txt`: ``` a,b ``` `mod/main.tf`: ```terraform terraform { required_providers { local = { source = "hashicorp/local" } } } data "local_file" "existing" { filename = "${path.module}/ids.txt" } locals { existing = split(",", data.local_file.existing.content) } resource "terraform_data" "this" { for_each = toset(["a", "b"]) input = each.key } import { for_each = toset(local.existing) to = terraform_data.this[each.key] id = each.key } ``` ### Debug Output ``` ``` ### Expected Behavior Both plans succeed, as they do when the same configuration is written in the root module. ### Actual Behavior Case A: ``` Error: Reference to undeclared input variable on mod/main.tf line 11, in import: 11: id = var.name An input variable with the name "name" has not been declared. This variable can be declared with a variable "name" {} block. Error: Invalid import id argument on mod/main.tf line 11, in import: 11: id = var.name The import block "id" argument depends on resource attributes that cannot be determined until apply, so Terraform cannot plan to import this resource. ``` `var.name` *is* declared in `mod/main.tf`. The second error looks like a knock-on effect of the first: nothing here depends on a resource attribute. Case B: ``` Error: Reference to uninitialized local value on mod/main.tf line 23, in import: 23: for_each = toset(local.existing) The local value local.existing was not processed by the most recent operation, this likely means the previous operation either failed or was incomplete due to targeting. ``` The data source is read, but apparently not before the `import` block is expanded. ### Steps to Reproduce 1. Create the files above 2. `terraform init` 3. `terraform plan` ### Additional Context Two things that may help narrowing it down: - In case A, the `id` argument seems to be evaluated in the **root** module scope. Declaring `name` only in the root module, and giving the child module a differently named variable, makes the plan succeed and import `"resolved-from-root"`. `for_each` and `to` do use the child module scope, hence the asymmetry. - In case B, the failure disappears if the resource's own `for_each` also references the same data source, which suggests the `import` block's references do not create a dependency edge of their own. ### References - #38352 - #33474 ### Generative AI / LLM assisted development? Claude assisted me to narrow down the reproduction cases and to write this report
Click on a version to see all relevant bugs
Terraform Integration
Learn more about where this data comes from
BugZero Plan
Streamline upgrades with automated vendor bug scrubs
BugZero Prevent
Wish you caught this bug sooner? Get proactive today.