Refactoring resources in configuration
In the previous recipes, we learned how to manage resources in Terraform state using the Terraform CLI.
In some situations, we may want to refactor the Terraform configuration by:
- Renaming resources or modules to be more consistent with the resource role
- Adding
count
orfor_each
iterators inside existing resources to scale the provisioning of the resource from 1 instance to N instances - Moving resources inside a module to refactor Terraform configuration
What you need to know is that any change in the Terraform configuration brings about changes in the Terraform state file.
In this recipe, we will learn how to perform a simple and basic refactoring operation so as not to destroy the resources that already exist in state, using two methods:
- Using the Terraform CLI
- Using the
moved
block
Let’s get started!
Getting ready
To complete this recipe, we will start with a simple...