Error creating resource — hard dependency ordering
aws_instance.example: creating... error: InvalidVpcID.NotFound
Focused incident mode — strips away everything but this runbook.
Symptoms
- aws_instance.example: creating... error: InvalidVpcID.NotFound
- Resource creation fails because prerequisite resource does not exist yet
- terraform apply fails even though the config is correct
Evidence to grab
Collect these artifacts before you change anything — they prove (or rule out) each cause.
Apply error output
terraform apply 2>&1 | tail -30Resource dependency graph
terraform graph | grep -A5 '<RESOURCE-ADDR>'
Diagnostic flow
Run the steps in order, marking each claim confirmed or refuted. The evidence you gather feeds the ranked causes below.
Guided decision tree is ready.
Steps with commands, expected output, and pass/fail branches.
Ranked root causes
Evidence checklist
- Resource A needs Resource B but does not reference any attribute of B
- Terraform creates them in parallel or wrong order
Confirm before fixing
terraform graph | grep '<RESOURCE-A>' | grep '<RESOURCE-B>'Check if the dependency edge exists
Fix
terraform apply -target=<RESOURCE-B> && terraform applyCreate the prerequisite first, then apply the rest
Resolution runbook
Apply from top to bottom after the cause is confirmed. Review every command before running it — risk tags are shown on each.
terraform apply 2>&1 | tail -30Read the exact creation error
terraform graph | dot -Tsvg > graph.svgVisualize the dependency order
terraform apply -target=<PREREQUISITE>Create the prerequisite first
terraform applyApply the remaining resources
Prevention
- Always reference resource attributes to create implicit dependencies.
- Use depends_on for cross-module dependencies that cannot be expressed via references.
- Use -target in CI for complex multi-phase applies.
- Test applies in a staging environment before production.
Related runbooks
Same stack, same failure family — worth a skim.
Error: Cycle: resource A -> resource B -> resource A
terraform plan shows - destroy for resources that should exist
Commands marked READ CHANGE DESTRUCTIVE — never run DESTRUCTIVE commands against production data you cannot restore. Copy → verify → execute.