Terraform 状态 Lock
错误信息
错误: 错误获取状态 lock
快速摘要
Another Terraform 处理 or stale lock 是持有状态 lock, preventing current 操作从 proceeding.
为什么发生
Another team 成员 or CI 作业是 运行 Terraform simultaneously
上一个 Terraform 运行是 interrupted and 左 stale lock
lock 表/bucket 是 in 不同区域 or account
Permissions 到 lock 表/bucket 是 insufficient
最小示例
$ terraform apply
Error: Error acquiring the state lock
Lock Info:
ID: 12345-abcde# Verify no one else is running terraform
# Then force unlock if stale
terraform force-unlock 12345-abcde
terraform apply # now worksstale lock 是 preventing apply. fix force-unlocks 它 (仅如果你 是确定否 one else 是运行 Terraform).
如何诊断
检查如果 another Terraform 处理是 运行
View lock 信息:
terraform force-unlock -信息 <lock-id>(如果 supported)检查 DynamoDB 表 (for AWS S3 backend) or Consul for lock 条目
验证 backend 配置 in Terraform code
如何修复
Wait for other Terraform 操作到 完整
Force unlock 如果 lock 是 stale:
terraform force-unlock <lock-id>Fix backend 配置到 点到 正确 lock 表/bucket
Ensure IAM 用户有 permissions 到 lock 表
如何预防
Use remote backend 使用 locking (S3 + DynamoDB, or Terraform Cloud)
坐标 Terraform 运行 in CI 到 avoid concurrent 操作
Use CI queue 到 serialize Terraform 运行