Skip to content
Latchkey

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.

terraform output
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.

outputs.tf
# resource reference
value = aws_s3_bucket.audit_logs.arn
# data source reference
value = data.aws_caller_identity.current.account_id

Reconcile after renames

  1. Search the config for the old address and update every reference.
  2. If the resource was renamed, add a moved block so state follows the rename.
  3. Run terraform validate to catch any remaining undeclared references.

How to prevent it

  • Run terraform validate in CI to catch undeclared references early.
  • Update all references (and add moved blocks) when renaming resources.
  • Use editor/IDE Terraform tooling to flag unresolved addresses.

Frequently asked questions

What causes ""Reference to undeclared resource""?
A reference still points at the old address after a resource was renamed or deleted, so Terraform cannot resolve it.
How do I fix "Reference to undeclared resource"?
Point the reference at the resource’s real address; for data sources include the data. prefix.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card