Terraform Associate 004 ยท Providers and state fundamentals
How are provider versions installed and kept consistent?
required_providers block, version constraint operators, terraform init download, .terraform.lock.hcl and init -upgrade (2a).
Key points
- required_providers (terraform block): source and version constraint (>=, ~>, =) per provider.
- terraform init: downloads a match, records the exact version and package checksums in .terraform.lock.hcl.
- Commit that file so everyone gets the same versions.
- Later inits: reuse the locked version.
- terraform init -upgrade: ignores the lock, picks the newest allowed version, rewrites the lock file.
Source: https://developer.hashicorp.com/terraform/language/files/dependency-lock
Practice questions
Which file records the exact provider versions and checksums selected by terraform init?
- terraform.tfstate
- .terraform.lock.hcl
- terraform.tfvars
- providers.auto.tf
Show answer
Answer: B. .terraform.lock.hcl
.terraform.lock.hcl is the dependency lock file. terraform.tfstate holds resource data, and the .terraform directory is a local cache of downloaded plugins and modules, not the record of selections.
Which version constraints would accept provider version 3.2.5?
- ~> 3.2
- = 3.2.4
- ~> 3.2.1
- >= 3.3
- ~> 4.0
Show answer
Answer: A. ~> 3.2, C. ~> 3.2.1
~> 3.2 allows 3.x at or above 3.2, ~> 3.2.1 allows 3.2.x at or above 3.2.1, so both accept 3.2.5. >= 3.3 is too low a floor, and = 3.2.4 is an exact pin.
True or false: .terraform.lock.hcl should be added to .gitignore because terraform init regenerates it.
Show answer
Answer: False
False. The lock file should be committed so every teammate and pipeline installs the same provider versions and changes to them are reviewed like code.
Practice 8 questions on this topic in Bitzu
Short lessons, instant explanations and spaced review of what you get wrong. Free on iOS and Android.
