Module sources and versions
Terraform 004 · Lesson 12 of 18 · 1:28:05
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the HashiCorp Terraform Associate (004) exam. In this lesson: modules and local paths, public and private registry sources, Git and other sources with a ref, and module versions pinned with the version argument. Includes three original practice questions, with Bluefin Logistics, a fictional parcel delivery company, as the example.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
Code written once
Bluefin's teams copy the same network code into every project, and each copy drifts a little further from the others. Modules package that code once, and every configuration calls it. This lesson covers what a module is, where Terraform gets modules from, and how to pin their versions.
Modules and local paths
A module is simply the set of dot T F files in one directory. The directory where you run Terraform is the root module, and it calls child modules with module blocks, each with a source argument that says where the code comes from. A local path starts with dot slash or dot dot slash, and Terraform uses those files in place, without downloading anything. Other sources are downloaded into the dot terraform directory by terraform init. So you run terraform init again whenever you add a module, or change its source.
Registry sources
The public Terraform Registry gives each module an address with three parts: a namespace, a name, and the provider that it targets. A private registry, like the one in HCP Terraform, adds its hostname in front, here app dot terraform dot io, followed by the organization. The registry page of a module shows how to call it, and the inputs that it requires. And the partner filter of the registry lists partner modules, which HashiCorp reviews for stability and compatibility.
Git and other sources
Modules can also come from GitHub or Bitbucket, from any Git server with the git double colon prefix, or from Mercurial, an HTTP address, or a bucket in S3 or Google Cloud Storage. With Git, the ref parameter in the address selects a branch, a tag, or a commit, and a tag is the usual choice. Without ref, Terraform takes the default branch, so the code can change under you at the next init. Prefer tags or commits that you control, so that every init gets the same code.
Module versions
For a registry module, the version argument takes a constraint, like the ones that you write for providers. Terraform uses an installed version that meets the constraint, or downloads the newest one allowed. Unlike providers, modules are not recorded in the dependency lock file, so on a fresh clone, a loose constraint can bring a newer version than your teammates have. To get the same code every time, pin an exact version, or a Git tag, and upgrade on purpose with terraform init and the upgrade option. Local paths and Git sources do not accept the version argument at all.
Recap
Let's recap. A module is a directory of dot T F files, and module blocks call child modules through their source argument. Local paths are used in place, while registry, Git, and other sources are downloaded by terraform init. Registry addresses have a namespace, a name, and a provider, and Git sources pin a tag or a commit with ref. And registry modules take a version constraint, but they are not in the lock file, so pin a version to get the same code every time.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
A module block has source = "../modules/network". What does terraform init do with this module?
- Downloads it from the public registry
- Clones it from the default Git server
- Uses the files in place, without downloading
- Asks for its version argument
Show the answer
Uses the files in place, without downloading
A source that starts with dot slash or dot dot slash is a local path, and Terraform uses those files in place. “Downloads it from the public registry” is wrong, because registry addresses never start with dots. “Clones it from the default Git server” is wrong, because a Git source needs a Git address, or the git double colon prefix. And “Asks for its version argument” is wrong, because a local module cannot take a version argument at all.
The network module is in a GitHub repository, and every init must get exactly the release tagged v1.4.0. Which source fits?
- github.com/bluefin/network
- github.com/bluefin/network?ref=v1.4.0
- github.com/bluefin/network?version=1.4.0
- ./network?ref=v1.4.0
Show the answer
github.com/bluefin/network?ref=v1.4.0
With Git sources, the ref parameter selects a tag, a branch, or a commit, and a tag pins the release. “github.com/bluefin/network” would take the default branch, which can change at the next init. “github.com/bluefin/network?version=1.4.0” is wrong, because the parameter is called ref, and version is an argument of registry modules. And “./network?ref=v1.4.0” is wrong, because a local path is used in place, and takes no ref.
A public registry module has version = "~> 1.2". Version 1.3 was just published, and a teammate runs terraform init on a fresh clone. Which version is installed?
- 1.3, the newest one the constraint allows
- 1.2, the version in the lock file
- 1.2.0 exactly, the lowest allowed
- None, until init gets -upgrade
Show the answer
1.3, the newest one the constraint allows
The constraint allows any version from one point two up to, but not including, two point zero, and modules are not in the lock file, so init takes the newest one allowed. “1.2, the version in the lock file” is the trap: the lock file tracks providers, never modules. “1.2.0 exactly, the lowest allowed” is wrong, because Terraform picks the newest allowed version, not the lowest. And “None, until init gets -upgrade” is wrong, because a fresh clone has no module installed yet, so init simply installs one.