Version constraints and dependency locking
Separate the CLI version from provider versions
Terraform configurations can depend on several independently released components. The CLI interprets your configuration; a provider implements a resource interface; a module is reusable configuration. A version constraint describes what you accept, not necessarily what is installed. This fictional provider example is for reading only; never initialize it. The CLI pin is inherited from the earlier provisional labs and is not a verified recommendation.
terraform { required_version = "= 1.14.8" required_providers { demo = { source = "registry.example.com/learning/demo" version = "~> 2.4.0" } }}The first constraint checks the installed CLI; it does not install Terraform. The second limits releases of the fictional demo provider. The provider source is its distribution identity, not its release number.
Predict which versions satisfy a constraint
Read the following five independent constraint strings, not a single combined expression.
= 2.4.1!= 2.4.1>= 2.4.0, < 3.0.0~> 2.4.0~> 2.4An exact constraint permits only 2.4.1. The exclusion rejects that release. A comma combines requirements: both comparisons must hold. The three-component pessimistic constraint accepts stable versions from 2.4.0 up to, but excluding, 2.5.0. The two-component form allows versions from 2.4.0 up to, but excluding, 3.0.0. For candidates 2.4.1, 2.4.9 and 2.5.0, the three-component form accepts the first two, while the two-component form accepts all three. Versions are compared by numeric components, not text or decimal fractions. Pre-release selection requires explicit exact targeting; do not assume ordinary ranges include it.
Combine requirements across modules
Suppose the root requests ~> 2.4.0 while a reusable child needs >= 2.4.5. Given the candidate inventory, 2.4.9 satisfies both; 2.4.1 is too old for the child, and 2.5.0 exceeds the root range. If a second child requires >= 2.5.0, no shared version exists. Fix the compatibility requirements through a reviewed change rather than deleting the lock. A reusable module usually expresses the minimum compatible provider release; an application root can impose a narrower upgrade policy. Review genuinely incompatible newer versions explicitly.
Read a provider lock record
The root working directory records provider selections in .terraform.lock.hcl. Here is a deliberately simplified JSON teaching record, not valid lockfile syntax and not actual generated data.
{ "source": "registry.example.com/learning/demo", "version": "2.4.1", "constraints": "~> 2.4.0", "hashes": "omitted: no real package supplied"}Imagine the recorded package is 2.4.1, and 2.4.9 becomes available. A normal initialization keeps 2.4.1 if it still meets the configuration. The upgrade workflow may select 2.4.9 after all constraints are checked. If the recorded version is incompatible, initialization reports a problem rather than silently treating that selection as valid. Review and commit the generated root lockfile. It tracks provider selections and package digests, not managed resource IDs or remote module releases. Pin registry module versions or immutable source revisions separately.
Make upgrades deliberate
In an approved initialized project, ordinary init is the normal dependency preparation step. An explicit upgrade can refresh providers and installed modules; it is not a CLI installer. Review the dependency changes, changelogs, security fixes, module revisions and plan before applying anything. A readonly lockfile mode prevents lock updates during repeat initialization and cannot be combined with upgrade mode. Neither mode guarantees an offline run. Use the worksheet to predict selection without invoking Terraform.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Version constraints and dependency locking — knowledge check
Check dependency selection, installation policy and package trust.
What does required_version constrain?
Which release is excluded by ~> 2.4.0?
Which version meets ~> 2.4.0 and >= 2.4.5?
A compatible provider 2.4.1 is locked; 2.4.9 is available. What does ordinary init select?
Which dependency is not locked by .terraform.lock.hcl?