HashiCorp Terraform Associate (004) Question 137
Single answer5a Explain how Terraform sources modulesA platform team maintains a reusable Terraform module in a Git repository. The repository contains multiple modules under different subdirectories, and the networking module is stored in the path modules/network. The team wants application teams to consume a specific tagged version of only that networking module directly from Git so their deployments are reproducible. Which module source string should the application teams use in the module block?
- A
- B
- C
- D
Show answer and explanation
Correct answer: A
Explanation
Terraform can source modules from several locations, including the public or private Terraform Registry, local paths, and version control systems such as Git. When sourcing a module from Git, the correct pattern is to use a source string like git::REPO_URL//SUBDIR?ref=REVISION. The double slash identifies a subdirectory inside the repository, which is important when a repository contains multiple modules. The ref query parameter selects a branch, tag, or commit, allowing teams to pin an exact version for predictable runs. By contrast, version is used with registry modules, not Git URLs. This reflects Terraform best practices for reusable modules: pin module versions and use explicit source addresses so teams know exactly what code is being consumed. Refer to Terraform documentation on module sources and module syntax for Git repositories and subdirectory selection.
- A. Correct.
Correct. For Git-based module sources, Terraform supports the git:: prefix, an optional double-slash (//) to select a subdirectory within the repository, and the ref query parameter to pin a branch, tag, or commit. This source string correctly points to the Git repository, selects the modules/network subdirectory, and pins tag v1.4.0 for reproducible use.
- B. Incorrect.
Incorrect. This resembles a URL path with an @version suffix, but that is not the correct syntax for Terraform Git module sources. Terraform does not use @ to select Git tags in module source addresses. Instead, Git revisions are selected with ?ref=, and subdirectories are specified using //.
- C. Incorrect.
Incorrect. The registry:: prefix is not how Terraform references a Git repository for a module. Registry module sources use registry address syntax such as namespace/name/provider, optionally with a separate version argument in the module block when using a registry source. A raw Git repository should use the git:: source syntax.
- D. Incorrect.
Incorrect. Terraform does not use the version query parameter for Git module revisions. For Git sources, the correct parameter is ref. Also, the placement is wrong: the subdirectory selector //modules/network must appear before the query string.