terraform config_error ai_generated true

错误:无效的 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

其他格式: JSON · Markdown 中文 · English
90%修复率
82%置信度
1证据数
2023-04-12首次发现

版本兼容性

版本状态引入弃用备注
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.

generic

官方文档

https://developer.hashicorp.com/terraform/language/settings#specifying-a-required-terraform-version

解决方案

  1. Use a single valid constraint: `terraform { required_version = ">= 1.2" }` or `required_version = "~> 1.2"` without combining multiple operators.
  2. If combining constraints, use only compatible operators: `terraform { required_version = ">= 1.2, < 2.0" }` (both range operators) instead of mixing `~>` and `>=`.
  3. Validate the constraint syntax using `terraform version` command or an online HCL validator before applying.

无效尝试

常见但无效的做法:

  1. 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.

  2. 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.

  3. 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.