Link Search Menu Expand Document

Rename Resource

Procedure

  1. Rename block.
  2. Rename all references within the module.
  3. Move state from old name to new.
  4. Verify empty plan.

Automation

See tfrefactor.

tfrefactor rename my_resource.old my_resource.new

Source code

Before

resource "my_resource" "old" {
}

resource "something" "else" {
  attribute = my_resource.old
}

After

resource "my_resource" "new" {
}

resource "something" "else" {
  attribute = my_resource.new
}

moved {
  from my_resource.old
  to my_resource.new
}

State operations

If not using moved blocks introduced in Terraform 1.1, a state mv operation must be used to avoid destroying and recreating the resource.

terraform state mv my_resource.old my_resource.new

Or, with tfmigrate:

migration "state" "rename" {
  dir = "."
  actions = [
    "mv my-resource.old my-resource.new"
  ]
}

See also.

  • Rename resource array.
  • Move resource into module.
  • Move resource out of module.
  • Rename variable.
  • Rename local.
  • Rename data.