错误:无效的 Terraform 版本约束:版本约束 "~> 1.0, >= 1.2" 包含无效的操作符或语法
Error: Invalid terraform version constraint: version constraint "~> 1.0, >= 1.2" contains invalid operators or syntax
ID: terraform/invalid-terraform-version-constraint-syntax
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Terraform v1.0 | active | — | — | — |
| Terraform v1.1 | active | — | — | — |
| Terraform v1.2 | active | — | — | — |
根因分析
required_version 块中的版本约束字符串存在语法错误,例如混合不兼容的操作符或使用不支持的字符。
English
The version constraint string in the required_version block has a syntax error, such as mixing incompatible operators or using unsupported characters.
官方文档
https://developer.hashicorp.com/terraform/language/settings#specifying-a-required-terraform-version解决方案
-
Use a single valid constraint: `terraform { required_version = ">= 1.2" }` or `required_version = "~> 1.2"` without combining multiple operators. -
If combining constraints, use only compatible operators: `terraform { required_version = ">= 1.2, < 2.0" }` (both range operators) instead of mixing `~>` and `>=`. -
Validate the constraint syntax using `terraform version` command or an online HCL validator before applying.
无效尝试
常见但无效的做法:
-
Adding a comma between constraints without checking operator compatibility (e.g., `~> 1.0, >= 1.2`)
60% 失败
The comma is allowed but the combination of `~> 1.0` (pessimistic) and `>= 1.2` can conflict; Terraform may reject ambiguous combinations.
-
Using a single equals sign (e.g., `= 1.0`) instead of `~>` or `>=`
40% 失败
Single equals is valid but too restrictive; it doesn't cause the syntax error but may fail to match expected versions, leading to a different error.
-
Removing all quotes around the version constraint string
95% 失败
Quotes are required for string values in HCL; removing them causes a parse error before version validation.