Skip to content

Renaming Terraform resources without destroying

Updated: at 10:20 AM

I use Terraform to provision LXCs/VMs on Proxmox cluster in my homelab. One day, I noticed that a typo in the resources name as following:

resource "proxmox_virtual_environment_container" "proxmox_lxc_contoiner" {
    for_each = local.lxc_containers
    // The rest of the code to define Proxmox LXC container
}

Notice the typo in the resource name proxmox_lxc_contoiner instead of the correct name proxmox_lxc_container

If I fix the typo in Terraform code and run terraform plan, it tell me that it will destroy existing LXC containers and create new ones, not ideal. Luckily, Terraform allows to move state as following (I use OpenTofu but the process should be the same for Terraform)

  1. List the existing state:

    tofu state list

    Here is the output in my case:

     proxmox_virtual_environment_container.proxmox_lxc_contoiner["postgresql"]
     proxmox_virtual_environment_container.proxmox_lxc_contoiner["zitadel"]
  2. Move the state to the new names:

    tofu state mv "proxmox_virtual_environment_container.proxmox_lxc_contoiner['postgresql']" "proxmox_virtual_environment_container.proxmox_lxc_container['postgresql']"

    Repeat this step for all the resources that you want to rename

  3. Update your terraform code with the new resource name

  4. Verify Terraform state with the new names

    tofu plan 

    Terraform should tell you that infrastructure is up-to-date.