terraform
config_error
ai_generated
true
错误:引用未声明的输入变量:根模块中未声明 "var.environment"
Error: Reference to undeclared input variable: "var.environment" is not declared in the root module
ID: terraform/terraform-validate-undeclared-variable
90%修复率
85%置信度
1证据数
2024-03-01首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Terraform 1.0.0 | active | — | — | — |
| Terraform 1.5.0 | active | — | — | — |
| Terraform 1.6.0 | active | — | — | — |
根因分析
配置文件引用了变量(var.*),但该模块的任何 .tf 文件中都没有使用 variable 块声明该变量。
English
A configuration file references a variable (var.*) that has not been declared with a variable block in any of the module's .tf files.
官方文档
https://developer.hashicorp.com/terraform/language/values/variables解决方案
-
在 .tf 文件(如 variables.tf)中声明缺失的变量,包含适当的类型和可选的默认值。
-
如果变量来自模块,确保正确引用模块的输出。使用 module.<module_name>.<output_name> 而不是 var.<name>。
-
如果不需要该变量,将引用替换为字面量或 local 值。
无效尝试
常见但无效的做法:
-
95% 失败
The .tfvars file only provides values; the variable must first be declared with a variable block in a .tf file.
-
80% 失败
Changing the prefix doesn't create the variable. 'local' requires a locals block, which is a different construct.
-
90% 失败
The issue is a missing variable declaration in the root module, not a missing module. Init doesn't add variable declarations.