terraform config_error ai_generated true

Error: Invalid terraform version constraint: version constraint "~> 1.0, >= 1.2" contains invalid operators or syntax

ID: terraform/invalid-terraform-version-constraint-syntax

Also available as: JSON · Markdown · 中文
90%Fix Rate
82%Confidence
1Evidence
2023-04-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Terraform v1.0 active
Terraform v1.1 active
Terraform v1.2 active

Root Cause

The version constraint string in the required_version block has a syntax error, such as mixing incompatible operators or using unsupported characters.

generic

中文

required_version 块中的版本约束字符串存在语法错误,例如混合不兼容的操作符或使用不支持的字符。

Official Documentation

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

Workarounds

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

中文步骤

  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.

Dead Ends

Common approaches that don't work:

  1. Adding a comma between constraints without checking operator compatibility (e.g., `~> 1.0, >= 1.2`) 60% fail

    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% fail

    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% fail

    Quotes are required for string values in HCL; removing them causes a parse error before version validation.