# 错误：引用未声明的输入变量：根模块中未声明 "var.environment"

- **ID:** `terraform/terraform-validate-undeclared-variable`
- **领域:** terraform
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

配置文件引用了变量（var.*），但该模块的任何 .tf 文件中都没有使用 variable 块声明该变量。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Terraform 1.0.0 | active | — | — |
| Terraform 1.5.0 | active | — | — |
| Terraform 1.6.0 | active | — | — |

## 解决方案

1. ```
   在 .tf 文件（如 variables.tf）中声明缺失的变量，包含适当的类型和可选的默认值。
   ```
2. ```
   如果变量来自模块，确保正确引用模块的输出。使用 module.<module_name>.<output_name> 而不是 var.<name>。
   ```
3. ```
   如果不需要该变量，将引用替换为字面量或 local 值。
   ```

## 无效尝试

- **** — The .tfvars file only provides values; the variable must first be declared with a variable block in a .tf file. (95% 失败率)
- **** — Changing the prefix doesn't create the variable. 'local' requires a locals block, which is a different construct. (80% 失败率)
- **** — The issue is a missing variable declaration in the root module, not a missing module. Init doesn't add variable declarations. (90% 失败率)
