{
  "id": "terraform/data-source-refers-to-resource-not-yet-created",
  "signature": "Error: data.aws_subnet.selected: data source refers to resource that has not yet been created",
  "signature_zh": "错误：data.aws_subnet.selected：数据源引用了尚未创建的资源",
  "regex": "data\\.[a-zA-Z_]+\\.[a-zA-Z_]+: data source refers to resource that has not yet been created",
  "domain": "terraform",
  "category": "runtime_error",
  "subcategory": null,
  "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.",
  "root_cause_type": "generic",
  "root_cause_zh": "数据源配置为读取依赖于另一个资源先创建的 AWS 资源，但未正确声明依赖关系，导致计划时错误。",
  "versions": [
    {
      "version": "Terraform v1.5",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Terraform v1.6",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Terraform v1.7",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Adding `depends_on` to the data source pointing to the resource",
      "why_fails": "Data sources cannot use `depends_on` in Terraform; they are read-only and must rely on explicit references to infer dependencies.",
      "fail_rate": 1.0,
      "condition": "",
      "sources": []
    },
    {
      "action": "Moving the data source inside a module and calling the module with depends_on",
      "why_fails": "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.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using `terraform refresh` before plan to pre-populate the data source",
      "why_fails": "Refresh only updates existing state; it doesn't create missing resources. The data source will still fail if the underlying resource doesn't exist.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "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`.",
      "success_rate": 0.95,
      "how": "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`.",
      "condition": "",
      "sources": []
    },
    {
      "action": "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.",
      "success_rate": 0.85,
      "how": "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.",
      "condition": "",
      "sources": []
    },
    {
      "action": "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.",
      "success_rate": 0.75,
      "how": "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.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "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."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://developer.hashicorp.com/terraform/language/data-sources#data-resource-dependencies",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.84,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2023-08-01",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}