TExcelTAD

Drift & Refresh

Drift is any difference between the real infrastructure and what the code and state say: someone opened port 22 in the console during an incident, an autoscaler changed a desired count, a colleague deleted a "test" bucket that was in fact managed. The tool detects drift during every plan's refresh step, when each provider reads the current version of every object in state.

Detecting it

bash · try it yourself
mkdir d && cd d
cat > main.tf <<'HCL'
resource "local_file" "nginx" {
  filename = "nginx.conf"
  content  = "worker_processes 2;\n"
}

resource "local_file" "motd" {
  filename        = "motd.txt"
  content         = "hi\n"
  file_permission = "0644"
}
HCL
tofu init >/dev/null && tofu apply -auto-approve >/dev/null
echo "--- someone edits the server by hand"
echo "worker_processes 8;" > nginx.conf
chmod 600 motd.txt
tofu plan -detailed-exitcode | grep -E '^  #|^Plan:'; echo "exit=${PIPESTATUS[0]}"
echo "--- what changed outside the code"
tofu plan -refresh-only | grep -E '^  #|^Note: Objects'
echo "--- apply puts the file back"
tofu apply -auto-approve >/dev/null
cat nginx.conf
--- someone edits the server by hand
  # local_file.nginx will be created
Plan: 1 to add, 0 to change, 0 to destroy.
exit=2
--- what changed outside the code
Note: Objects have changed outside of OpenTofu
  # local_file.nginx has been deleted
--- apply puts the file back
worker_processes 2;

Three things to notice:

  • plan reverts drift. The code is the source of truth, so the plan undoes the manual edit. That is what you want for an unauthorised change, and a nasty surprise if the edit was an emergency fix nobody put back into the code.
  • -refresh-only shows the drift itself ("Objects have changed outside of OpenTofu") without proposing to fix anything.
  • A provider only sees what it reads. The local provider identifies a file by the hash of its content, so a changed file looks deleted, and it doesn't check permissions at all, so chmod 600 went unnoticed. Cloud providers read nearly every attribute, but not all (some API fields are write-only), so drift detection is good, not perfect.

Deciding what to do

Situation Action
Unauthorised or accidental change apply: the code wins and the change is reverted
A legitimate change (an incident fix) put it into the code, then plan shows no changes
A value another system owns (autoscaler, tagging policy) lifecycle { ignore_changes = [...] }
The code should accept reality as is apply -refresh-only: record the new values in state, change nothing

apply -refresh-only replaced the old tofu refresh command, which wrote state without showing you anything first. It is useful after a drift you accept, but remember it only updates state: the code still says the old value, and the next normal plan will want to revert it unless you also change the code.

Drift against AWS

A sample session: a security group rule was added in the console, and tags were changed by hand.

bash
$ tofu plan
aws_security_group.web: Refreshing state... [id=sg-0f1e2d3c4b5a69788]

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:

  # aws_security_group.web has changed
  ~ resource "aws_security_group" "web" {
        id      = "sg-0f1e2d3c4b5a69788"
      ~ ingress = [
          + {
              + cidr_blocks = ["0.0.0.0/0"]
              + from_port   = 22
              + protocol    = "tcp"
              + to_port     = 22
                # (4 unchanged attributes hidden)
            },
            # (1 unchanged element hidden)
        ]
      ~ tags    = {
          ~ "Owner" = "platform" -> "ada"
        }
        # (8 unchanged attributes hidden)
    }

OpenTofu will perform the following actions:

  # aws_security_group.web will be updated in-place
  ~ resource "aws_security_group" "web" {
…
Plan: 0 to add, 1 to change, 0 to destroy.

Catching drift early

Drift found during an unrelated change is the worst time to find it. Run a scheduled job instead:

yaml
# .github/workflows/drift.yml (excerpt)
on:
  schedule:
    - cron: "0 6 * * 1-5"          # weekday mornings
jobs:
  drift:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: opentofu/setup-opentofu@v2
      - run: tofu init -input=false
      - run: tofu plan -input=false -detailed-exitcode -lock=false
        # exit 2 = drift: the job fails and notifies the owners

-lock=false is acceptable here because the job never writes state; everywhere else keep locking on. Reduce the causes too: read-only console access for humans in production, changes through pull requests only, and break-glass roles whose use is audited (CloudTrail, see CloudTrail and AWS Config).

Test yourself

Someone changed a tag in the AWS console. What does a normal tofu plan propose?

  1. Nothing; manual changes are kept
  2. To change the tag back to the value in the code
  3. To delete the resource
  4. To import the change into the code

What does tofu apply -refresh-only do?

  1. Reverts drift
  2. Records the real values in state without changing any infrastructure
  3. Deletes the state
  4. Updates the code

Why might a manual change go undetected by plan?

  1. Plans never check reality
  2. The provider doesn't read that attribute back from the API
  3. Locking hides it
  4. The lock file is outdated