Module sources and versions
Choose where module code comes from
A module source tells Terraform where to locate a child configuration. Choose a relative local path for code reviewed in the same repository, a registry package for independently versioned releases, or a VCS source for a selected repository revision. Source selection is distinct from input values and provider version selection. The examples below illustrate source syntax; only the local module from the preceding lesson is intended for optional execution.
Local paths follow the calling module
This relative path resolves from the directory of the module containing the call, not necessarily the root directory. It uses the same repository revision as the surrounding code when both are committed together.
module "catalog" { source = "./modules/label" name = "preview-catalog"}Local paths do not support the registry version argument. Record the repository revision instead. Terraform needs initialization when adding a module or changing its source, but ordinary edits to a referenced local module do not require reinstalling its source. For portability, prefer relative paths within a reviewed repository. Avoid paths that reach into unreviewed host directories.
Registry releases and version constraints
A public registry address conventionally contains namespace, name and target system/provider segments. Private registries can add a hostname. A registry module may support a version constraint; select a reviewed release explicitly rather than automatically consuming every update.
# Illustration only: fictional registry package, not available for installationmodule "catalog" { source = "example-org/catalog/aws" version = "1.2.3"}The exact version above is fictional. It would request release 1.2.3 if that package existed. A constraint such as ~> 1.2.0 permits compatible numeric versions from 1.2.0 up to, but not including, 1.3.0; it does not prove semantic compatibility. Read release notes and test changes. The provider dependency lock file records providers, not selected remote module versions, so record module selections separately.
Git sources and pinned revisions
A Git source may select a branch, tag or commit using the ref query parameter. A full reviewed commit ID is usually more reproducible than a moving branch or tag. In a repository package, the double slash introduces a subdirectory; put the query after that subdirectory.
# Illustration only: reserved hostname and fictional commitmodule "catalog" { source = "git::https://example.com/infra.git//modules/catalog?ref=0123456789abcdef0123456789abcdef01234567"}This is a fictional example, not a download instruction. A Git ref is not a registry version constraint; omit the version argument. A pinned commit does not establish safety by itself: inspect its contents and dependencies, confirm provenance, and avoid credentials in source URLs. Other remote source schemes exist and have their own rules.
Initialization and controlled upgrades
For this baseline, use literal strings in module source addresses and registry version constraints. Do not assume arbitrary runtime expressions, resource attributes or interpolation can select a source. Current documentation mentions some version-dependent constant-expression features; exact compatibility is not verified for the pinned patch, so this chapter avoids them. Adding or changing a source or registry version normally requires terraform init. In an approved environment, terraform init -upgrade can reconsider installed modules and providers within constraints, so inspect all resulting changes and regenerate plans. Do not execute it against this fictional example or an unknown project. Never treat a successful download or validation as a safety audit.
Fix a common mistake
The following example is intentionally invalid because a local directory is not a registry package.
# Invalid for a local path: version is a registry selection argumentmodule "catalog" { source = "./modules/label" version = "1.2.3" name = "preview-catalog"}Remove the version argument and manage the source with the enclosing repository revision, or choose a reviewed registry source with a real release. A valid module source also needs all required child inputs. Before introducing a dependency, review its transitive modules, provider requirements, provisioners, external data sources, license and resource side effects. Keep credentials and real state out of shared artifacts. Practice choosing a source for an internal module and a third-party module, and explain how you would verify updates.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Module sources and versions — knowledge check
Review module calls, addresses and source selection.
Which source supports the module version argument?
How can a Git module source be pinned?
Which versions satisfy ~> 1.2.0?
What should you do when a reviewed registry module version changes?
Does the dependency lockfile pin downloaded module versions?