Address-preserving refactor
moved {
from = terraform_data.old_name
to = terraform_data.service
}
resource "terraform_data" "service" { input = "api" }- Terraform can update state address ownership instead of replacing the underlying object.
Refactor Terraform addresses without unnecessary replacement, understand lifecycle controls, and bring existing infrastructure under management carefully.
Renaming a resource in configuration normally looks like destroy-and-create because the address changed. A moved block records the address transition so Terraform can preserve the existing managed object.
create_before_destroy, prevent_destroy, ignore_changes and replace_triggered_by affect lifecycle decisions. They should be used intentionally because they can change risk and drift behavior.
Import maps an existing provider object to a Terraform resource address. Configuration-driven import blocks make imports reviewable as part of plan/apply, but the provider resource must support import and configuration must match reality.
A removed block can remove a resource or module from Terraform state through the normal plan/apply workflow. Setting lifecycle { destroy = false } tells Terraform to stop managing the object without destroying the underlying infrastructure, which is safer and more reviewable than an ad-hoc state rm for planned ownership handoffs.
moved {
from = terraform_data.old_name
to = terraform_data.service
}
resource "terraform_data" "service" { input = "api" }import {
to = aws_s3_bucket.logs
id = "existing-logs-bucket"
}removed {
from = aws_instance.legacy
lifecycle {
destroy = false
}
}Goal: Rename a managed resource without creating or destroying it.
mkdir -p ~/gnu-terraform-labs/module08 && cd ~/gnu-terraform-labs/module08
cat > main.tf <<'EOF'
resource "terraform_data" "old_name" {
input = "api"
}
EOF
terraform init
terraform apply -auto-approvecat > main.tf <<'EOF'
moved {
from = terraform_data.old_name
to = terraform_data.service
}
resource "terraform_data" "service" {
input = "api"
}
EOFterraform planterraform apply -auto-approve
terraform state listAddress migrations should be reviewed like schema migrations because many downstream tools and state references depend on stable addresses.
Open each answer only after you have tried to answer the question yourself.
It records an address refactor so Terraform can preserve managed object identity.
No; import support is provider/resource specific.
It can intentionally stop Terraform from reconciling selected drift, masking changes you may care about.
Progress is saved in this browser immediately. When you sign in to My Learning, it can also sync across devices.
Attempt the core lab and knowledge checks before marking the module complete.