HashiCorp Terraform Associate (004) Question 147
Single answer5c Use modules in configurationYour team maintains a reusable Terraform module named "network" in a private Git repository. A root configuration uses this module to create VPCs for multiple environments. After a teammate updates the module source code in the Git repo to add new subnet tags, you run terraform plan in the root configuration and notice the plan does not reflect the module changes. You want Terraform to download the updated module code without changing the module block itself. What should you do?
- A
Run
terraform get -updatein the working directory to refresh downloaded modules - B
Run
terraform init -upgradein the working directory to reinitialize and upgrade modules - C
Run
terraform refreshso Terraform reloads the module source before planning - D
Delete the state file so Terraform is forced to fetch the latest module source
Show answer and explanation
Correct answer: B
Explanation
Terraform modules are installed into the working directory during terraform init. When a module source is updated externally, Terraform does not automatically redownload it during terraform plan. To tell Terraform to revisit installed dependencies and retrieve newer acceptable versions or updated source content, use terraform init -upgrade. This is especially relevant when consuming modules from registries or version-controlled sources such as Git. In current Terraform usage, terraform init is the standard command for initializing and updating modules, while terraform refresh only reconciles state with real infrastructure and does not affect downloaded module code. Best practice is to manage module versions explicitly, often with version constraints for registry modules or stable Git references such as tags, so updates are intentional and repeatable.
- A. Incorrect.
Incorrect.
terraform getwas historically used to download and update modules, but modern Terraform workflows rely onterraform initfor module installation and upgrades. On the Terraform Associate 004 exam, the expected current behavior is to useterraform init, notterraform get, to install or update modules in a working directory. - B. Correct.
Correct.
terraform init -upgradetells Terraform to reinitialize the working directory and check for newer versions of installed providers and modules that match the configuration. This is the appropriate way to have Terraform redownload updated module source code when the module source has changed upstream but the module block in the root configuration has not changed. - C. Incorrect.
Incorrect.
terraform refreshupdates state with information from real infrastructure. It does not reinstall or upgrade module source code from a registry, Git repository, or other module source location. A common misconception is to treat refresh as a general sync command, but it is for state and resource data, not module installation. - D. Incorrect.
Incorrect. Deleting the state file is unnecessary and dangerous. Module source code is stored in the working directory's
.terraformdata, not in the state file. Removing state can orphan managed infrastructure and create drift or recreation risks. This option reflects a common but harmful troubleshooting mistake.