Infrastructure as Code Drift: Detection, Diagnosis, and Safe Remediation
Terraforminfrastructure as codecloud operationsconfiguration managementplatform engineering

Infrastructure as Code Drift: Detection, Diagnosis, and Safe Remediation

DDeploy Website Editorial Team
2026-11-29
8 min read

Learn how to detect infrastructure as code drift, diagnose its cause, and safely reconcile Terraform code, state, and live cloud resources.

Infrastructure as code drift occurs when the real configuration of a cloud environment no longer matches the configuration defined in code or recorded in state. This guide provides a repeatable workflow for detecting drift, diagnosing its cause, and choosing a safe remediation path without blindly applying a destructive plan.

Overview

Infrastructure as code (IaC) drift is not limited to a resource changed manually in a cloud console. It can also result from an outdated Terraform state file, an incomplete import, a provider behavior change, a module update, a failed deployment, or another automation system modifying the same resource. The visible symptom is often a surprising plan: a proposed replacement, an unexpected attribute change, or a large number of updates after a period of inactivity.

The safest response is to treat drift as an investigation rather than a command to approve. First establish what changed, then determine which representation is authoritative. The desired configuration in version-controlled code, the recorded state, and the live provider or cloud API may each contain different information. Remediation should bring those representations back into agreement while preserving intentional changes and protecting critical resources.

Three representations to compare

  • Configuration: the Terraform files, modules, variables, and provider settings that describe the intended result.
  • State: the tool-managed record connecting configuration addresses to real resources and their known attributes.
  • Remote reality: the current configuration returned by the cloud provider or other infrastructure API.

Drift detection compares some or all of these representations. A plan can reveal differences between state and remote infrastructure, while code review and state inspection help identify differences between configuration and state. The distinction matters: refreshing state may explain a difference, but it does not necessarily fix it.

Drift-detection methods

MethodUseful forLimitations
Routine plan in CIDetecting changes before an approved deploymentMay miss resources or workspaces excluded from the pipeline
Scheduled plan or drift monitorFinding changes between normal releasesRequires clear ownership, credentials, and alert handling
Cloud audit logsIdentifying who or what changed a resourceLogs may have retention, permission, or attribution gaps
State and configuration reviewDiagnosing imports, renames, and module changesDoes not independently prove the live resource matches state
Provider or API inspectionChecking values that plans report as changedDisplayed values may differ in format or default handling

Checklist by scenario

Scenario 1: A plan shows unexpected changes

  1. Save the plan output and identify the exact resource addresses and attributes involved.
  2. Classify each change as an in-place update, a create, a destroy, or a replacement. Treat replacement proposals as a stop-and-review event.
  3. Confirm the selected workspace, account, region, subscription or project, and variable files. A correct plan against the wrong target is still dangerous.
  4. Review recent commits, module changes, provider version changes, imports, and state operations.
  5. Inspect the live resource through the provider console, API, or command-line tooling, and compare it with both code and state.
  6. Ask whether the change is intentional. If it is, document the decision before applying it; if not, continue diagnosis.

Do not use a broad apply to discover what will happen. Generate a reviewable plan, limit the scope where appropriate, and require an independent review for changes affecting production, access control, networking, data, or replacement-sensitive resources.

Scenario 2: Someone changed infrastructure manually

Emergency console changes are sometimes necessary, but they create a decision point rather than an automatic error. First record the reason, time, affected resources, and person or system that made the change. Then choose one of two paths:

  • Keep the manual change: update the IaC configuration to represent the desired result, validate it, and commit the change through the normal review process.
  • Revert to code: confirm that the code still represents the approved state, review the resulting plan, and apply it only after checking operational impact.

If the change is temporary, mark an owner and expiry point. Temporary exceptions become persistent drift when nobody is responsible for converting them into code or removing them.

Scenario 3: A resource was created outside the IaC workflow

Use an import workflow when the resource should become managed by code. Before importing, create or revise the resource definition and decide which attributes should be explicit, variable-driven, or left to provider defaults. After import, run a plan and reconcile differences gradually. An import connects a resource to state; it does not automatically produce a complete, maintainable configuration.

Scenario 4: A module, provider, or resource address changed

Separate a logical rename from a physical replacement. If the real resource is unchanged but its address moved because of a module refactor, use the tool's supported state-move or address-migration process and review the plan afterward. If a provider upgrade changes schemas, defaults, normalization, or replacement behavior, test the upgrade in an isolated environment and read the relevant upgrade guidance before changing production state.

Scenario 5: The plan suggests destructive remediation

Pause. Confirm whether the resource has a changed identity, immutable attribute, missing import, incorrect provider context, or stale state entry. Check backups, recovery procedures, dependencies, and maintenance requirements. For production systems, consider a staged change, a targeted migration, or a safer rollout pattern. A deployment strategy such as blue-green or canary deployment may be relevant when infrastructure changes affect application availability, but it does not replace resource-level review.

What to double-check

  • State location and locking: Verify that the pipeline and local operator are using the same intended backend, workspace, and locking behavior. Never edit state by hand unless the tool documentation and recovery plan explicitly support the operation.
  • Resource identity: Check IDs, names, regions, accounts, projects, subscriptions, and provider aliases. A mismatch can look like drift when the configuration is simply targeting a different object.
  • Ignored attributes: Review lifecycle ignore rules and equivalent settings carefully. They can suppress useful signals and allow changes to remain unmanaged.
  • Defaults and normalization: Providers may omit defaults, reorder values, normalize formats, or expose computed attributes differently from configuration. Confirm behavior rather than assuming every diff represents a human change.
  • Secrets and sensitive values: Avoid copying credentials or sensitive state into tickets and chat. Limit state access, protect backend storage, and use redacted plan output where possible.
  • Dependencies: Check security groups, identity bindings, routes, databases, storage, certificates, and Kubernetes or application consumers before changing a shared resource.
  • Operational signals: Review alerts, logs, error budgets, and recent incidents before applying remediation. Reliability context can change the safest order of operations; an SLO and error budget review can help frame a production decision.

A practical remediation decision tree

  1. Is the difference intentional? If yes, update code and document the change. If no, continue.
  2. Is the configuration targeting the correct object? If no, correct the workspace, provider, account, or address before doing anything else.
  3. Is the resource unmanaged or incorrectly associated with state? If yes, use a reviewed import, move, or state-reconciliation operation.
  4. Does the plan still propose a safe in-place change? If yes, validate dependencies and apply through the normal approval path.
  5. Does it propose replacement or deletion? Stop, identify the immutable or missing input, verify recovery options, and design a migration or controlled replacement plan.

Common mistakes

  • Applying the plan just to make the warning disappear: A clean plan is not the only goal; preserving service, data, access, and auditability matters more.
  • Refreshing state and assuming drift is fixed: Refreshing updates what the tool knows. It does not make the live system match code.
  • Editing state as a shortcut: Manual state changes can hide the relationship between configuration and infrastructure. Prefer documented commands, backups, and peer review.
  • Allowing multiple automation owners: Terraform, deployment scripts, operators, and cloud-native controllers should have clearly defined boundaries. Shared ownership of one attribute is a recurring source of drift.
  • Ignoring failed applies: A partially completed operation can leave state and remote infrastructure out of sync. Preserve logs, inspect both sides, and recover deliberately.
  • Importing without writing configuration: Imported state without maintainable code creates a fragile arrangement that may drift again at the next plan.
  • Excluding too much with ignore rules: Ignore only attributes that have a documented external owner and a way to monitor the exception.

Teams can reduce repeat incidents by defining who may make emergency changes, how those changes are recorded, how quickly they must be reconciled, and which environments require a scheduled drift check. These controls should be proportionate to the risk and maturity of the platform.

When to revisit

Review this drift process before seasonal planning cycles, major infrastructure changes, and changes to the delivery workflow. Revisit it whenever you upgrade providers, reorganize modules, change state backends, add accounts or regions, adopt a new platform team boundary, or introduce another controller that can modify managed resources.

Use the following short review before each significant change:

  1. List the resources and environments in scope.
  2. Confirm the source of truth for each resource and attribute.
  3. Run and save a plan from the intended workspace and provider context.
  4. Investigate every unexpected change, especially replacement and deletion.
  5. Verify backups, rollback or recovery steps, monitoring, and an accountable reviewer.
  6. After applying, run a follow-up plan and record any remaining exception.

Infrastructure as code works best when it is treated as an operating discipline, not only a provisioning mechanism. Clear ownership, protected state, routine detection, and cautious remediation make drift easier to explain and safer to correct. For adjacent platform work, pair this process with resource sizing guidance such as the Kubernetes resource requests and limits guide, and standardize the developer tools and review steps that make infrastructure changes visible to the whole team.

Related Topics

#Terraform#infrastructure as code#cloud operations#configuration management#platform engineering
D

Deploy Website Editorial Team

Cloud and Platform Engineering Editor

Senior editor and content strategist. Writing about technology, design, and the future of digital media. Follow along for deep dives into the industry's moving parts.