Enforcing a Specific Provider Version
For example, if the registry’s default for the local provider is version 2.0.0 but you want to use an older version (e.g., 1.4.0), follow these steps:- Click on the “Use Provider” tab in the registry to copy the code snippet.
- Add a new
terraformblock to your configuration with a required_providers block that includes version constraints.
Using version constraints helps avoid unexpected breaking changes when new major provider versions are released.
Using Version Constraints
Terraform supports various operators to control which provider versions are installed. Below are some practical examples.Excluding a Specific Version
To ensure that a particular version (e.g., 2.0.0) is never used, utilize the “not equal” operator:terraform init with this constraint ensures that version 2.0.0 is excluded.
Using Comparison Operators
You can also specify constraints using standard comparison operators. For example, to allow only versions less than 1.4.0:Using the Pessimistic Constraint Operator
Terraform supports the pessimistic constraint operator (~>), which allows you to specify a minimal version while permitting updates that do not modify the leftmost version digit. For example, the following configuration permits incremental updates to any version starting from 1.2 up to, but not including, 2.0:
Including descriptive version constraint explanations in your documentation improves both clarity for developers and your article’s search engine visibility.
By applying these version constraints, you ensure that your Terraform configurations use the intended provider versions, preventing unwanted upgrades that could introduce breaking changes. This practice is essential for maintaining a stable and predictable infrastructure deployment workflow. For further information, refer to the Terraform Documentation and the Terraform Registry.