解决 Terraform 报错 Error acquiring the state lock 状态锁死
分析 Terraform 执行报 Error acquiring the state lock 诱因,详解 terraform force-unlock 命令与锁 ID 清除规范。
问题现象
在执行 terraform plan 或 terraform apply 时命令直接被阻断退出:
Error: Error acquiring the state lock
Lock Info:
ID: 1a2b3c4d-5e6f-7g8h-9i0j-1k2l3m4n5o6p
Path: my-bucket/terraform.tfstate
Operation: OperationTypeApply
Who: user@ci-runner
Created: 2026-11-15 10:20:15
Terraform acquires a state lock to protect the state from being written
by multiple users at the same time. Please resolve the issue above and try
again. For most backends, you can force-unlock the state using the following
command:
terraform force-unlock 1a2b3c4d-5e6f-7g8h-9i0j-1k2l3m4n5o6p
触发根因
Terraform 为了防御团队多人协作或多流水线同时修改基础设施状态导致状态文件(tfstate)损毁,在远端(AWS S3 + DynamoDB / Consul / GitLab HTTP)维护了一把互斥分布式锁。若上一次执行被强制按 Ctrl+C 中断或 CI/CD 容器异常崩溃,锁未被正常释放,遗留在数据库中。
解决方案
使用官方 force-unlock 命令安全解锁
根据报错信息中明确提供的 Lock ID,执行强制解锁命令:
terraform force-unlock 1a2b3c4d-5e6f-7g8h-9i0j-1k2l3m4n5o6p
控制台提示确认解锁,输入 yes 即可原子清除锁状态。
验证方法
再次执行命令:
terraform plan
顺利获取远端状态并完成执行计划计算,状态锁死彻底排除。


