A module is the smallest unit of reusable Terraform configuration. Every directory that contains .tf files is already a module in the formal sense, and the directory the operator runs terraform init against is the root module. The root module can call other modules through module blocks, and any module that is called by another module is a child module. The relationship is hierarchical: the root calls children, those children can call grandchildren, and so on. There is no flat namespace; a module is identified by the chain of module.<name> references that leads from the root to it.
The mechanism Terraform uses to find a child module is the source argument on the module block. The argument is mandatory, accepts a string literal, and is evaluated during terraform init. It is not a runtime value; you cannot use input variables, locals, or expressions inside source because module loading happens before variables and providers are wired. The string itself is a small URL-like grammar that tells the binary which fetcher to use, and the fetchers are built into Terraform Core rather than supplied by providers. That means a fresh init against a configuration that calls ten modules from ten different sources still works with no extra plugins installed.
The supported source types fall into a documented list. Local paths use a string that begins with ./ or ../; anything that starts with one of those two segments is treated as a relative path on disk and is never downloaded. The public Terraform Registry uses the three-part shorthand <NAMESPACE>/<NAME>/<PROVIDER>, where PROVIDER is the target platform the module wraps (for example, aws, google, azurerm, or kubernetes). Git repositories are referenced with a URL that starts with git::, optionally followed by https:// or ssh://, with a ?ref= query parameter that pins the checkout to a tag, branch, or commit SHA. GitHub repositories accept the shorter github.com/<owner>/<repo> form, which Terraform rewrites internally to the equivalent git::https://github.com/... URL. Bitbucket uses bitbucket.org/<owner>/<repo>. HTTP archives are URLs that point at a .tar.gz or .zip and may use HTTP, HTTPS, or signed query strings. Object stores are supported through s3:: and gcs:: prefixes that wrap the underlying signed URLs. Mercurial and generic HTTP X-Terraform-Get redirects round out the list.
The //subdir suffix lets a single repository hold many modules. If a module lives inside a sub-directory of the source, the path after the double slash tells Terraform to descend into that directory after the fetch. For git sources, this is paired with ?ref= for the checkout target, and the order matters: the sub-directory part comes before the query string. The HashiCorp documentation calls this the "double-slash separator" and explicitly warns that the path before // is the entire repository root, and the path after // is the module entry point. The same separator works for archives and object-store sources.
When terraform init runs, it walks every module block reachable from the root, evaluates the source for each, and downloads the module body into the local .terraform/modules/ directory. The downloaded layout is recorded in .terraform/modules/modules.json, which maps each module call address (such as module.network or module.network.module.subnet) to a relative on-disk path. Subsequent terraform validate, plan, and apply invocations read from that on-disk cache rather than re-fetching, which is why a Git outage during a normal plan does not break the run. The -upgrade flag on init forces Terraform to re-evaluate registry and git sources and pull the latest matching release, ignoring whatever the cache already contains.
Local-path modules are special: they are not copied into .terraform/modules/ at all. Instead, the modules.json file points directly at the source directory on disk, so an edit to the local module file is picked up by the next plan without any re-init. Remote modules behave the opposite way; you must re-run init (often with -upgrade) to pull a new version of a registry or git module. That asymmetry is the single most common source of confusion for engineers moving from local-path development to a registry-driven workflow.
Local path sources
A local-path source uses a relative directory reference. The path is evaluated relative to the calling module's directory, not the working directory of the operator. A root-module configuration that calls ./modules/network looks for a directory named modules/network next to its own .tf files. A child module that itself calls ./shared/iam is resolved relative to the child module's own location, which lets module trees compose without every level knowing the full filesystem layout.
# Root-module call: ./infra/main.tf
module "network" {
source = "./modules/network"
cidr_block = "10.0.0.0/16"
azs = ["a", "b", "c"]
}
infra/
main.tf
variables.tf
outputs.tf
modules/
network/
main.tf
variables.tf
outputs.tf
README.md
💡 Exam Trap: A relative source like
./modules/networkis resolved relative to the calling module, not relative to the directory you invoketerraformfrom. Two different root modules that both containmodule "x" { source = "./common" }look up./commonnext to their own files, not a shared./commonnext to the CLI cwd.
⚠️ Anti-Pattern: Storing local-path modules outside the repository root (such as
source = "../../shared/network") and then trying to consume the same root module from a different checkout. The..segments break the moment the directory tree changes; either move the shared module into the repository or publish it through a registry.
Public Terraform Registry sources
The public Terraform Registry hosts modules under a three-part address. The format is <NAMESPACE>/<NAME>/<PROVIDER>, where the namespace is the publishing organisation, the name is the module name, and the provider is the target platform the module configures. The provider part of the address has nothing to do with the required_providers block; it is a label the registry uses to group modules by their target. A module address like terraform-aws-modules/vpc/aws says "the module called vpc published by terraform-aws-modules, targeting the aws provider."
Registry sources are the only source type that accepts the version argument. The version is checked against the registry's release index, the matching tarball is downloaded, and the unpacked module body is cached in .terraform/modules/. Re-running init without -upgrade keeps whatever release already satisfies the constraint; running init -upgrade re-evaluates the constraint and pulls a newer matching release if one exists.
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 5.0"
name = "example-vpc"
cidr = "10.0.0.0/16"
}
📊 Limit: Public Terraform Registry module addresses are always three slash-separated parts. A four-part address such as
terraform-aws-modules/vpc/aws/networkis invalid as asource; sub-modules inside a registry module are reached through//<subdir>instead, for examplesource = "terraform-aws-modules/vpc/aws//modules/vpc-endpoints".
Git sources
A git source begins with the git:: prefix, which tells Terraform to use the built-in git fetcher rather than treat the URL as a generic HTTP archive. The body of the URL is a normal git URL (HTTPS or SSH), and the optional ?ref= query parameter pins the checkout to a tag, branch, or commit SHA. Without ref, Terraform clones the default branch, which is rarely what you want for production use because it converts the configuration into a moving target.
# HTTPS with a tag pin
module "policies" {
source = "git::https://example.com/infra/policies.git?ref=v2.4.0"
}
# SSH with a commit SHA pin and a sub-directory
module "iam" {
source = "git::ssh://git@example.com/infra/platform.git//modules/iam?ref=8a31f2c"
}
When both //subdir and ?ref= appear, the sub-directory comes first and the query string comes last. The ?depth=1 parameter is also supported and tells the git fetcher to perform a shallow clone, which is the standard recommendation for CI pipelines that pull modules on every run.
💡 Exam Trap: Git sources do NOT accept a
versionargument. The pinning happens inside thesourcestring through?ref=<tag-or-sha>. Addingversion = "1.2.0"next to a git source either errors out or is silently ignored depending on the release; either way the version is not honoured.
GitHub and Bitbucket shortcuts
A source string that begins with github.com/<owner>/<repo> is treated as syntactic sugar for git::https://github.com/<owner>/<repo>.git. The same shortcut exists for bitbucket.org. Both forms still accept the //subdir and ?ref= modifiers. There is no gitlab.com shortcut; GitLab repositories are consumed through the full git::https://gitlab.com/... form.
module "platform" {
source = "github.com/example-org/platform-modules//networking?ref=v1.8.2"
}
HTTP archives and object stores
A bare HTTPS URL that points at a .tar.gz, .tgz, or .zip is fetched, unpacked, and treated as a module. The s3:: and gcs:: prefixes wrap signed URLs into the same flow, so a module can be served from a private bucket without exposing it on the public internet. These sources are useful in environments where a private git host is not available but an object store with signed URLs is.
module "telemetry" {
source = "https://artifacts.example.com/modules/telemetry-1.4.0.tar.gz"
}
module "audit" {
source = "s3::https://s3.amazonaws.com/example-modules/audit-2.1.0.zip"
}
How init resolves and caches modules
🎯 Scenario: A platform team publishes a
networkmodule to a private git repository and pins consumers to?ref=v1.4.0. An engineer updates the module to add an output and tagsv1.4.1. The consuming root module still resolves?ref=v1.4.0afterterraform init; the new output is not visible until someone updates thesourcestring to?ref=v1.4.1and re-runsinit. The lesson: git pinning is part of thesourcestring, so a new release requires a configuration change in every consumer.
⚠️ Anti-Pattern: Mixing a registry address with a
?ref=query string, such assource = "terraform-aws-modules/vpc/aws?ref=v5.0.0". Registry sources are pinned through theversionargument; the?ref=modifier is meaningful only for git, and the registry fetcher does not parse it. The result is a parse-time error or a silently ignored ref.
💡 Exam Trap: The
//subdirseparator points at a sub-directory inside the module source, not at a nested module call. If you writesource = "github.com/example-org/platform//modules/iam", Terraform descends intomodules/iamafter the clone and treats that directory as the entry point. The other directories in the repository are not loaded, even if they contain.tffiles.
Decision Anchor: Choose a local-path source for in-repo modules that ship with the configuration they back, because edits are picked up without re-init. Choose a registry source for shared modules that follow a release cadence and need semver pinning, because only registry sources accept the version argument. Choose a git source for shared modules that live in a private repository without a registry, and always pin with ?ref=<tag-or-sha> to stop the configuration from drifting under you.
