Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Providers and Project Initialization  ›  Lesson

Provider and Terraform Versions

Versions 12 minVersion constraints such as ~> 5.0 say which provider and Terraform versions a configuration accepts, and the dependency lock file records the exact provider version chosen so every run uses the same one.

Provider and Terraform Versions

Providers release new versions often. If Crumb's pipeline quietly picks up a new major version with breaking changes, a routine plan can suddenly want to change things nobody touched. Version constraints prevent that surprise.

The rule

Constrain the range in code; the lock file freezes the exact pick.

A version constraint is a string of conditions:

terraform {
  required_version = ">= 1.6.0"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}
  • required_version limits which Terraform CLI versions may run this configuration.
  • version inside required_providers limits which provider versions are acceptable.

Operators: =, !=, >, >=, <, <=, and the pessimistic operator ~>, which allows only the rightmost number given to grow:

Constraint Allows
~> 5.0 5.0 and up, below 6.0
~> 5.31 5.31 and up, below 6.0
~> 5.31.0 5.31.0 and up, below 5.32.0
>= 5.0, < 5.50 a range written out in full

The constraint is a range. The dependency lock file, .terraform.lock.hcl, records the exact provider version that terraform init chose, plus checksums. Commit it. From then on everyone gets that same version, even if a newer one in the range exists. To move forward on purpose, run terraform init -upgrade, which picks the newest version the constraints allow and rewrites the lock file.

The lock file covers providers only. It does not pin the Terraform CLI, which required_version handles, nor module versions, which have their own version argument on each module call.

The shortcut

For ~>, cover the last number with your thumb; everything to its left is frozen. ~> 5.31 freezes the 5; ~> 5.31.0 freezes 5.31.

Reading Crumb's AWS constraintStep 1 of 4
  1. Read the constraint

    ~> 5.0 has two numbers. Cover the last one: 5 stays frozen.

The trap: leaving the lock file out of Git. Each machine then chooses its own version inside the range, and plans differ between laptops.

Drills

Answer fast — trust the rule.

quick drill

Which versions does ~> 5.0 allow?

quick drill

Which versions does ~> 5.31.0 allow?

quick drill

What does .terraform.lock.hcl record?

quick drill

A newer 5.x provider is released. Your constraint is ~> 5.0 and the lock file says 5.61.0. What does a plain terraform init install?

quick drill

Which setting limits the Terraform CLI version itself?

Remember

  • Constraints set the allowed range; the lock file freezes the exact version. Commit it.
  • Shortcut for ~>: cover the last number; the rest is frozen.
  • Trap: no lock file in Git means different versions on different machines.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainPin it safely

A Crumb teammate got a different AWS provider version than you and their plan looks different. Explain how version constraints and the .terraform.lock.hcl file prevent this, what ~> 5.0 allows, and how you would deliberately move to a newer version.