Terraform "Reference to undeclared resource" in CI
An expression references a resource or data source address that does not exist in the configuration. The resource was never declared, was renamed/removed, or the type prefix is wrong.
What this error means
validate/plan fails with "Reference to undeclared resource", naming the address it could not resolve. It surfaces after renaming or deleting a resource, or from a typo in the resource type or name.
Error: Reference to undeclared resource
on outputs.tf line 2, in output "bucket_arn":
2: value = aws_s3_bucket.logs.arn
A managed resource "aws_s3_bucket" "logs" has not been declared in the root module.Common causes
Resource renamed or removed
A reference still points at the old address after a resource was renamed or deleted, so Terraform cannot resolve it.
Typo or wrong type/data prefix
A misspelled name, a wrong resource type, or referencing a data source without the data. prefix produces an undeclared-resource error.
How to fix it
Fix the reference to a declared address
Point the reference at the resource’s real address; for data sources include the data. prefix.
# resource reference
value = aws_s3_bucket.audit_logs.arn
# data source reference
value = data.aws_caller_identity.current.account_idReconcile after renames
- Search the config for the old address and update every reference.
- If the resource was renamed, add a
movedblock so state follows the rename. - Run
terraform validateto catch any remaining undeclared references.
How to prevent it
- Run
terraform validatein CI to catch undeclared references early. - Update all references (and add
movedblocks) when renaming resources. - Use editor/IDE Terraform tooling to flag unresolved addresses.