Error: Reference to undeclared resource
Fixes OpenTofu's 'Error: Reference to undeclared resource' by finding the dangling reference. Use when tofu plan or validate names a resource address that has no block, usually after a rename or delete. Not for undeclared variables or data sources.
TL;DR: Something in your config references a resource that doesn't exist: a typo, a deleted block, or a renamed label whose references weren't updated. The error names the file, line, and address. Fix the reference, or add the missing resource back.
Error: Reference to undeclared resource
on retire.tf line 5, in output "build_info_path":
5: value = local_file.build_info.filename
There is no managed resource "local_file" "build_info" definition in the root
module.Steps
- Read the address in the error (
local_file.build_infoabove) and the location of the reference (file, line).
Expected: you know exactly which reference is dangling.
- Search for the resource block:
grep -rn 'resource "local_file" "build_info"' .
Expected: either no block (it was deleted or never existed) or a block with a different label (it was renamed).
- Fix it one of these ways:
- Typo: correct the reference to the real label.
- Deleted by accident: restore the block.
- Renamed on purpose: update every reference (outputs, locals, other resources), or add a
movedblock so tofu tracks the rename. - Retiring on purpose: delete the consumers too, or use a
removedblock to forget it without destroying.
Expected: no dangling references remain.
- Run
tofu validate.
Expected: Success! The configuration is valid.
When this applies
tofu planortofu validatefails naming atype.nameaddress withno ... definition in the root module(or in a child module).- Right after renaming or deleting a resource block.
When it doesn't apply
Error: Reference to undeclared input variableis the variable version; same idea, different fix location.Error: Reference to undeclared data sourcemeans adatablock is missing.- A reference that exists but has
count = 0fails differently (Invalid index); that's not this error.
Tool versions
All OpenTofu versions.
Why it happens
References are resolved statically before anything is planned. A reference to an address with no block is a config bug by definition, so tofu fails fast and names every dangling reference instead of guessing.
Edge cases
- Renames dangle in bulk: renaming a resource label breaks every reference at once. A
moved { from, to }block renames the state address too, so you don't get this error at all. tofu state mvrenames state without touching config; if you do one without the other, expect this error or a destroy-and-recreate plan.- Module-internal references say
in module "name"instead ofin the root module; fix them inside the module source.