liveBook · Manning

Drift

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

Drift is a discrepancy between the infrastructure or files Terraform expects and the resources or files actually present.

Drift is a mismatch between what Terraform has recorded or configured and what is actually present. A common cause is an infrastructure change made outside Terraform, so the change never enters state. Drift can also appear when a Terraform-managed local file is missing on the next run. section 6.6 section 10.5.3

Terraform checks actual infrastructure during the refresh at the start of a plan. If it finds a difference, the resulting plan can propose changes that bring infrastructure back in line with configuration. To inspect changes without changing controlled infrastructure, use refresh-only planning mode; it refreshes state rather than following up with infrastructure changes. section 6.6 section 5.3.1

Before applying a plan, find out what changed and why. The right response depends on the cause:

  • Investigate unexpected changes through system behavior, logs, and recent team activity. Correct the underlying cause rather than repeatedly repairing its effects. section 6.6
  • Accept an expected change when appropriate. An automatic database minor-version upgrade, for example, may need only a refresh-only update to state. section 6.6.3
  • Choose whether to redeploy after a new machine or container image appears, or keep an existing resource unchanged. For attributes that another system controls, consider Terraform’s ignore_changes lifecycle rule. section 6.6.3
Listing 6.18 Ignoring changes
resource "aws_instance" "main" {
  tags = {                                  #1
    Name        = var.name                  #2
    Application = var.application           #3
  }
 
  lifecycle {
    ignore_changes = [tags]                 #4
  }
}

The aws_instance example places tags in ignore_changes. This prevents changes to that attribute from driving an update, which can help when an external system adds tags; it also means intended tag changes need consideration. section 6.6.3

Some recurring differences come from how values are generated or where Terraform runs. A timestamp function returns a new value each run; time-provider resources instead retain recorded times until their defined trigger or rotation. Random-provider resources similarly persist generated values until their keepers change. A local_file resource can repeatedly appear missing if runs occur in different locations and the file is not restored. section 6.8.2 section 6.8.1 section 10.5.3

Detecting drift on pull requests alone can miss changes made between them. GitOps-style continuous reconciliation calls for a larger delivery system to check regularly and, when appropriate, run Terraform to correct drift; Terraform by itself does not run continuously. section 8.3.2

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Explains refresh-based detection, cause investigation, and responses to expected changes; does not detail the four referenced error categories.
Examples
Uses externally added tags, autoscaling counts, database upgrades, regenerated values, and local files that disappear between runs.
What you can do after
Diagnose/debug with Terraform plans and refresh-only mode, then investigate the cause before choosing reconciliation, acceptance, or ignore_changes.
How it connects
Connects drift to Terraform state, refresh-only planning, lifecycle ignore_changes, and GitOps continuous reconciliation.

Also explained in

Compare how 12 books explain drift →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.