Import Blocks and `moved` Blocks
An import block is declarative import written in the config file. A moved block is a rename that keeps the live resource.
Import with a block
The import block names the resource and its live id. Apply performs the import.
import {
to = aws_s3_bucket.legacy
id = "my-old-bucket"
}
resource "aws_s3_bucket" "legacy" {
bucket = "my-old-bucket"
}Rename with a moved block
The moved block says the old name is now the new name. Terraform moves the state record.
moved {
from = aws_s3_bucket.legacy
to = aws_s3_bucket.adopted
}Plan shows the move
A rename is not a rebuild. Plan shows no new resource and no destroyed resource.
terraform planExample
# Start the emulator first: moto_server -p 5000
# Create the bucket outside Terraform first:
# aws --endpoint-url http://localhost:5000 s3api create-bucket --bucket my-old-bucket --region us-east-1
# main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
provider "aws" {
region = "us-east-1"
s3_use_path_style = true
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true
endpoints {
s3 = "http://localhost:5000"
sts = "http://localhost:5000"
}
}
import {
to = aws_s3_bucket.legacy
id = "my-old-bucket"
}
resource "aws_s3_bucket" "legacy" {
bucket = "my-old-bucket"
}
# Stage 2, as a separate step: rename the resource.
# Add this moved block, and replace the aws_s3_bucket.legacy block above with:
# moved {
# from = aws_s3_bucket.legacy
# to = aws_s3_bucket.adopted
# }
# resource "aws_s3_bucket" "adopted" {
# bucket = "my-old-bucket"
# }
# Then run terraform plan and terraform apply again.
# The moved block and the adopted block replace the legacy block;
# do not keep both resource blocks in one file.
# terraform init
# terraform plan
# terraform apply -auto-approve
# terraform destroy -auto-approveKey points
- The import command works, but it leaves no record in the config.
- An import block records the import in the config file itself.
- Import blocks arrived in Terraform 1.5.
- A moved block renames a resource without destroy and recreate.
- Plan shows the rename as a move, not a new resource.
- The bucket in this lesson lives in the emulator, never in real AWS.
The import block adopts the bucket when apply runs. The moved block is a second, separate step that renames it in state without touching the live bucket. Plan shows a move, not a destroy and a recreate.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
What does a moved block do?