002 · Domain 3: Infrastructure Lifecycle and Operations · 22% of the exam
Task 3.9: Manage provider and module versions
Keeping versions under control: constraint operators, where to pin and where to allow ranges, init -upgrade and the lock file, and registry module versions.
Study it
Provider and module versions: constraints, upgrades and the lock file
Lesson coming
Sample questions
Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.
Question 1 · choose 1
A root module requires hashicorp/aws with version = "~> 5.80.0", and the lock file records 5.80.3. Versions 5.80.7 and 5.81.0 have since been released. Which version does terraform init -upgrade select?
A5.80.7
B5.81.0
C5.80.3, the version already recorded in the lock file
DAn error about the constraint conflicting with the lock file
Show the answer and why
A5.80.7
Correct
~> allows only the right-most component to increment, so 5.80.x releases qualify, and -upgrade takes the newest one that matches.
B5.81.0
Incorrect
With three components, ~> 5.80.0 does not allow the minor version to increase.
C5.80.3, the version already recorded in the lock file
Incorrect
-upgrade disregards the recorded selection and chooses the newest version that matches the constraints.
DAn error about the constraint conflicting with the lock file
Incorrect
The recorded version also satisfies the constraint, so there is no conflict; -upgrade simply re-selects.
"~> 5.80.0" means at least 5.80.0 and below 5.81.0. init -upgrade ignores the lock file's selection and records the newest matching version.
A company runs several Terraform Enterprise instances, each with a private registry holding the same modules. It wants one set of configurations whose module sources work unchanged on every instance. What should each source use?
Aapp.terraform.io/<ORG>/<NAME>/<PROVIDER> on every instance
BA relative path to a vendored copy of each module in the repository
Clocalterraform.com/<ORG>/<NAME>/<PROVIDER> in every source
DA credentials block named after each instance's hostname
Show the answer and why
Aapp.terraform.io/<ORG>/<NAME>/<PROVIDER> on every instance
Incorrect
app.terraform.io is the HCP Terraform SaaS host, not a self-managed instance.
BA relative path to a vendored copy of each module in the repository
Incorrect
Local copies drop registry versioning, and the company wants to keep using its registries.
Clocalterraform.com/<ORG>/<NAME>/<PROVIDER> in every source
Correct
The generic hostname localterraform.com makes Terraform request the module from the instance it runs on.
DA credentials block named after each instance's hostname
Incorrect
Credentials authenticate to a host; they do not change which host a source address names.
CLI-driven workflows need Terraform v1.4 or later for the generic hostname, and testing on a workstation without the remote backend means swapping in a literal hostname. HashiCorp recommends it mainly for organizations with several Terraform Enterprise instances.
Module sources point at a private Git server over HTTPS, and init works on CI runners that hold Git credentials. After the runs move to HCP Terraform remote execution, every init fails to clone the modules. What does the module block reference say?
AHCP Terraform can clone private modules only from GitHub.com repositories
BHCP Terraform runs can authenticate to Git only with SSH keys
CRemote runs need ref to name a tag, not a branch, to clone
DGit module sources need depth=1 when they run remotely
Show the answer and why
AHCP Terraform can clone private modules only from GitHub.com repositories
Incorrect
Other Git hosts work; the limit is how HCP Terraform authenticates.
BHCP Terraform runs can authenticate to Git only with SSH keys
Correct
For Terraform operations in HCP Terraform you can only authenticate with SSH keys, so switch the sources to SSH and add a key.
CRemote runs need ref to name a tag, not a branch, to clone
Incorrect
ref can name a branch, a tag or a commit hash; the failure is authentication.
DGit module sources need depth=1 when they run remotely
Incorrect
depth makes a shallow clone; it does not supply credentials.
Runners clone with their local Git configuration and credentials, which remote runs in HCP Terraform do not have. An SSH source with a key added to HCP Terraform works in both places.