Renaming a resource
Given...
resource "employee" "mike" {
name = "Michael"
}
We want to change the resource name from "mike" to "michael".
First, we update the resource name.
resource "employee" "michael" {
name = "Michael"
}
Next, we update the state.
terraform state mv employee.mike employee.michael
Renaming a module
Given...
module "the_employer" {
name = "Ferrari"
}
We want to change the module name from "the_employer" to "employer".
First, we update the module name.
module "employer" {
name = "Ferrari"
}
Next, we update the state.
terraform state mv module.the_employer module.employer
Moving multiple resources into a single resource
Given...
locals {
names = [
"Alain",
"Jim",
"Lewis"
]
}
resource "employee" "employee_1" {
name = local.names[0]
}
resource "employee" "employee_2" {
name = local.names[1]
}
resource "employee" "employee_3" {
name = local.names[2]
}
We want to manage these similar objects via a single block.
First, we create a new resource block that leverages the count
meta-argument.
locals {
names = [
"Alain",
"Jim",
"Lewis"
]
}
resource "employee" "employees" {
count = length(local.names)
name = local.names[count.index]
}
Next, we update the state.
terraform state mv employee.employee_1 employee.employees[0]
terraform state mv employee.employee_2 employee.employees[1]
terraform state mv employee.employee_3 employee.employees[2]