# 错误：资源前置条件失败：必须满足所有前置条件

- **ID:** `terraform/lifecycle-precondition-failed`
- **领域:** terraform
- **类别:** assertion_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

资源配置中的生命周期前置条件块在计划或应用阶段评估为假。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.5.0 | active | — | — |
| 1.6.0 | active | — | — |
| 1.7.0 | active | — | — |
| 1.8.0 | active | — | — |

## 解决方案

1. ```
   检查前置条件表达式。例如，如果 precondition { condition = var.instance_count > 0 error_message = "实例计数必须为正" } 失败，请将 var.instance_count 设置为正值。使用 terraform console 测试条件：terraform console 然后评估 var.instance_count > 0。
   ```
2. ```
   如果前置条件依赖于数据源输出，请确保数据源配置正确并返回预期值。如果需要，向数据源添加 depends_on。
   ```
3. ```
   暂时删除或修改前置条件以允许应用继续，然后修复根本原因并重新添加前置条件。
   ```

## 无效尝试

- **Comment out the precondition block and re-apply without fixing the underlying condition.** — The precondition exists to enforce an invariant; bypassing it may lead to incorrect infrastructure state. (70% 失败率)
- **Change the precondition condition to always true without understanding why it failed.** — This defeats the purpose of the precondition and masks real issues. (90% 失败率)
- **Run terraform plan -refresh-only to update state before applying.** — Preconditions are evaluated during plan; refresh-only doesn't change the logical condition causing the failure. (80% 失败率)
