TF-004 Interact with Terraform modules Practice Question
A team renamed a module from 'module.network' to 'module.vpc' in their configuration. They run 'terraform plan' and see that Terraform wants to destroy the old network resources and create new ones. They want to preserve the existing resources without downtime. What should they do?
⚠ Common exam trap
HashiCorp often tests the distinction between declarative (`moved` block) and imperative (`terraform state mv`) refactoring, trapping candidates who think manual state manipulation is the correct approach for configuration-driven changes.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Add a 'moved' block to the configuration to map the old module address to the new one.
The `moved` block in Terraform allows you to refactor module addresses without destroying and recreating resources. By adding a `moved` block that maps the old module address (`module.network`) to the new one (`module.vpc`), Terraform will automatically update the state to reflect the new address during the next plan and apply, preserving the existing infrastructure and avoiding downtime.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Add a 'moved' block to the configuration to map the old module address to the new one.
Why this is correct
Adding a 'moved' block to the configuration is the correct and declarative approach for handling resource address refactoring, such as renaming a module. This block explicitly informs Terraform that a resource or module previously known by one address should now be tracked under a new address. During the next 'terraform plan' and 'apply', Terraform will update the state file to reflect this change without destroying and recreating the underlying infrastructure, thus preserving existing resources and their attributes.
- ✗
Use 'terraform state mv' to move the resources to the new module address.
Why it's wrong here
While 'terraform state mv' can manually move resources within the state file, it is generally not considered the best practice for refactoring resource addresses in modern Terraform. This command is an imperative, out-of-band operation that directly manipulates the state, bypassing the configuration as the primary source of truth for such changes. It lacks the declarative nature and version control benefits of a 'moved' block, making the change less transparent and potentially more error-prone in collaborative environments.
- ✗
Update the module source to a new version.
Why it's wrong here
Updating the module source to a new version primarily changes the content or implementation of the module itself, not its logical address within the root configuration. This action would typically involve pointing to a different Git commit, registry version, or local path, potentially introducing new resources, modifying existing ones, or removing others based on the module's updated code. It does not address the specific problem of a module being renamed from 'network' to 'vpc' within the current configuration, which requires a state update to map the old address to the new one.
- ✗
Accept the destroy and recreate since it's the only way.
Why it's wrong here
Accepting a destroy and recreate cycle is an undesirable and often unnecessary outcome when refactoring resource addresses in Terraform. This approach would lead to significant downtime or service interruption for the affected infrastructure, as all resources associated with the old module address would be terminated and then provisioned anew. Modern Terraform provides robust, non-disruptive mechanisms like the 'moved' block specifically to handle such refactoring gracefully, preserving existing infrastructure and avoiding operational risks.
Go deeper
Related to this question
About these practice questions
One of 428 original TF-004 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This TF-004 practice question is part of Courseiva's free HashiCorp certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the TF-004 exam.