All guides

Terraform import and moved blocks: refactor without destroying

By default, Terraform reads a changed resource address as an instruction to destroy the existing resource and create a new one. Import, moved and removed blocks say what happened, and the plan shows the result before anything changes.

Over the shoulder of someone working in a code editor on a laptop

Three blocks for three jobs

Terraform uses its state to map configuration to resources in the real world. Three blocks change that mapping:

  • import instructs Terraform to import existing infrastructure resources.
  • moved changes the address of a resource and keeps the object.
  • removed removes a resource from state, with or without destroying the real object.

Each one is configuration, and its effect appears in the plan before you apply.

Import an existing resource

HashiCorp's import tutorial dates configuration-driven import to Terraform 1.5. Write an import block and a matching resource block, then plan and apply.

HCL

import {
  to = aws_s3_bucket.logs
  id = "my-app-logs"
}

resource "aws_s3_bucket" "logs" {
  bucket = "my-app-logs"
}

The resource and the bucket name are the example's choice. to is the instance address to import the resource into, and id is the cloud provider's ID for the resource. identity is an alternative to id, and the two are mutually exclusive. provider selects an alternate provider configuration, and for_each imports several resources with one block. The v1.7 changelog added for_each, and the v1.16 changelog added support for import blocks inside modules.

The plan counts imports. This is the summary line from an import of one resource with nothing else to change, run on Terraform v1.9.8:

Text

Plan: 1 to import, 0 to add, 0 to change, 0 to destroy.

The tutorial recommends reviewing the plan output before applying, to avoid destructive changes.

Generate the configuration

When no resource block exists yet, Terraform can write one:

Shell

terraform plan -generate-config-out=generated.tf

Terraform generates HCL for imported resources that are not already present in the configuration. The path has to be a new file. In a test on v1.9.8, a second run against the same file ended with the error Target generated file already exists. The plan reference marks the option as experimental.

The generated file opens with a comment that asks you to review the resources and move them into your main configuration files. The tutorial says the generated configuration lists every possible argument of the imported resources, and it recommends cutting the file down to the arguments that are required. The documentation also warns that Terraform may fail to build a valid configuration for a resource whose schema is complex.

Rename and move with moved blocks

A moved block updates a resource address and does not destroy the resource. It requires Terraform v1.1 or later. This is the block from HashiCorp's tutorial on moving resources:

HCL

moved {
  from = aws_instance.example
  to   = module.ec2_instance.aws_instance.example
}

The plan reports the move on its own line:

Text

# aws_instance.example has moved to module.ec2_instance.aws_instance.example

The reference shows the same block for other refactors. It renames a resource, as in aws_instance.a to aws_instance.b. It renames a module call, as in module.a to module.b. It gives a resource an instance key when you add for_each, as in aws_instance.a to aws_instance.a["small"].

HashiCorp strongly recommends keeping moved blocks. Removing one is a breaking change: any configuration that still uses the old address will produce a plan that deletes the existing object. Its exception is a private module within an organization, once you are certain that all users have run terraform apply with the new module version.

Stop managing with removed blocks

To remove a resource from state, replace its resource block with a removed block. The v1.7 changelog introduced the block.

HCL

removed {
  from = aws_instance.example

  lifecycle {
    destroy = false
  }
}

With destroy = false, Terraform removes the resource from state and does not destroy the real object. In a test on v1.9.8, the plan said the resource "will no longer be managed by Terraform, but will not be destroyed".

The removed block reference states the default: Terraform removes the resource from state and destroys the actual resource. Write the lifecycle block out every time. Delete any references to the resource's attributes as well. Running terraform validate helps you find them.

What replaced the state commands

The CLI commands still have reference pages: terraform import ADDRESS ID, terraform state mv SOURCE DESTINATION and terraform state rm ADDRESS. Those pages now point at the blocks:

  • The page for terraform import says that in place of importing resources manually, you can add import blocks to your configuration.
  • The page for terraform state rm says removed blocks can remove more than one resource at a time, and that you can review removals as part of your normal plan and apply workflow.
  • The refactoring guide names terraform state mv for Terraform versions older than v1.1, where moved blocks are not available.

Read the plan before applying

  • For a move, look for the has moved to line.
  • For an import, look for the import count in the summary line.
  • For a removed block with destroy = false, look for the line that says the resource will not be destroyed.
  • If the plan lists a resource to destroy that you meant to keep, stop. In a test on v1.9.8, a resource renamed with no moved block was planned as one resource to add and one to destroy.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides