Skip to content

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?

  1. AIgnore it, because init recreates it on every machine from the version constraints alone
  2. BIgnore it, because it caches downloaded provider binaries that are specific to one platform
  3. CIgnore it, because HCP Terraform regenerates it at the start of every run
  4. 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.

Question 2 · choose 1

Where does a configuration declare that it needs the hashicorp/azurerm provider and which versions it accepts?

  1. AIn the provider "azurerm" block, using its source and version arguments
  2. BIn the .terraform.lock.hcl file, edited by hand before init
  3. CIn required_providers, inside the top-level terraform block
  4. DIn the required_version argument of the terraform block
Show the answer and why
  • AIn the provider "azurerm" block, using its source and version arguments

    Incorrect

    The provider block configures the provider; requirements belong in required_providers.

  • BIn the .terraform.lock.hcl file, edited by hand before init

    Incorrect

    Terraform writes the lock file; you declare requirements in configuration.

  • CIn required_providers, inside the top-level terraform block

    Correct

    Provider requirements, with source and version for each local name, are declared in required_providers inside the terraform block.

  • DIn the required_version argument of the terraform block

    Incorrect

    That argument is not where provider requirements are declared.

required_providers says which providers a module needs; provider blocks say how to configure them.

Question 3 · choose 1

A configuration requires source = "acme/inventory". Where does Terraform look for this provider when no special installation settings exist?

  1. AOn registry.terraform.io, in the acme namespace
  2. BOn GitHub, in a repository named acme/inventory
  3. CIn the hashicorp namespace, since acme is treated as a module name
  4. DIn HCP Terraform's private registry for the organization named acme
Show the answer and why
  • AOn registry.terraform.io, in the acme namespace

    Correct

    A source address is [HOSTNAME/]NAMESPACE/TYPE; when the hostname is omitted it defaults to registry.terraform.io.

  • BOn GitHub, in a repository named acme/inventory

    Incorrect

    Provider sources are registry addresses, not Git repositories.

  • CIn the hashicorp namespace, since acme is treated as a module name

    Incorrect

    The first part of a two-part address is the namespace.

  • DIn HCP Terraform's private registry for the organization named acme

    Incorrect

    A private registry needs its hostname in the address.

Hostname, namespace, type: the public registry is assumed when the hostname is left out.

Question 4 · choose 1

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?

  1. ABeta releases of providers can be installed only from HCP Terraform workspaces
  2. BThe lock file blocks every major version change permanently
  3. CThe constraint must use ~> 6.0 to allow a beta
  4. 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.

Question 5 · choose 1

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?

  1. AThe TF_DATA_DIR variable pointing all directories at one folder
  2. Bplugin_cache_dir in the CLI configuration file
  3. CA required_providers entry with source = "local"
  4. 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.

Question 6 · choose 1

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?

  1. AFill a directory with terraform providers mirror and use it as a mirror
  2. BCopy the .terraform.lock.hcl file into the network, which contains the provider packages
  3. CRun terraform init -input=false, which avoids network access
  4. 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.

Practise domain 2 →Practise all domains →