terraform config_error ai_generated true

Error: Reference to undeclared resource

ID: terraform/invalid-reference

Also available as: JSON · Markdown
92%Fix Rate
93%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1 active

Root Cause

Terraform config references a resource that doesn't exist. Typo or missing module.

generic

Workarounds

  1. 95% success Check for typos in resource name and verify resource block exists
    grep -r 'resource "aws_instance"' .

    Sources: https://developer.hashicorp.com/terraform/language/expressions/references

  2. 88% success If in module, ensure output is declared and module source is correct
    # Use terraform console to test expressions:
    terraform console
    > aws_instance.web.id
    > module.vpc.subnet_ids
    
    # Verify the reference format:
    # resource: aws_instance.web.id
    # module output: module.name.output_name
    # variable: var.name
    # local: local.name
    # data source: data.aws_ami.latest.id

    Sources: https://developer.hashicorp.com/terraform/language/modules

Dead Ends

Common approaches that don't work:

  1. Create a dummy resource to satisfy the reference 70% fail

    Creates unneeded infrastructure, costs money

  2. Comment out the reference 65% fail

    Breaks dependent resources

Error Chain

Leads to:
Preceded by: