HashiCorp Certified: Terraform Associate
Module sources, the registry and versions for Terraform Associate
A module block's source tells Terraform where to get the module: a local path starting with ./ or ../, a registry address such as terraform-aws-modules/vpc/aws, a Git repository, or another remote source. The version argument works only for registry modules. For a Git source you pin a release with ?ref= in the source, and the dependency lock file does not record modules.
Source addresses
Local path: source = "./modules/network". Local paths start with ./ or ../. Absolute paths also work but are copied into the module cache, and HashiCorp does not recommend them because they tie the configuration to one computer's layout.
Public Terraform Registry: <NAMESPACE>/<NAME>/<PROVIDER>. In terraform-aws-modules/vpc/aws, the last segment, aws, is the provider the module is written for. HCP Terraform's private registry uses app.terraform.io/<NAMESPACE>/<NAME>/<PROVIDER>, and Terraform Enterprise uses its own hostname in front.
Git: for example git::https://example.com/vpc.git or git::ssh://.... A module in a subdirectory of a repository or archive uses //, as in git::https://example.com/network.git//modules/vpc. Query parameters such as ref go after the subdirectory.
Choosing a version
Registry modules take a version argument with a version constraint. Terraform uses the newest installed version that meets it, and downloads the newest matching version if none is installed. HashiCorp recommends constraining versions explicitly.
version does not apply to other sources. Local modules share their caller's version. A Git source uses the default branch unless you add ?ref= with a tag, branch or commit SHA, for example git::https://example.com/vpc.git?ref=v1.2.0. Adding depth=1 makes a shallow clone.
An input variable used in source or version must set const = true, because Terraform needs the value during init, before it evaluates most variables.
Installing and using a module
terraform init downloads module sources into a hidden subdirectory of the working directory. Run init again after changing source or version. Add -upgrade to move installed modules to the newest version the constraint allows.
Inputs are arguments in the module block. Outputs are read as module.<NAME>.<OUTPUT>, so a value the module does not output cannot be reached. A module block also accepts count, for_each and depends_on, and can be given a non-default provider configuration.
The lock file and modules
The dependency lock file, .terraform.lock.hcl, currently tracks only provider dependencies. Terraform does not remember module version selections, so it always selects the newest module version that meets the constraint.
To make every machine install the same registry module version, use an exact version constraint. For a Git module, pin ?ref= to a tag or commit.
Where the exam catches people
- Believing
versionworks on a Git source: it applies only to registry modules. Pin Git modules with?ref=. - Believing
.terraform.lock.hclpins module versions: it records providers only. - Believing the last part of
terraform-aws-modules/vpc/awsis a version or name: it is the provider the module targets. - Believing
source = "modules/network"is a local path: local paths begin with./or../. - Believing a changed module
versiontakes effect on the next plan: runterraform initagain first.