All guides

Terraform drift: find the changes made outside code

Change a resource that Terraform controls by hand, and the state file is out of sync with the real infrastructure. HashiCorp's documentation calls that drift. A scheduled plan finds it, and you then adopt the change or revert it.

A laptop screen showing JavaScript in a code editor

What drift is

Terraform records every resource it manages in the state file. HashiCorp's tutorial on drift says to make no manual changes to a resource under Terraform's control: the state file then no longer matches the real infrastructure, which is what "drift" means.

Two plans that show it

By default, terraform plan first reads the current state of the remote objects that already exist, to make sure the Terraform state is up to date. The tutorial describes what follows a manual change: a plan or an apply run with no -refresh-only flag would attempt to revert it.

Refresh-only mode looks at the outside change alone:

Shell

terraform plan -refresh-only

This plan has a single goal: bring the Terraform state and the output values of the root module in line with changes that were made to remote objects outside Terraform. Its output lists them under the note Objects have changed outside of Terraform and adds that Terraform will not take any actions to undo them.

Two options reduce the refresh. -refresh=false disables the synchronization of the state with remote objects, so Terraform ignores external changes. In a test on Terraform v1.9.8, a plan with -refresh=false reported No changes for a managed file that had been edited by hand. -minimal-refresh, a new option in the v1.17 changelog, refreshes only the resource instances that Terraform plans to change. Leave both out of a plan that is meant to find drift.

Exit codes for automation

With -detailed-exitcode, the exit code of terraform plan says what the plan contains:

  • 0: succeeded with an empty diff, so there are no changes
  • 1: error
  • 2: succeeded with a non-empty diff, so changes are present

In the same test on v1.9.8, the plan returned 0 right after an apply. It returned 2 after the managed file was edited by hand.

A scheduled check in CI

This GitHub Actions workflow runs that plan on a schedule. The cron expression, the role, the region and the lock timeout are the example's choice.

YAML

name: drift
on:
  schedule:
    - cron: "17 5 * * 1-5"

permissions:
  id-token: write
  contents: read

jobs:
  plan:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: aws-actions/configure-aws-credentials@v6
        with:
          role-to-assume: arn:aws:iam::111122223333:role/terraform-plan
          aws-region: us-east-1
      - uses: hashicorp/setup-terraform@v4
        with:
          terraform_wrapper: false
      - run: terraform init -input=false
      - run: terraform plan -input=false -lock-timeout=5m -detailed-exitcode

A scheduled workflow uses the newest commit of the default branch, and its times are UTC by default. GitHub says high load can delay the schedule event, and it counts the start of every hour among the high-load periods. The example runs at minute 17 for that reason.

GitHub's workflow syntax page says the runner reports the status of a step as fail or succeed based on the exit code of its script. Its page on exit codes says any exit code other than 0 indicates failure. Exit codes 1 and 2 from the plan both fall under that.

terraform_wrapper: false leaves out the wrapper script that setup-terraform installs. Its README says the wrapper can be skipped when later steps in the job do not need the results of Terraform commands.

HCP Terraform can run automatic health assessments in a workspace, and drift detection is part of them.

Adopt or revert the change

The tutorial shows both responses.

To revert, run a normal terraform apply. Terraform attempts to revert the manual change.

To adopt, run terraform apply -refresh-only. It makes the state file match the real infrastructure, but not the configuration. The tutorial then updates the configuration to match and applies again.

A resource that was created outside Terraform is a different case. An import block brings it into the state.

Changes Terraform should ignore

The ignore_changes argument lists resource attributes that Terraform should ignore when it plans updates. HashiCorp's example ignores tags, so that a management agent can update them from a rule set managed elsewhere:

HCL

resource "aws_instance" "example" {
  # ...

  lifecycle {
    ignore_changes = [tags]
  }
}

Terraform still uses the listed arguments when it plans a create operation, and ignores them when it plans an update. The keyword all in place of the list covers every attribute. Terraform can then create and destroy the object but will never propose updates to it. You cannot apply ignore_changes to itself or to any other meta-argument.

Preventing drift

  • Start from the tutorial's rule: no manual changes to resources that Terraform controls. Give people read-only access to production, and let the pipeline's role make the changes.
  • Run the scheduled plan against every configuration you apply.
  • When the check fails, choose one of the two responses: an apply to revert, or a refresh-only apply and a code change to adopt.
  • Keep -refresh=false and -minimal-refresh out of the check.
  • List in ignore_changes only the attributes that another process manages.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides