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
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 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-dependenciesWorkarounds
-
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`.
-
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. -
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.
中文步骤
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`.
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.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:
-
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.
-
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.
-
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.