My Digital Garden

Move resource to another Terraform plan

Move resource to another Terraform plan

As projects mature and evolve it may become necessary to migrate a resource under Terraform-control from one plan (Plan A) to Terraform control of another plan (Plan B), without deleting the physical resource. (If deletion is acceptable you can just remoe the resource from Plan A, re-apply, then add to Plan B and apply).

Overview of approach

  • remove resource from the state associated with Plan A
  • remove resource from Plan A
  • check apply Plan A - should see no changes needed
  • add resource to Plan B
  • import resource to state associaed with Plan B
  • check apply Plan B - should see no changes needed

Example with remote state in Terraform Cloud

For this example we assume:

  • remote state in Terraform Cloud
  • all resources in Azure (this only impacts the import of the resource into Plan B)

Removing from Plan A

In the local working directory for Plan A:

  • terraform init
  • terraform state list
    • identify the resource you are interested in
  • terraform state rm <resourcename>
  • edit the plan files to remove the resource
  • run the plan (e.g. by pushing change and running in Terraform Cloud)
    • confirm no infrastructure changes required

Adding to Plan B

In the local working directory for Plan B

See also

internal and external references