terraform config_error ai_generated true

Error: Invalid interpolation in variable default: Variables may not be used here

ID: terraform/invalid-interpolation-in-variable-default

Also available as: JSON · Markdown · 中文
85%Fix Rate
85%Confidence
1Evidence
2023-08-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.5.0 active
1.6.0 active
1.7.0 active

Root Cause

Terraform variables cannot reference other variables in their default values; defaults must be literal values.

generic

中文

Terraform 变量在其默认值中不能引用其他变量,默认值必须是字面值。

Official Documentation

https://developer.hashicorp.com/terraform/language/values/variables

Workarounds

  1. 95% success Replace the variable reference with a literal value, or use a local value to compute the default after variable declaration.
    Replace the variable reference with a literal value, or use a local value to compute the default after variable declaration.
  2. 90% success Define a separate variable with no default and use a locals block to set the default based on other variables.
    Define a separate variable with no default and use a locals block to set the default based on other variables.

中文步骤

  1. 将变量引用替换为字面值,或使用 local 值在变量声明后计算默认值。
  2. 定义一个无默认值的单独变量,并使用 locals 块根据其他变量设置默认值。

Dead Ends

Common approaches that don't work:

  1. 95% fail

    Adding escape characters or quotes around the interpolation does not resolve the fundamental limitation.

  2. 80% fail

    Moving the interpolation to a locals block still fails because the variable default is evaluated before locals are available.