terraform config_error ai_generated true

Error: Resource instance has been removed from the configuration but is still tracked in the state

ID: terraform/resource-in-config-no-longer-exists

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
1Evidence
2023-11-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Terraform v1.5.0 active
Terraform v1.6.0 active
Terraform v1.7.0 active

Root Cause

A resource was removed from the .tf configuration file but Terraform still has it in the state, causing a plan to destroy it unexpectedly.

generic

中文

从 .tf 配置文件中移除了某个资源,但 Terraform 状态中仍存在该资源记录,导致计划意外地要销毁它。

Official Documentation

https://developer.hashicorp.com/terraform/language/state/import

Workarounds

  1. 95% success Run 'terraform state rm <resource_address>' to remove the resource from state without destroying it, then run 'terraform apply' to sync.
    Run 'terraform state rm <resource_address>' to remove the resource from state without destroying it, then run 'terraform apply' to sync.
  2. 90% success If you want to destroy the resource, run 'terraform apply' directly after verifying the plan shows the intended destroy.
    If you want to destroy the resource, run 'terraform apply' directly after verifying the plan shows the intended destroy.
  3. 85% success Use 'terraform state list' to identify the full resource address, then 'terraform state rm <address>'.
    Use 'terraform state list' to identify the full resource address, then 'terraform state rm <address>'.

中文步骤

  1. 运行 'terraform state rm <资源地址>' 从状态中移除资源而不销毁它,然后运行 'terraform apply' 同步。
  2. 如果希望销毁该资源,在验证计划显示预期销毁后直接运行 'terraform apply'。
  3. 使用 'terraform state list' 识别完整资源地址,然后运行 'terraform state rm <地址>'。

Dead Ends

Common approaches that don't work:

  1. 90% fail

    The apply will execute the destroy, removing the resource permanently.

  2. 80% fail

    Manual edits often break state file format or leave orphaned dependencies.

  3. 70% fail

    Terraform will try to create a new resource, conflicting with the existing state entry.