The Practitioner’s Guide to Scaling Infrastructure as Code

➡️ Download Now

General

How to Manage Cloud Infrastructure Drift & Best Practices

drift management

Infrastructure drift is the difference between live resources and the state defined in your IaC config files. It’s a common cause of errors, unexpected behavior, and compliance violations.

As your infrastructure inventory scales, you need to plan how you’ll detect and correct drift to maintain stable operations. Implementing a configuration drift management strategy allows you to deal with drift pragmatically. This article will explore techniques you can use to take control of drift, then show how Spacelift lets you automate drift detection and reconciliation.

  1. What is drift in cloud infrastructure?
  2. Managing Cloud infrastructure drift
  3. Best practices for implementing cloud drift management
  4. Drift management with Spacelift

What is drift in cloud infrastructure?

Drift in cloud infrastructure refers to the situation where the actual state of the infrastructure in a cloud environment deviates from the desired state defined by Infrastructure as Code (IaC) or configuration management tools. It is the discrepancy between what should be running and what’s actually running. 

When you provision infrastructure with an IaC tool, you expect that your resources will match the IaC code you applied. But in practice, it’s common to find differences after your resources have been running for a while. Resources tend to drift away from the correct state.

Drift can be caused by several different factors:

  • Manual changes: One of the most common sources of drift is when engineers modify live infrastructure directly, often using local CLIs and other unmanaged tools.
  • Conflicting tools: Combining multiple IaC and CI/CD tools can enable more powerful infrastructure workflows, but it may also cause drift if tools conflict and overwrite each others’ changes.
  • Automatic updates: Auto-updates keep you protected from zero-day security issues, but they can mean your live resources end up running different versions to those listed in your IaC files.
  • External dependencies: Dependencies may trigger drift if they’re broken, incorrectly configured, or enter a failed state. For instance, if your IaC file uses an external service to provision a resource, then drift may occur if that service releases a breaking change that produces a different output.

These factors fall into two main groups: changes introduced by actors external to your IaC tool, such as developers and scripts, and dependencies on external systems that affect your IaC configurations.

It’s not possible to completely prevent drift. Buggy tools, essential auto-updates, and unavoidable manual config changes all mean drift should be expected at scale. Hence, it’s crucial to configure tooling that efficiently detects and resolves drift when it occurs.

Types of cloud infrastructure drift

The types of cloud infrastructure drift include:

Type of drift Description Examples
Configuration drift Misaligned settings or parameters in cloud services. Security group rules modified, encryption settings disabled.
Resource drift Resources created or deleted outside the defined IaC. Unplanned S3 bucket created, missing EC2 instance.
Security drift Security policies altered, reducing protection or compliance. IAM roles changed, public access enabled on storage.
Network drift Unplanned changes in networking components. Routing table modified, firewall rules altered.
State drift Resource lifecycle states don’t match expectations. Instance stopped when it should be running, database in maintenance mode.
Performance drift Changes affecting resource behavior or scaling. Autoscaling thresholds changed, instance performance downgraded.
Policy drift Non-compliance with governance rules or standards. Missing resource tags, cost policy overrides.
Dependency drift Alterations in connections or dependencies between services. Lambda pointing to the wrong endpoint, database connection modified.

What is drift management?

Infrastructure drift management refers to the process of identifying, tracking, and correcting discrepancies between the desired (or provisioned) state of cloud infrastructure and its actual state, which are known as drifts. Drift can occur when infrastructure changes happen outside of controlled processes (e.g., manual updates, or unauthorized modifications), leading to configurations that deviate from the intended baseline.

Managing cloud infrastructure drift

Managing drift is essential to ensure consistency, security, and compliance across your cloud environment. Here are the key steps to manage cloud infrastructure drift effectively:

1. Detecting drift

You can detect cloud infrastructure drift by comparing the state of the live resources in your cloud accounts to the declared configuration in your IaC files. If there’s a difference, then drift has occurred and should be corrected. Developers need to be able to quickly find drift to apply effective mitigations.

One of the easiest ways to detect drift is your IaC tool’s built-in execution plan. Commands such as terraform plan and pulumi preview reveal the changes that would be made to your infrastructure if you applied the config currently in your repository. 

If your cloud resources are already correctly configured, no changes should be required. If the IaC tool reports actions are needed, then drift has occurred. Applying the plan will then restore the correct state.

IaC CLIs allow you to perform one-off checks for drift, but they’re not suitable for ongoing drift detection at scale. Implementing continuous infrastructure monitoring allows you to find drift as it happens, mitigating its effects on your infrastructure. 

For example, Spacelift’s drift detection mechanism periodically scans your environments for discrepancies, then triggers an automatic reconciliation job to resolve detected drift. This rapidly fixes issues without needing developer input.

2. Analyzing drift patterns

Effective drift management requires regular analysis of the causes of drift incidents. If drift keeps occurring for the same reasons, then this indicates an opportunity to improve your processes. For instance, you may find that developers using a local Kubectl CLI to manipulate staging environments are causing drift in your Kubernetes deployments.

Comparing newly detected drift to previous events lets you spot these patterns so you can prevent future drift. This enables a more proactive approach to drift management. Instead of waiting for drift to happen, you’ll be able to anticipate possible sources of drift and implement safeguards sooner.

3. Handling drift resolution

Drift is resolved by realigning the state of your cloud resources with the configuration from your IaC repositories. Reconciling the two states removes the drift from your environment.

You can use your IaC tool to reapply the correct configuration, recovering your infrastructure to how it was before the drift occurred. Check the execution plan first in case any destructive operations will be applied. In some cases, you may need to manually patch cloud resources to nudge your tool towards a safer course of action.

Once you’ve resolved the drift, it’s important to verify that the rollout was successful and the drift doesn’t recur. Periodically repeat your IaC tool’s execution plan to confirm that your infrastructure has now stabilized on the correct IaC configuration.

Implications of unmanaged drift

Leaving drift uncorrected can quickly cause more serious problems in your infrastructure. Not only does drift impede visibility into your resources, but it also threatens the stability and reliability of your apps. Configuration drift could introduce errors that prevent your app from working properly, causing disruption for users. Depending on the resources that are impacted, drift may even pose a security risk if it means correct policies are no longer applied.

These drifts can lead to:

  1. Security vulnerabilities: Misconfigurations expose systems to breaches and compliance violations.
  2. Operational instability: Inconsistent infrastructure may cause unexpected downtime or performance issues.
  3. Increased costs: Resource sprawl or misallocated services can result in excessive cloud spend.
  4. Compliance risks: Drift can lead to non-compliance with regulations and policies.
  5. Inefficient debugging: Diagnosing issues becomes harder when the infrastructure state is unpredictable.

Drift often becomes harder to resolve when it’s left for longer periods of time. Drift in one service can impact others, creating drift chains that are more challenging to investigate and correct. More extensive drift makes it more likely that restoration will be disruptive, such as when resources change so much that they must be recreated instead of reverted.

For these reasons, it’s crucial to regularly monitor for drift and resolve problems as soon as they’re detected. This will minimize the risk of disruption to apps and users and prevent drift-induced disasters.

Best practices for managing cloud infrastructure drift

Now we’ve learned how to detect and resolve drift, here are some best practices that will help keep your infrastructure running reliably.

  • Make IaC and CI/CD the only way to apply infrastructure changes: Ensuring all changes are applied by automated workflows ensures developers can’t introduce discrepancies or overwrite previous revisions.
  • Prevent unauthorized changes by restricting user privileges: Locking down infrastructure access further prevents accidental changes from being made. Use central platforms like Spacelift to provide safe self-service access to just the users who need it.
  • Regularly run IaC drift detection and reconciliation tools: Drift detection tools enable continuous scanning for discrepancies, while automated reconciliation makes it easy to restore the correct state.
  • Implement instant drift detection alerts: Real-time drift alerts ensure you’re notified as soon as differences are detected. This helps minimize how long the drift lasts, reducing the resolution work required.
  • Reduce external infrastructure dependencies: Avoiding external dependencies reduces your infrastructure’s exposure to drift. Only add dependencies if they’re trusted services with proven reliability.
  • Educate developers on the causes of drift: Helping developers understand the causes of drift makes mistakes less likely. Devs will be able to anticipate where drift could occur, allowing them to proactively avoid it.
  • Store all IaC code in central repositories: Ensuring all IaC code is stored centrally prevents infrastructure configuration from becoming fragmented. There’s a single place for new changes to be made, preventing drift caused by devs working from different unsynchronized repositories.
  • Immediately commit infrastructure hotfixes back to IaC repos: Urgent bugs and vulnerabilities sometimes need to be patched in production. Bypassing IaC pipelines enables a faster rollout but causes drift if the change isn’t immediately reflected in the IaC repository. Commit all hotfixes as soon as they’re applied so they’re not lost nthe ext time your IaC tool runs.

Automation and consistency are the two main themes of these best practices. Drift will be less common and easier to fix when infrastructure is exclusively managed using IaC and CI/CD tools.

Drift management with Spacelift

Spacelift includes a powerful drift management feature that automates drift detection and reconciliation for your IaC resources.

Spacelift periodically compares your live infrastructure to the current state defined in your IaC configuration. If any differences are found, then a reconciliation job is automatically created to restore the correct state.

Drift reconciliation jobs execute as tracked Spacelift runs. You can govern them using Spacelift’s standard policy features, ensuring required criteria are met before any changes get applied. For instance, you may enforce that manual approval is needed before a reconciliation run can start.

To get started enabling drift detection for a Spacelift stack, head to your stack’s Scheduling tab and select Create schedule > Drift detection:

Next, enter a cron expression to define your drift detection schedule. Ensure the “Reconcile” toggle button is enabled to have Spacelift automatically create reconciliation runs:

Your drift detection config will activate when you press the “Create” button. Jobs will start scheduling to detect the drift in your live infrastructure once your stack enters the Finished state. You can monitor your drift detection schedule and inspect drift detection runs in your Stack’s Scheduling tab.

If you want to take your infrastructure automation to the next level, create a Spacelift account today or book a demo with one of our engineers.

Key points

Drift management is the process of detecting and resolving cloud infrastructure configuration drift. Drift commonly occurs when changes are made outside IaC or your configuration depends on unreliable external dependencies. Addressing these issues will help reduce drift in your environments, but it’s still possible that some problems will occur.

Scheduled scans and automated reconciliation runs are the best way to monitor drift. Drift detection tools like Spacelift let you find, fix, and monitor drift in real time, making it easy to keep cloud infrastructure in its expected configuration. You can use policies to control when reconciliation occurs, such as only when manual approval is granted or the required change is small.

Detect and Remediate Drift with Spacelift

Drift happens, so let Spacelift deal with it. Spacelift provides drift detection capabilities to any IaC provider to enable the desired state for application infrastructure across teams, applications, and clouds.

Learn more

The Practitioner’s Guide to Scaling Infrastructure as Code

Transform your IaC management to scale

securely, efficiently, and productively

into the future.

ebook global banner
Share your data and download the guide