Lesson 2 of 3 Terraform Providers

Installing and Versioning Providers

Configurations declare which providers they need and which versions they accept, and Terraform records its choices in the dependency lock file. This lesson covers requirements, constraints and upgrades.

Learning outcomes

After completing this lesson, you will be able to:

  • Interpret provider source addresses and version constraints, including the ~> operator.
  • Explain how the dependency lock file controls provider versions.
  • Describe how to upgrade providers deliberately.

Declaring requirements

Each module declares its providers in a required_providers block inside the terraform block. Each entry maps a local name to a source address and a version constraint. The same block can set required_version to constrain the Terraform CLI.

terraform {
  required_version = ">= 1.12.0"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 6.0"
    }
    random = {
      source  = "hashicorp/random"
      version = ">= 3.6.0"
    }
  }
}

The source address has the form [HOSTNAME/]NAMESPACE/TYPE. When the hostname is omitted, as here, it is registry.terraform.io. A private registry address includes its hostname, such as app.terraform.io/example-org/internal. The local name, here aws, is what provider blocks and resource type prefixes use; it is normally the same as the type.

Version constraints

A version constraint is a string of conditions separated by commas, all of which must be true. The operators are described in the following table.

OperatorMeaningExample and versions allowed
= or noneExactly this version"6.4.0": only 6.4.0
!=Exclude this version">= 6.0, != 6.2.0": any 6.x or later except 6.2.0
> >= < <=Comparisons">= 5.0, < 7.0": 5.0 up to but not including 7.0
~>Only the rightmost component may increase"~> 6.4": 6.4 to below 7.0; "~> 6.4.0": 6.4.0 to below 6.5.0

The pessimistic constraint operator ~> deserves practice, because the number of components changes its meaning. With two components, ~> 6.4, minor and patch updates are allowed. With three, ~> 6.4.0, only patch updates are. HashiCorp recommends that root modules use ~> so that they receive fixes without unexpected major changes, and that shared child modules declare only a minimum version, such as >= 6.0, leaving the upper bound to callers. Terraform combines the constraints from all modules and selects the newest version that satisfies every one.

Sort into groups

Does the constraint allow it?

Exam skills 2.1.1 · 2.1.2

Decide whether each provider version satisfies the constraint given.

Log in to try this activity Interactive activities are free with an account.

The dependency lock file

After selecting provider versions, terraform init writes .terraform.lock.hcl in the root module directory. For each provider it records the exact version chosen, the constraints that applied and a list of checksums of the provider packages.

provider "registry.terraform.io/hashicorp/aws" {
  version     = "6.70.0"
  constraints = "~> 6.0"
  hashes = [
    "h1:...",
    "zh:...",
  ]
}

On later runs, init installs the recorded version even if a newer version satisfies the constraint, and verifies the download against the checksums, which protects against tampered packages. Commit the lock file to version control so that every teammate and pipeline uses the same versions; do not commit the .terraform directory. The lock file covers providers only. Module versions are controlled by the version arguments in module blocks.

  1. Change the constraint if needed

    Edit the version argument in required_providers, for example from ~> 5.0 to ~> 6.0.

  2. Run terraform init -upgrade

    Terraform ignores the locked versions and selects the newest versions that satisfy the constraints.

  3. Review the plan

    Provider upgrades can change defaults; run terraform plan and check for unexpected changes.

  4. Commit the lock file

    The updated .terraform.lock.hcl records the new versions for everyone.

Checksums are platform-specific. If developers use macOS and pipelines Linux, terraform providers lock -platform=darwin_arm64 -platform=linux_amd64 records checksums for both, so init does not fail on the other platform.

The following diagram shows which provider version each command selects under a constraint.

Which aws provider version each command selects under the constraint ~> 6.0. The lock file records 6.10.0, so terraform init keeps it even though 6.20.0 exists; init -upgrade selects 6.20.0 and updates the lock file; 7.0.0 is never selected, because the constraint excludes it.
Version positions are schematic.
True or false

The lock file

Exam skills 2.1.3

Check what .terraform.lock.hcl records and how init uses it.

Log in to try this activity Interactive activities are free with an account.
Worked example

Why did the version not change?

A configuration's lock file records aws provider 6.10.0 and the constraint is ~> 6.0. Version 6.20.0 has been released. A developer runs terraform init and is surprised that 6.10.0 is still installed. Is this a problem?

The decisive fact is the role of the lock file.

This is the intended behaviour: init honours the lock file so that versions change only when someone decides they should. Running terraform init -upgrade selects 6.20.0, which satisfies ~> 6.0, and updates the lock file. A release 7.0.0 would not be selected even with -upgrade, because the constraint excludes it.

Check yourself

Which versions does the constraint ~> 3.5.0 allow?

Show answer

3.5.0 and later 3.5.x patch releases, but not 3.6.0, because only the rightmost component may increase.

Exam Tips

Remember for the exam

  • required_providers maps local names to source addresses and version constraints.
  • ~> 6.4 allows 6.x from 6.4; ~> 6.4.0 allows 6.4.x only.
  • The lock file records provider versions and checksums; commit it.
  • init -upgrade is how locked versions change.