Refactor Terraform Modules with Moved Blocks
Refactoring modules is essential for maintaining clean, manageable infrastructure as code. When you need to rename or move resources, the challenge lies in ensuring that existing resources remain intact. Moved blocks in Terraform provide a solution by allowing you to update resource addresses without destruction, which is critical for production environments where uptime and data integrity matter.
To use moved blocks, you need Terraform v1.1 or later. You add a moved block to your configuration, specifying the 'from' and 'to' addresses. Terraform checks the state for an existing resource at the 'from' address before creating a new plan. This means that the resource remains intact during the transition. For example, you can write a moved block like this: moved{from=aws_instance.a=aws_instance.b}. This tells Terraform to treat the resource at the old address as if it were at the new address in future plans, ensuring a seamless transition.
In production, it's vital to remember that removing a moved block is a breaking change. This can lead to unexpected behavior if not managed carefully. Additionally, you cannot use moved blocks to convert a managed resource into a data resource. Understanding these limitations will help you avoid pitfalls when refactoring your modules.
Key takeaways
- →Use moved blocks to update resource addresses without destroying existing resources.
- →Specify 'from' and 'to' addresses in moved blocks to ensure seamless transitions.
- →Remember that removing a moved block is a breaking change and can disrupt your infrastructure.
Why it matters
In production, maintaining resource integrity during refactoring is crucial. Moved blocks help prevent downtime and data loss, which can be costly.
Code examples
moved{from=aws_instance.a=aws_instance.b}module"a"{source="../modules/example"count=3# (module arguments)}moved{from=module.a=module.a[2]}resource"aws_instance""a"{count=2# (resource-type-specific configuration)}moved{from=aws_instance.a=aws_instance.a["small"]}When NOT to use this
The official docs don't call out specific anti-patterns here. Use your judgment based on your scale and requirements.
Want the complete reference?
Read official docsHigh-performance cloud infrastructure — deploy in 60 seconds. New accounts get $100 free credit to try Kubernetes, VMs, and managed databases.
Get $100 free credit →Mastering Dynamic Blocks in Terraform Modules
Dynamic blocks are a powerful feature in Terraform that can simplify your module configurations. They allow you to generate nested blocks based on complex values, making your code more flexible and reusable. Dive in to learn how to leverage this capability effectively.
Mastering Terraform's Built-in Functions for Effective Module Management
Built-in functions in Terraform are essential for transforming and combining values within your configurations. Understanding how to leverage these functions can significantly streamline your module management. For instance, using `max(5,12,9)` allows you to easily determine the highest value among your inputs.
Mastering Module Publishing in Terraform: Best Practices and Pitfalls
Publishing modules in Terraform is crucial for sharing reusable infrastructure code. Adhering to the three-part naming convention is just the start. Understanding how the registry processes your modules can save you headaches down the line.
Get the daily digest
One email. 5 articles. Every morning.
No spam. Unsubscribe anytime.