Skip to content
Latchkey

Terraform depends_on "Reference to undeclared resource" in CI

A depends_on lists a resource address that does not exist in the configuration -- a typo, a removed resource, or a wrong type/name -- so Terraform cannot resolve the dependency.

What this error means

validate/plan fails with "Reference to undeclared resource" inside a depends_on. It appears after deleting or renaming a resource that another block still depends on.

terraform
Error: Reference to undeclared resource

  on main.tf line 9, in resource "aws_instance" "app":
   9:   depends_on = [aws_security_group.legacy]

A managed resource "aws_security_group" "legacy" has not been declared in the
root module.

Common causes

depends_on target deleted or renamed

The referenced resource was removed or renamed, but the depends_on entry still names the old address.

Wrong type or name in the address

A typo in the resource type or local name makes the address resolve to nothing.

How to fix it

Reference a declared resource, or drop the entry

Point depends_on at a resource that exists, or remove the stale dependency entirely.

main.tf
resource "aws_instance" "app" {
  # ...
  depends_on = [aws_security_group.app]   # an actually-declared resource
}

Clean up after renames

  1. Grep for the old resource address after a rename/delete.
  2. Update or remove every depends_on that names it.
  3. Run validate to confirm no undeclared references remain.

How to prevent it

  • Update depends_on entries when renaming or deleting resources.
  • Prefer implicit dependencies over explicit depends_on.
  • Run terraform validate in CI to catch undeclared references.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →