highIntermediateSymptom-first#apply#dependency#ordering#create

Error creating resource — hard dependency ordering

aws_instance.example: creating... error: InvalidVpcID.NotFound

Likely source:deploymentconnectivity

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 -30
  • Resource 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>'
READ

Check if the dependency edge exists

Fix

terraform apply -target=<RESOURCE-B> && terraform apply
CHANGE

Create 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.

1
terraform apply 2>&1 | tail -30
CHANGE

Read the exact creation error

2
terraform graph | dot -Tsvg > graph.svg
READ

Visualize the dependency order

3
terraform apply -target=<PREREQUISITE>
CHANGE

Create the prerequisite first

4
terraform apply
CHANGE

Apply 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.

Commands marked READ CHANGE DESTRUCTIVE — never run DESTRUCTIVE commands against production data you cannot restore. Copy → verify → execute.