github.com/hugorut/terraform@v1.1.3/website/docs/language/expressions/version-constraints.mdx (about) 1 --- 2 page_title: Version Constraints - Configuration Language 3 description: >- 4 Version constraint strings specify a range of acceptable versions for modules, 5 providers, and Terraform itself. Learn version constraint syntax and behavior. 6 --- 7 8 # Version Constraints 9 10 Anywhere that Terraform lets you specify a range of acceptable versions for 11 something, it expects a specially formatted string known as a version 12 constraint. Version constraints are used when configuring: 13 14 - [Modules](/language/modules) 15 - [Provider requirements](/language/providers/requirements) 16 - [The `required_version` setting](/language/settings#specifying-a-required-terraform-version) in the `terraform` block. 17 18 ## Version Constraint Syntax 19 20 Terraform's syntax for version constraints is very similar to the syntax used by 21 other dependency management systems like Bundler and NPM. 22 23 ```hcl 24 version = ">= 1.2.0, < 2.0.0" 25 ``` 26 27 A version constraint is a [string literal](/language/expressions/strings) 28 containing one or more conditions, which are separated by commas. 29 30 Each condition consists of an operator and a version number. 31 32 Version numbers should be a series of numbers separated by periods (like 33 `1.2.0`), optionally with a suffix to indicate a beta release. 34 35 The following operators are valid: 36 37 - `=` (or no operator): Allows only one exact version number. Cannot be combined 38 with other conditions. 39 40 - `!=`: Excludes an exact version number. 41 42 - `>`, `>=`, `<`, `<=`: Comparisons against a specified version, allowing 43 versions for which the comparison is true. "Greater-than" requests newer 44 versions, and "less-than" requests older versions. 45 46 - `~>`: Allows only the _rightmost_ version component to increment. For example, 47 to allow new patch releases within a specific minor release, use the full 48 version number: `~> 1.0.4` will allow installation of `1.0.5` and `1.0.10` 49 but not `1.1.0`. This is usually called the pessimistic constraint operator. 50 51 ## Version Constraint Behavior 52 53 A version number that meets every applicable constraint is considered acceptable. 54 55 Terraform consults version constraints to determine whether it has acceptable 56 versions of itself, any required provider plugins, and any required modules. For 57 plugins and modules, it will use the newest installed version that meets the 58 applicable constraints. 59 60 If Terraform doesn't have an acceptable version of a required plugin or module, 61 it will attempt to download the newest version that meets the applicable 62 constraints. 63 64 If Terraform isn't able to obtain acceptable versions of external dependencies, 65 or if it doesn't have an acceptable version of itself, it won't proceed with any 66 plans, applies, or state manipulation actions. 67 68 Both the root module and any child module can constrain the acceptable versions 69 of Terraform and any providers they use. Terraform considers these constraints 70 equal, and will only proceed if all of them can be met. 71 72 A prerelease version is a version number that contains a suffix introduced by 73 a dash, like `1.2.0-beta`. A prerelease version can be selected only by an 74 _exact_ version constraint (the `=` operator or no operator). Prerelease 75 versions do not match inexact operators such as `>=`, `~>`, etc. 76 77 ## Best Practices 78 79 ### Module Versions 80 81 - When depending on third-party modules, require specific versions to ensure 82 that updates only happen when convenient to you. 83 84 - For modules maintained within your organization, specifying version ranges 85 may be appropriate if semantic versioning is used consistently or if there is 86 a well-defined release process that avoids unwanted updates. 87 88 ### Terraform Core and Provider Versions 89 90 - Reusable modules should constrain only their minimum allowed versions of 91 Terraform and providers, such as `>= 0.12.0`. This helps avoid known 92 incompatibilities, while allowing the user of the module flexibility to 93 upgrade to newer versions of Terraform without altering the module. 94 95 - Root modules should use a `~>` constraint to set both a lower and upper bound 96 on versions for each provider they depend on.