Drift, refresh-only, moved, and removed
Terraform 004 · Lesson 15 of 18 · 1:45:13
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the HashiCorp Terraform Associate (004) exam. In this lesson: drift between the state and real infrastructure, refresh-only plans, the moved block to rename or reorganize resources, and the removed block and the replace option. Includes three original practice questions, with Bluefin Logistics, a fictional parcel delivery company, as the example.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
State, code, and reality
Someone at Bluefin changed a firewall rule by hand in the console, and the next plan wanted to undo it. The same week, a renamed resource was almost destroyed and created again. This lesson covers drift, refresh-only plans, and the moved and removed blocks.
Drift
Drift happens when the real infrastructure changes outside Terraform, for example a rule edited by hand in the console. Every plan refreshes the state first, so it reports the objects that have changed outside of Terraform, and then proposes to bring them back to the configuration. If the manual change was a mistake, applying that plan fixes the drift. If the change was right, change the configuration to match, so that the next plan is empty.
Refresh-only
Sometimes you only want the state to match reality, without touching the infrastructure. Terraform plan with the refresh-only option shows only the changes made outside Terraform, and proposes no action on resources. Terraform apply with the same option writes those changes to the state, and changes nothing in the infrastructure. The old terraform refresh command does the same thing without showing you anything first, so it is deprecated. After a refresh-only apply, change the configuration to match, or the next normal plan will propose to undo the drift again.
The moved block
Renaming a resource in the configuration looks to Terraform like one resource removed and another added, so the plan would destroy the old object and create a new one. A moved block, since Terraform 1.1, tells Terraform that the object at the old address now belongs at the new address. It works for a rename, for moving a resource into a module, and for switching a resource to count or for each. The terraform state mv command has the same effect, but outside the plan, so the moved block is the safer choice, which reviewers can see.
Removed and replace
To stop managing a resource without destroying it, delete its block and add a removed block, available since Terraform 1.7, with destroy set to false in its lifecycle. The plan then shows that Terraform forgets the object, which keeps running, unmanaged. With destroy set to true, or without the lifecycle setting, a removed block destroys the object instead. The terraform state R M command also forgets an object, but outside the plan. And to force the replacement of a damaged object, run plan or apply with the replace option and its address, which takes over from the deprecated terraform taint command.
Recap
Let's recap. Drift is a change made outside Terraform, and every plan reports it, then proposes to undo it. The refresh-only option brings the state in line with reality without touching resources, and takes over from the deprecated refresh command. A moved block changes the address of an object without destroying it, for a rename, a module, or count and for each. And a removed block with destroy set to false forgets an object without destroying it, while the replace option forces a replacement.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
An engineer added a tag by hand to a server that Terraform manages, and the configuration does not have that tag. What does the next terraform plan propose?
- Nothing, since tags are ignored by default
- To write the tag into the configuration
- To destroy and recreate the whole server
- To remove the tag, to match the configuration
Show the answer
To remove the tag, to match the configuration
The plan sees the change made outside Terraform, and proposes to bring the server back to the configuration, here by removing the tag. “Nothing, since tags are ignored by default” is wrong, because Terraform ignores an attribute only when ignore changes lists it. “To write the tag into the configuration” is wrong, because Terraform never edits your configuration files. And “To destroy and recreate the whole server” is wrong, because a tag can change in place, without a replacement.
The team renames aws_s3_bucket.photos to aws_s3_bucket.parcel_photos, and adds a moved block from the old address to the new one. What does the next plan show?
- The bucket moves to its new address, unchanged
- Destroy the old bucket, create a new one
- An error: the old address no longer exists
- A new bucket, with the old one kept as well
Show the answer
The bucket moves to its new address, unchanged
The moved block tells Terraform that the existing bucket now belongs at the new address, so the plan shows a move, with no destroy and no create. “Destroy the old bucket, create a new one” is what happens without the moved block. “An error: the old address no longer exists” is wrong, because the old address is exactly what the from argument refers to. And “A new bucket, with the old one kept as well” is wrong, because Terraform keeps a single object, under its new address.
Bluefin hands its logging bucket over to another team, which will manage it in its own configuration. The bucket must keep running. What should Bluefin do?
- Delete the block, and run terraform apply
- Add a removed block with destroy set to false
- Apply with the replace option on the bucket
- Add prevent_destroy, then delete the block
Show the answer
Add a removed block with destroy set to false
A removed block with destroy set to false makes Terraform forget the bucket, which keeps running for the other team to import. “Delete the block, and run terraform apply” is wrong, because deleting the block alone plans the destruction of the bucket. “Apply with the replace option on the bucket” is wrong, because the replace option destroys the object and creates it again. And “Add prevent_destroy, then delete the block” is wrong, because prevent destroy disappears with the block, so it protects nothing.