terraform runtime_error ai_generated true

Error: data.aws_subnet.selected: data source refers to resource that has not yet been created

ID: terraform/data-source-refers-to-resource-not-yet-created

Also available as: JSON · Markdown · 中文
85%Fix Rate
84%Confidence
1Evidence
2023-08-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Terraform v1.5 active
Terraform v1.6 active
Terraform v1.7 active

Root Cause

A data source is configured to read from an AWS resource that depends on another resource being created first, but the dependency is not properly declared, causing a plan-time error.

generic

中文

数据源配置为读取依赖于另一个资源先创建的 AWS 资源,但未正确声明依赖关系,导致计划时错误。

Official Documentation

https://developer.hashicorp.com/terraform/language/data-sources#data-resource-dependencies

Workarounds

  1. 95% success Ensure the data source has a direct or indirect reference to the resource: change `data.aws_subnet.selected` to reference a subnet ID from the created resource, e.g., `vpc_id = aws_vpc.main.id`.
    Ensure the data source has a direct or indirect reference to the resource: change `data.aws_subnet.selected` to reference a subnet ID from the created resource, e.g., `vpc_id = aws_vpc.main.id`.
  2. 85% success Use a local value to compute the dependency: `locals { subnet_id = aws_subnet.main.id }` then reference `local.subnet_id` in the data source's filter.
    Use a local value to compute the dependency: `locals { subnet_id = aws_subnet.main.id }` then reference `local.subnet_id` in the data source's filter.
  3. 75% success If the resource must be created before the data source, use `terraform apply -target=aws_subnet.main` first to create the resource, then run the full plan.
    If the resource must be created before the data source, use `terraform apply -target=aws_subnet.main` first to create the resource, then run the full plan.

中文步骤

  1. Ensure the data source has a direct or indirect reference to the resource: change `data.aws_subnet.selected` to reference a subnet ID from the created resource, e.g., `vpc_id = aws_vpc.main.id`.
  2. Use a local value to compute the dependency: `locals { subnet_id = aws_subnet.main.id }` then reference `local.subnet_id` in the data source's filter.
  3. If the resource must be created before the data source, use `terraform apply -target=aws_subnet.main` first to create the resource, then run the full plan.

Dead Ends

Common approaches that don't work:

  1. Adding `depends_on` to the data source pointing to the resource 100% fail

    Data sources cannot use `depends_on` in Terraform; they are read-only and must rely on explicit references to infer dependencies.

  2. Moving the data source inside a module and calling the module with depends_on 80% fail

    While modules can use depends_on, the data source itself still cannot depend on resources; the error persists because the data source has no reference to the resource.

  3. Using `terraform refresh` before plan to pre-populate the data source 90% fail

    Refresh only updates existing state; it doesn't create missing resources. The data source will still fail if the underlying resource doesn't exist.