terraform assertion_error ai_generated true

Error: Resource precondition failed: All preconditions must be met

ID: terraform/lifecycle-precondition-failed

Also available as: JSON · Markdown · 中文
85%Fix Rate
83%Confidence
1Evidence
2024-02-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.5.0 active
1.6.0 active
1.7.0 active
1.8.0 active

Root Cause

A lifecycle precondition block in a resource configuration evaluated to false during the plan or apply phase.

generic

中文

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

Official Documentation

https://developer.hashicorp.com/terraform/language/meta-arguments/lifecycle#precondition-and-postcondition

Workarounds

  1. 90% success Inspect the precondition condition expression. For example, if precondition { condition = var.instance_count > 0 error_message = "Instance count must be positive" } fails, set var.instance_count to a positive value. Use terraform console to test the condition: terraform console then evaluate var.instance_count > 0.
    Inspect the precondition condition expression. For example, if precondition { condition = var.instance_count > 0 error_message = "Instance count must be positive" } fails, set var.instance_count to a positive value. Use terraform console to test the condition: terraform console then evaluate var.instance_count > 0.
  2. 85% success If the precondition depends on data source output, ensure the data source is correctly configured and returns expected values. Add a depends_on to the data source if needed.
    If the precondition depends on data source output, ensure the data source is correctly configured and returns expected values. Add a depends_on to the data source if needed.
  3. 75% success Temporarily remove or modify the precondition to allow the apply to proceed, then fix the root cause and re-add the precondition.
    Temporarily remove or modify the precondition to allow the apply to proceed, then fix the root cause and re-add the precondition.

中文步骤

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

Dead Ends

Common approaches that don't work:

  1. Comment out the precondition block and re-apply without fixing the underlying condition. 70% fail

    The precondition exists to enforce an invariant; bypassing it may lead to incorrect infrastructure state.

  2. Change the precondition condition to always true without understanding why it failed. 90% fail

    This defeats the purpose of the precondition and masks real issues.

  3. Run terraform plan -refresh-only to update state before applying. 80% fail

    Preconditions are evaluated during plan; refresh-only doesn't change the logical condition causing the failure.