Skip to main content
RunBook Academy

← All runbooks in Terraform

medium riskcluster affecting~15 min

Runbook: Investigate Drift and Reconcile

1 · Prerequisites

Confirm every item is in place before any state change.

  • A Terraform configuration with a remote backend
  • A plan showing drift
  • Permission to read the real-world resources

2 · Pre-checks

Read-only diagnostic commands. If any of these don't match expected output, stop and investigate further.

  • · The drift is detected by terraform plan.
  • · The real-world resources are reachable.
  • · The state is in the backend.

3 · Procedure

Execute each step in order. Verify the expected output of a step before moving to the next.

  1. 1Identify the drifted resource from the plan output.
  2. 2Verify the drift via the provider API.
  3. 3Determine the cause of the drift.
  4. 4Decide: accept drift into the configuration, reconcile to the configuration, or ignore.
  5. 5Document the decision.
  6. 6Apply the chosen remediation.
  7. 7Verify the empty plan.

4 · Verification

Confirm the procedure actually fixed the problem.

  • The drift is documented.
  • The chosen remediation is applied.
  • The plan is empty after the remediation.
  • The incident is recorded.

5 · Rollback

If verification fails, undo the procedure in reverse order.

  • If the wrong remediation is applied, identify the mistake and re-apply.
  • If the state is corrupted, restore from the most recent version.

6 · Escalation

When the runbook isn't enough, contact:

  • · If the drift is from a security incident, escalate to the security team.
  • · If the drift is from a production outage, escalate to the incident commander.
  • · If the drift is from a bug in the provider, escalate to the engineering manager.

Purpose

This runbook walks through the investigation and remediation of drift. Drift is the discrepancy between the configuration and the real world. The investigation is the safety net.

When to use this runbook

Use this runbook when:

  • A plan shows drift.
  • A drift detection CI job alerts.
  • The real-world resources are different from the configuration.

Procedure

Step 1: Identify the drifted resource

The plan output shows the drift:

# aws_instance.web will be updated in-place
~ resource "aws_instance" "web" {
    ~ tags["Environment"] = "dev" -> "production"
  }

The drifted resource is aws_instance.web. The drifted attribute is tags["Environment"].

Step 2: Verify the drift

Verify the drift via the providers API:

aws ec2 describe-instances \
  --instance-ids i-0abc123def456789 \
  --query "Reservations[].Instances[].Tags"

The real-world tags include Environment: production. The configuration says Environment: dev. The drift is real.

Step 3: Determine the cause

The cause of the drift determines the remediation:

  • Manual change. An engineer edited the tag via the providers console.
  • Third-party tool. A monitoring tool added the tag.
  • Provider bug. A bug in the provider changed the tag.
  • Drift from creation. The resource was created with a different tag.

Step 4: Decide the remediation

The three possibilities:

DecisionWhenAction
Accept drift into configurationThe drift is intentional.Update the configuration; the plan is empty.
Reconcile to configurationThe drift is accidental.Apply to reconcile the real world to the configuration.
Ignore the driftThe drift is intentionally managed outside Terraform.Add lifecycle.ignore_changes to the resource.

Step 5: Document the decision

The decision is documented:

  • The drifted resource.
  • The cause of the drift.
  • The remediation chosen.
  • The rationale.

Step 6: Apply the chosen remediation

Accept drift into configuration:

# Edit the configuration to match the drift
# Apply
terraform plan
# The plan is empty

Reconcile to configuration:

# Verify the configuration is correct
terraform plan
# Apply
terraform apply

Ignore the drift:

# Add lifecycle.ignore_changes to the resource
# Apply
terraform plan
# The plan is empty

Step 7: Verify the empty plan

terraform plan

The plan is empty.

Verification

The runbook is successful if:

  • The drift is documented.
  • The chosen remediation is applied.
  • The plan is empty after the remediation.
  • The incident is recorded.

Rollback

If the procedure fails:

  • The wrong remediation is applied. Identify the mistake and re-apply.
  • The state is corrupted. Restore from the most recent version.

Escalation

Escalate to:

  • Security team if the drift is from a security incident.
  • Incident commander if the drift is from a production outage.
  • Engineering manager if the drift is from a bug in the provider.

References

  1. Drift detection
  2. Refresh behaviour