# 错误：获取状态锁失败。锁信息：锁 ID：<id>。错误：ConditionalCheckFailedException：条件请求失败

- **ID:** `policy/terraform-state-lock-timeout-dynamodb`
- **领域:** policy
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

Terraform 通过 DynamoDB 的状态锁定在另一个进程持有锁超过默认重试超时时失败，通常是由于长时间运行的 apply 或过时的锁。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Terraform v1.5.0 | active | — | — |
| Terraform v1.6.0 | active | — | — |
| DynamoDB standard table | active | — | — |

## 解决方案

1. ```
   通过 AWS CLI 识别锁持有者：aws dynamodb get-item --table-name terraform-locks --key '{"LockID": {"S": "<lock-id>"}}'。如果持有者已过期，运行 terraform force-unlock -force <lock-id>。
   ```
2. ```
   在 terraform 块中增加锁重试超时：terraform { backend "s3" { lock_timeout = "5m" } } 以允许更长的等待。
   ```
3. ```
   实施预 apply 检查脚本，验证没有其他 CI 管道使用相同状态文件运行。
   ```

## 无效尝试

- **** — Running terraform force-unlock without verifying the lock holder can corrupt the state if the original process is still running. (60% 失败率)
- **** — Deleting the DynamoDB lock table entirely loses all lock state and requires reinitialization. (80% 失败率)
- **** — Manually editing the DynamoDB item with incorrect permissions may cause AccessDenied errors. (50% 失败率)
