Lesson 4 of 4 Terraform Modules

Module Versioning

Module versions decide what code a configuration runs. This lesson covers version constraints for registry modules, pinning other sources and upgrading safely.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how to constrain registry module versions.
  • Distinguish how registry, Git and local modules are pinned.
  • Describe a safe module upgrade.

Constraining registry modules

For modules from a public or private registry, the version argument constrains which releases Terraform may install, using the same operators as provider constraints.

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "~> 6.7"   # 6.7 or later 6.x releases
}

module "dns" {
  source  = "app.terraform.io/example-org/dns/aws"
  version = "2.1.4"    # exactly this release
}

Registry modules use semantic versioning: MAJOR.MINOR.PATCH, where a new major version may contain breaking changes such as renamed variables or replaced resources. A constraint such as ~> 6.7 accepts compatible updates but not 7.0. Without a version constraint, Terraform installs the latest version, which can change your infrastructure unexpectedly when the author publishes a new major release.

The following diagram shows how an unpinned module picks up a breaking release, and the fix.

Why an untouched configuration suddenly plans replacements. With no version argument, init on a fresh clone installs the newest module release, which the author published mid-week with breaking changes. Pinning the previous version, running init -upgrade and planning again restores no changes; the upgrade can then be planned deliberately. The lock file does not record module versions.
The module source and version are illustrative.

Other sources and the lock file

The version argument is valid only for registry sources. Local paths have no versions: they always use the files on disk. Git sources are pinned with ?ref= in the source string, ideally to a tag or a commit hash, because a branch name moves as commits are added.

How to pin
Registry (public or private)version argument
Git?ref= in the source
Local pathNot applicable
Example
Registry (public or private)version = "~> 6.7"
Gitgit::https://git.example.com/vpc.git?ref=v1.4.0
Local pathVersioned with the calling repository

The dependency lock file does not record module versions. Stability therefore comes from the constraints themselves, and teams that need fully reproducible builds pin exact versions or tags and change them through reviewed pull requests.

Sort into groups

How is this module pinned?

Exam skills 5.4.1 · 5.4.2

Decide how each kind of module source is pinned to a release.

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

Upgrading modules

terraform init keeps an installed module version that still satisfies its constraint. terraform init -upgrade installs the newest version allowed. A safe upgrade follows these steps.

  1. Read the changelog

    Look for breaking changes, new required variables and resource renames.

  2. Change the constraint

    Edit version, for example from ~> 5.0 to ~> 6.0, or change the Git ref.

  3. Run terraform init -upgrade

    Install the new version.

  4. Review the plan

    Check for unexpected replacements; well-maintained modules include moved blocks for renamed resources.

  5. Apply and commit

    Apply the reviewed plan and commit the configuration change.

Moved blocks in module upgrades

Module authors who rename resources between versions should add moved blocks, so that callers' existing objects move to the new addresses instead of being destroyed and recreated.

Put in order

Upgrading a module safely

Exam skills 5.4.2

Arrange the steps of a safe module upgrade.

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

An unexpected change

A configuration calls a registry module with no version argument. On Monday a plan showed no changes; on Friday, after a fresh clone and terraform init, the plan proposes to replace several resources, though no one changed the configuration.

The decisive fact is the missing version constraint.

Without a constraint, init on the fresh clone installed the newest module version, which the author released during the week with breaking changes. The team should not apply. Adding the previous version, such as version = "5.4.1", running terraform init -upgrade (which here installs the constrained version) and planning again should return the plan to no changes; they can then plan the upgrade deliberately.

Check yourself

Does the dependency lock file record module versions?

Show answer

No. It records provider versions only; module stability comes from version constraints.

Exam Tips

Remember for the exam

  • version works for registry modules only.
  • Git sources pin with ?ref=; prefer tags or commit hashes.
  • The lock file does not record module versions.
  • init -upgrade installs the newest allowed version; review the plan.