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
- Grep for the old resource address after a rename/delete.
- Update or remove every depends_on that names it.
- 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
Terraform "Reference to undeclared module" in CIFix Terraform "Reference to undeclared module" in CI -- reading module.<name>.<output> when no module block b…
Terraform module depends_on Pitfalls in CIFix Terraform module `depends_on` problems in CI - an invalid reference, a depends_on that forces everything…
Terraform "Reference to undeclared resource" in CIFix Terraform "Reference to undeclared resource" in CI - an expression references a resource address that doe…