004 · Domain 2: Terraform fundamentals · 13% of the exam
Task 2.1: Install and version Terraform providers
Getting the right plugins: required_providers with source addresses and version constraints, what terraform init installs, the dependency lock file and its checksums, and upgrading on purpose with -upgrade.
Study it
Installing providers: required_providers, version constraints 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
After terraform init, a new file named .terraform.lock.hcl appears next to the configuration. A teammate wants to add it to .gitignore along with the .terraform directory. What should the team do with it?
AIgnore it, because init recreates it on every machine from the version constraints alone
BIgnore it, because it caches downloaded provider binaries that are specific to one platform
CIgnore it, because HCP Terraform regenerates it at the start of every run
DCommit it, so every run selects the same provider versions and checks the same checksums
Show the answer and why
AIgnore it, because init recreates it on every machine from the version constraints alone
Incorrect
Without the committed file, each init could pick a newer matching version, which is exactly what the lock file prevents.
BIgnore it, because it caches downloaded provider binaries that are specific to one platform
Incorrect
Provider binaries live in .terraform/providers. The lock file holds version selections and checksums, not binaries.
CIgnore it, because HCP Terraform regenerates it at the start of every run
Incorrect
HCP Terraform installs providers using the versions in the lock file when one exists; it does not regenerate it.
DCommit it, so every run selects the same provider versions and checks the same checksums
Correct
The lock file records the provider versions Terraform selected and their checksums; committing it makes later init runs reuse those exact selections.
.terraform.lock.hcl belongs in version control with the configuration; the .terraform directory does not.
A team wants to try a provider's 6.0.0-beta1 release in a sandbox. Its constraint is version = ">= 5.80", and init keeps installing a 5.x release. Why?
ABeta releases of providers can be installed only from HCP Terraform workspaces
BThe lock file blocks every major version change permanently
CThe constraint must use ~> 6.0 to allow a beta
DA pre-release is selected only by an exact version, such as = 6.0.0-beta1
Show the answer and why
ABeta releases of providers can be installed only from HCP Terraform workspaces
Incorrect
Pre-releases come from the same registry; the constraint is what excludes them.
BThe lock file blocks every major version change permanently
Incorrect
The lock file records selections; it is not why a beta is skipped.
CThe constraint must use ~> 6.0 to allow a beta
Incorrect
~> does not match pre-releases either.
DA pre-release is selected only by an exact version, such as = 6.0.0-beta1
Correct
Terraform does not match pre-release versions with >, >=, <, <= or ~>; set the exact pre-release version to select it.
Pre-releases are opt-in and exact; range operators never pick them.
A developer has thirty working directories that all use the same large provider, and each terraform init downloads it again. Which setting lets them share one download?
AThe TF_DATA_DIR variable pointing all directories at one folder
Bplugin_cache_dir in the CLI configuration file
CA required_providers entry with source = "local"
DThe -get=false option on init
Show the answer and why
AThe TF_DATA_DIR variable pointing all directories at one folder
Incorrect
TF_DATA_DIR moves the .terraform directory of a working directory; sharing it would mix their modules and backend settings.
Bplugin_cache_dir in the CLI configuration file
Correct
The plugin cache directory lets Terraform reuse provider packages across working directories instead of downloading them each time.
CA required_providers entry with source = "local"
Incorrect
There is no local source; installation behavior is set in the CLI configuration.
DThe -get=false option on init
Incorrect
-get=false skips child module installation; providers are still downloaded.
The CLI configuration controls how providers are installed and cached for every working directory on a machine.
Build agents in an isolated network cannot reach registry.terraform.io. The team can copy files into that network. How can init still install the required providers?
AFill a directory with terraform providers mirror and use it as a mirror
BCopy the .terraform.lock.hcl file into the network, which contains the provider packages
CRun terraform init -input=false, which avoids network access
DSet the provider source to the local file path of each binary
Show the answer and why
AFill a directory with terraform providers mirror and use it as a mirror
Correct
providers mirror downloads the providers a configuration needs into a directory, and the CLI configuration's provider_installation can use it as a filesystem mirror.
BCopy the .terraform.lock.hcl file into the network, which contains the provider packages
Incorrect
The lock file has versions and checksums only, not packages.
CRun terraform init -input=false, which avoids network access
Incorrect
-input=false has nothing to do with where providers are installed from.
DSet the provider source to the local file path of each binary
Incorrect
Source addresses are registry-style addresses, not file paths.
Mirrors are the supported way to install providers without the public registry.