Reading a plan like a reviewer: drift, import and state refactoring
A worked example: read a plan by its symbols and summary line, find drift with a refresh-only plan and decide whether to revert or adopt it, and use import, moved and removed blocks to change state without destroying real resources.
Contentsand lesson path
On this page
- Step 1: Read the symbols, not the pipeline status
- Step 2: Find drift and decide what to do with it
- Step 3: Import, rename and let go without destroying
- Check your understanding
- Apply it
- Review prompt
Lessons of IaC Fundamentals
- Why infrastructure belongs in code: provisioning, configuration and images Concept · 20 min
- Lab: plan, apply and destroy, then extract a reusable module Lab · 40 min
- Remote state, environments and the checks every plan should pass Concept · 25 min
- Reading a plan like a reviewer: drift, import and state refactoring Worked example · 25 min
- Lab: configure a small fleet idempotently with an Ansible playbook Lab · 30 min
- Check: plan review, drift and configuration scenarios Knowledge check · 10 questions
What you will learn
- Keep state in a locked remote backend, separate environments, and review a plan to catch destructive or unintended changes before applying.
- Detect and reconcile drift, and bring an existing resource under management with import.
01. Step 1: Read the symbols, not the pipeline status
A pull request that changes infrastructure usually gets a comment from CI with the output of tofu plan. A green check only says that the code parsed and the provider could build a plan. It says nothing about whether the plan does what the pull request claims. Approving without reading the plan means approving changes you have not seen.
Each resource in a plan starts with a symbol. Read them in order of risk:
~update in place. The object keeps its ID and OpenTofu changes some of its attributes through the API. Check whether that change interrupts the service: a new instance class for an RDS database, for example, restarts it.-/+replace. OpenTofu destroys the object and creates a new one, and marks the attribute that causes it with# forces replacement. For a stateless instance behind a load balancer that may be fine. For a database, a disk, a DNS zone or a static IP address, the new object is empty or has a new address, so data or addresses are lost unless a migration was planned.-destroy. Check that the object is really retired and that nothing still uses it.
Then read the summary line, Plan: X to add, Y to change, Z to destroy., and compare it with what the pull request says it does. A change that should only rename a tag must not show 1 to destroy.
02. Step 2: Find drift and decide what to do with it
Drift is a difference between what the code declares, what the state recorded and what really exists. It appears when someone changes a resource outside OpenTofu, for example by resizing a virtual machine in the cloud console during an incident.
Every tofu plan first refreshes: it reads each object from the provider API into an in-memory copy of the state. The plan does not write state. If a live object differs from the recorded one, the plan first prints Objects have changed outside of OpenTofu, then proposes the changes that would make reality match the code again.
A refresh-only plan (tofu plan -refresh-only) stops after that first part. It shows how the state would change to match the real objects, proposes no change to them and writes nothing. To accept the real values into state, you would run tofu apply -refresh-only.
Try it in the working folder of the lesson 2 lab. Overwrite the staging file by hand, then plan twice. The hashicorp/local provider has no in-place update: when a file no longer matches the content recorded in state, the provider reports it as deleted, so the normal plan recreates it (1 to add). A cloud resource usually shows has changed and a ~ update instead.
tofu apply -auto-approve > /dev/null # recreates the files if you destroyed them
echo tampered > out/staging/service.json
tofu plan -refresh-only
tofu plan…
Note: Objects have changed outside of OpenTofu
OpenTofu detected the following changes made outside of OpenTofu since the
last "tofu apply" which may have affected this plan:
# module.web_staging.local_file.config has been deleted
- resource "local_file" "config" {
…
This is a refresh-only plan, so OpenTofu will not take any actions to undo
these. If you were expecting these changes then you can apply this plan to
record the updated values in the OpenTofu state without changing any remote
objects.
…
OpenTofu will perform the following actions:
# module.web_staging.local_file.config will be created
+ resource "local_file" "config" {
…
Plan: 1 to add, 0 to change, 0 to destroy.There are two ways to resolve drift:
- Revert. If the change was a mistake or was never approved, apply the normal plan. OpenTofu puts back what the code declares; here,
tofu applyrewritesservice.jsonwith the staging settings. - Adopt. If the change should stay, such as a larger instance that ended an outage, change the code to declare it and merge that change. The next plan then reports
No changes. Your infrastructure matches the configuration.
To find drift before the next pull request does, run a scheduled job with -detailed-exitcode. The plan then exits with 0 when there is nothing to do, 1 on an error and 2 when the plan is not empty. A normal plan that exits with 2 can also mean code that was merged but not yet applied. To look for drift alone, run tofu plan -refresh-only -detailed-exitcode on the commit that is deployed: in the lab above it exits with 2 after the file was overwritten and with 0 after the revert. Alert a person when it returns 2.
03. Step 3: Import, rename and let go without destroying
Three kinds of block change the state without touching the real object. You write them in the code, so they go through review like any other change:
importadopts an object that was created by hand. Write the resource block that should describe it, add animportblock with the resource address (to) and the provider's ID for the object (id), and runplan. The plan showswill be importedand changes nothing until you apply. Import blocks exist in Terraform 1.5 and later and in every OpenTofu release (1.6 and later). If you do not know the settings of the object,tofu plan -generate-config-out=generated.tfwrites a first draft of the resource block for you. That feature is still marked experimental, so read and tidy the file before you commit it.movedrecords a new address for a resource, for example whenaws_instance.webmoves into a module asmodule.web_cluster.aws_instance.server. State tracks objects by address, so without the block the plan destroys the object at the old address and creates a new one at the new address. With it, the plan showshas moved toand the object stays. Moved blocks need Terraform 1.1 or later, or any OpenTofu release.removedstops managing a resource but keeps the object, for example a log bucket that another team now owns. Delete the resource block and add aremovedblock withdestroy = falsein itslifecycleblock. That form needs Terraform 1.7 or later, or OpenTofu 1.10 or later. OpenTofu 1.7 to 1.9 stop withBlocks of type "lifecycle" are not expected here; on those versions write theremovedblock with onlyfrom, which already means "keep the object". The plan then says the objectwill be removed from the OpenTofu state but will not be destroyed. Before these blocks existed, the way to do this wastofu state rm, a command that edits state directly and leaves nothing to review.
# Adopt a bucket that was created by hand (1.5+)
import {
to = aws_s3_bucket.legacy_data
id = "corp-legacy-data-2022"
}
# Record a rename into a module, so nothing is destroyed (1.1+)
moved {
from = aws_instance.web
to = module.web_cluster.aws_instance.server
}
# Stop managing the logs bucket but keep it in the cloud
# (Terraform 1.7+, OpenTofu 1.10+; OpenTofu 1.7-1.9: drop lifecycle)
removed {
from = aws_s3_bucket.logs
lifecycle {
destroy = false
}
}The file works only together with the rest of the configuration: a resource block for aws_s3_bucket.legacy_data, a web_cluster module that declares aws_instance.server, and no block left for aws_s3_bucket.logs. A plan of that configuration reports each block on its own line, and its summary line is the one to check.
OpenTofu will perform the following actions:
# aws_s3_bucket.legacy_data will be imported
# (imported from "corp-legacy-data-2022")
resource "aws_s3_bucket" "legacy_data" {
…
# aws_s3_bucket.logs will be removed from the OpenTofu state but will not be destroyed
. resource "aws_s3_bucket" "logs" {
…
# aws_instance.web has moved to module.web_cluster.aws_instance.server
resource "aws_instance" "server" {
…
Plan: 1 to import, 0 to add, 0 to change, 0 to destroy, 1 to forget.Forget is the word the plan uses for a removed block with destroy = false: the object leaves the state and stays in the cloud. OpenTofu 1.10 and later also count forgets in the summary line; older releases show only the . forget entry. A refactoring pull request should show only imports, moves and forgets, with 0 to add, 0 to change, 0 to destroy. If the summary shows a destroy, a moved block is missing or names the wrong address. Fix the block before you merge, not after the apply.
04. Check your understanding
05. Apply it
Check one of your own repositories against these points:
- Production databases, disks and buckets have
prevent_destroyand the provider's own deletion protection, because the guard leaves with a deleted block. - Nobody approves a plan that replaces a stateful resource unless a data migration is planned for it.
- A scheduled job runs
tofu plan -refresh-only -detailed-exitcodeon the deployed commit and alerts on exit code 2. - After an emergency change in the console, someone opens a pull request that declares it, and the next plan reports no changes.
- Resources created by hand come in through
importblocks, renames usemovedblocks, and resources handed to another team leave throughremovedblocks.
06. Review prompt
Without looking back, describe how a reviewer reads a plan: which symbols carry the most risk, and what the summary line should match. Explain how a plan detects drift, what a refresh-only plan changes, and the two ways to resolve drift. Then say what an import, a moved and a removed block each do to state, and what the plan would do to the real object without them.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
10 questions · about 8 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.