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.

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.
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.
How is this module pinned?
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.
-
Read the changelog
Look for breaking changes, new required variables and resource renames.
-
Change the constraint
Edit version, for example from ~> 5.0 to ~> 6.0, or change the Git ref.
-
Run terraform init -upgrade
Install the new version.
-
Review the plan
Check for unexpected replacements; well-maintained modules include moved blocks for renamed resources.
-
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.
Upgrading a module safely
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.
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.