Are you trying to rename or move a Terraform resource and Terraform is now trying to re-create your resource rather than referencing the existing one?
Itâs a common issue, and the answer is quite straight-forward. Today weâll cover how you can move a Terraform resource instead of deleting it.
By the end of this article youâll understand how to rename, instead of deleting a resource and the reasons for how / why it works.
The Short Answer
If youâre in a hurry and just need the quick answer for how to rename a resource instead of deleting it, hereâs the steps:
- Define your new resource block.
- Use the Terraform move command.
Execute the Terraform move command by passing your old resource name first, followed by your new resource name.
Some important things to noteâŚ
- Do not manually alter your terraform state, only use the CLI commands or you could end up breaking your state and Terraform setup.
- Enable versioning on your remote state, or take a copy of local state before you make changes to make reverting easier if you make a mistake.
- Writing the new resource definition in Terraform is mandatory before you can move the state associated with it.
If you were after the short answer, thatâs it! Youâre good to go.
However, if youâre curious to learn about what the move command does and why you need it in the first place letâs dig into the detailsâŚ
Understanding Refactoring In Terraform
Refactoring Terraform is less like refactoring code, and more like refactoring a database schema. When you refactor a database schema you create a proposed new structure of the database and then you also write a migration script to explicitly say which data is going to move where.
To refactor Terraform we need the same process.
The reason for refactoring like this is because Terraformâs state acts like a database. There is a good explanation of why Terraform needs state on the Terraform website. But letâs recap it here quicklyâŚ
The primary reason Terraform uses state is to map your Terraform defined resources to real resources. When you move a resource, for instance a server into a module, youâre changing the stored name to the resource and you break the relationship between the scripted Terraform and the real world resource.
Letâs take moving a resource into a module, for instance.
Your stored resource name will now change from resource.resource_name
to become module.module_name.resource.resource_name.
Whilst the resource name itself hasnât changed, the full reference to the resource has changed. So youâll need to tell Terraform about where that resource has moved, using the terraform mv
command.
If youâre new to Infrastructure As Code, Iâd recommend you check out the article: Infrastructure As Code: An Ultimate Guide.
Refactor All The Terraform!
And thatâs all there is to it. Refactoring Terraform like this can be a bit of a pain and it can be fiddly. But it is possible to refactor Terraform without deleting your existing resourcesâgood news!
I hope that helped to clear things up and helps you refactor your existing Terraform. Now you should be able to continue with your refactoring, moving resources into or out of modules, or simply renaming resources with ease.
Speak soon Cloud Native friends!
The post Terraform: How To Rename (Instead Of Deleting) A Resource appeared first on The Dev Coach.
Lou is the editor of The Cloud Native Software Engineering Newsletter a Newsletter dedicated to making Cloud Software Engineering more accessible and easy to understand. Every month youâll get a digest of the best content for Cloud Native Software Engineers right in your inbox.
Top comments (1)
Thanks for the post, however you should change
terraform mv
withterraform state mv