Detecting and Managing Drift
Infrastructure changes outside Terraform are called drift. This lesson explains how to detect drift, the choices for resolving it and how to replace a resource deliberately.
Learning outcomes
After completing this lesson, you will be able to:
- Explain what drift is and how refresh-only mode detects it.
- Choose how to resolve drift.
- Describe how to force the replacement of a resource.
What drift is
Drift is a difference between the real infrastructure and what Terraform recorded in state, caused by changes outside Terraform. Typical causes are an engineer changing a setting in the console during an incident, another tool or script modifying a resource, the platform changing a value automatically, such as an automatic minor version upgrade, and objects deleted by hand.
Every normal plan refreshes state first, so drift appears in the plan as changes that Terraform wants to make to bring the object back to the configuration. Terraform reports these as "Objects have changed outside of Terraform" before the list of planned actions. The question is then whether to keep the external change or undo it.
Refresh-only mode
terraform plan -refresh-only shows only the differences between state and real infrastructure, without proposing any changes to infrastructure. terraform apply -refresh-only shows the same differences and, after approval, writes them to state, so state matches reality. Root outputs that depend on the changed values are updated too.
$ terraform plan -refresh-only
Note: Objects have changed outside of Terraform
# aws_security_group.web has changed
~ resource "aws_security_group" "web" {
~ ingress = [
+ {
+ cidr_blocks = ["203.0.113.0/24"]
+ from_port = 22
+ to_port = 22
...Refresh-only mode replaces the deprecated terraform refresh command, which wrote the refreshed state immediately without showing the changes first. That was dangerous: if credentials pointed at the wrong account or region, refresh could find no objects and remove them all from state.
Refresh-only runs
Resolving drift
There are three ways to resolve drift, depending on whether the external change should stay.
| Goal | Action |
|---|---|
| Undo the external change | Run a normal terraform apply; Terraform changes the object back to the configuration |
| Keep the change permanently | Update the configuration to match, then plan until it shows no changes |
| Let another process own an attribute | Add the attribute to lifecycle { ignore_changes = [...] } |
Accepting the change into state with apply -refresh-only alone is not a full solution if the configuration still differs: the next normal plan would propose to revert it. Accept it into state and update the configuration, or revert it.
The following diagram compares the three ways to respond to a change made outside Terraform.

Responding to drift
Forcing replacement
Sometimes an object must be replaced even though neither the configuration nor the object's recorded attributes have changed, for example a virtual machine whose disk is corrupted or whose start-up script failed. terraform apply -replace="aws_instance.web" plans the replacement of that object, shows it for approval and performs it. It replaces the deprecated terraform taint command, which marked the object in state without a plan. terraform untaint still exists to clear a taint, for example one set automatically after a failed provisioner, if you are sure the object is healthy.
An emergency change
During an incident, an engineer opened SSH from the support network in a security group through the console. A week later the team decides the rule should stay. The next pipeline plan proposes to remove it.
The decisive choice is whether to keep the change.
Since the rule should stay, the team adds it to the security group's configuration in a pull request. The plan for that change shows no changes for the security group, because configuration and reality now agree. If the rule should not stay, simply applying the pipeline plan removes it. Running only apply -refresh-only would record the rule in state, but the next normal plan would still try to remove it.
Which command updates state to match real infrastructure without changing the infrastructure?
Show answer
terraform apply -refresh-only.
Exam Tips
Remember for the exam
- Drift = changes made outside Terraform; every plan detects it.
- plan -refresh-only shows drift; apply -refresh-only records it in state.
- Revert with apply, or keep by updating the configuration.
- -replace replaces taint; -refresh-only replaces terraform refresh.