Terraform Amazon Web Services DevOps

Renaming Terraform Resources without Destroying

March 22, 2023

Terraform is a great tool for managing infrastructure as code. It allows you to define your infrastructure in a declarative way and then apply it to your cloud provider. It also allows you to make changes to your infrastructure and apply those changes. However, there are some limitations to this. One of those limitations is that you cannot rename things in Terraform. If you want to rename something, you have to destroy it and then recreate it. This is not ideal because it can cause downtime and other issues.

In this article, we will look at how to rename resources and modules in Terraform without destroying or recreating them.

Renaming Resources

Let’s say you have a resource defined in your Terraform code like this:

resource "aws_instance" "csfyi_application" {
  ami           = "ami-123456"
  instance_type = "t2.micro"
}

Let’s say that you wanted to change it to follow some sort of convention that you have recently adopted i.e. you will prefix all resources with the nature of the resource. For example, you could change it to this:

resource "aws_instance" "web_csfyi" {
  ami           = "ami-123456"
  instance_type = "t2.micro"
}

This will rename the resource from csfyi_application to web_csfyi. However, if you run terraform plan you will see that this change will cause Terraform to destroy the csfyi_application resource and then recreate it. The reason for that is because Terraform tied up the infrastructure resource with the resource name csfyi_application in its statefile. Now that you have changed that name, Terraform thinks that the resource has been deleted.

To avoid this, you can use the terraform state mv command which updates the statefile to reflect the new name. For example, after making the change to the resource name, you could run the following command:

terraform state mv aws_instance.csfyi_application aws_instance.web_csfyi

This will rename the resource from csfyi_application to web_csfyi without destroying or recreating it.

Renaming Modules

Let’s say you have a module defined in your Terraform code like this:

module "example" {
  source = "./example"
}

If you want to rename this module, you can do so by changing the name of the module. For example, you could change it to this:

module "example2" {
  source = "./example"
}

This will rename the module from example to example2. However, again, this will cause Terraform to destroy the example module and then recreate the example2 module. To avoid this, you can use the terraform state mv command. For example, you could rename the module like this:

terraform state mv module.example module.example2

This will rename the module from example to example2 without destroying or recreating it. This is a much better approach because it will not cause downtime or any changes to the infrastructure.

Summary

In this article, we looked at how to rename resources and modules in Terraform without destroying or recreating them. We looked at how to rename resources and modules using the terraform state mv command. This is a much better approach than destroying and recreating resources because it will not cause downtime or any changes to the infrastructure.

© All rights reserved — cs.fyi