Volume 9 · Chapter 6 — CI/CD, DevOps and Infrastructure as Code

GitOps, Environment Consistency, and Drift

Goutham Mayaluri 8 min read AWSCloudCI/CDInfrastructure as Code

6.1 Introduction

The final pieces of reliable delivery are keeping everything driven from version control, keeping environments consistent, and preventing drift - the silent divergence of running systems from their definitions. GitOps captures the idea of Git as the single source of truth for both application and infrastructure state. This chapter ties together the volume’s threads into an operating model where the desired state lives in version control and the system continuously matches it - a maturity marker interviewers look for.

6.2 Git as the Source of Truth

GitOps is an operating model where the desired state of your system - application deployments and infrastructure - is declared in Git, and automation continuously ensures the running system matches what’s in Git. Instead of making changes directly to the environment, you change the declaration in Git (through a reviewed pull request), and the automation applies it. This makes version control the single source of truth: what’s in Git is what should be running, every change is reviewed and recorded there, and you can see the full history and revert by reverting a commit. It unifies the discipline already applied to application code and infrastructure as code into one model - everything is declared in version control, changed through review, and reconciled automatically - which gives auditability, reproducibility, and easy rollback across the whole system.

flowchart LR
  Dev[Change via reviewed PR] --> Git[(Git - desired state:\napp + infra)]
  Git --> Auto[Automation reconciles]
  Auto --> Env[Running environment]
  Env -. drift detection: does reality match Git? .-> Git
  Note[Change the declaration, not the environment directly] -.-> Dev

6.3 Consistent Environments

A recurring benefit throughout this volume is consistent environments, and it comes from defining everything in code and promoting the same artifacts and templates. When development, staging, and production are all provisioned from the same infrastructure-as-code (parameterized per environment) and run the same promoted artifacts, they are genuinely alike - so testing in staging is a faithful predictor of production, and the “works in one environment but not another” class of problems disappears. Configuration differences are explicit, version-controlled parameters, not ad-hoc manual tweaks. This consistency is what makes the whole pipeline trustworthy: each stage validly predicts the next because the environments are the same but for declared, reviewed configuration differences.

6.4 Preventing and Detecting Drift

Configuration drift is the silent enemy: when someone makes a manual change directly to a running environment, the live system no longer matches its code definition, so the code is no longer the truth, environments diverge, and the next automated apply may revert the manual fix or behave unpredictably. The prevention is discipline: prohibit manual changes to production, make all changes through the reviewed pipeline/Git so the definition stays authoritative, and restrict direct access so people can’t casually change live infrastructure (with audited break-glass access for genuine emergencies). The detection is drift detection - tooling that periodically compares the running state to the declared state and alerts on any divergence - so drift is caught and corrected quickly. Keeping the running system continuously matching version control is what makes infrastructure knowable and reproducible.

6.5 Rollback and Recovery Through Version Control

A powerful consequence of the everything-in-version-control model is that rollback and recovery become straightforward. Because the desired state - application versions and infrastructure - is all declared and versioned in Git, rolling back a bad change is reverting to a previous known-good commit and letting automation reconcile, and rebuilding an environment for disaster recovery is applying the code from Git. There’s no guesswork about what the correct state was, because it’s recorded. This closes the loop with the reliability practices from earlier volumes: safe deployments give a fast rollback of the application, and GitOps/IaC give a fast, reliable way to restore both application and infrastructure to a known-good declared state - so recovery is a controlled, reproducible operation rather than a scramble.

6.6 Banking Production Scenario

A bank operates a GitOps model: the desired state of its applications and infrastructure lives in Git, and changes are made only through reviewed pull requests, never directly to environments. Automation reconciles the running systems to Git, and drift detection alerts if any environment diverges - manual production changes are prohibited, with access restricted and break-glass audited. Development, staging, and production are provisioned from the same IaC parameterized per environment and run the same promoted artifacts, so they’re genuinely consistent and staging faithfully predicts production. Rollback is reverting a commit; disaster recovery is re-applying from Git. The result is a fully auditable, reproducible operating model where version control is the single source of truth for everything running - exactly the control a regulated bank requires.

6.7 Troubleshooting: Drift and Inconsistency

Drift and inconsistency problems have a common root: changes made outside version control. Environments behaving differently despite “identical” setups mean manual changes crept in or they weren’t all provisioned from the same code - provision everything from IaC and prohibit manual changes. Live infrastructure not matching its definition is drift from manual edits - route all changes through Git/the pipeline and run drift detection to catch and correct it. An automated apply unexpectedly reverting a manual fix confirms the fix bypassed the source of truth - make the change in Git instead. Uncertainty about the correct state to recover to means the state wasn’t in version control - declare everything in Git. The remedy throughout is the GitOps discipline: declare desired state in version control, change it only through review, reconcile automatically, prohibit manual changes, and detect drift - so the running system always matches a known, reviewed, recoverable definition.

Interview Question

What is GitOps, and how do you prevent configuration drift?

GitOps is an operating model where the desired state of your entire system - both application deployments and infrastructure - is declared in Git, and automation continuously ensures the running system matches what’s declared there. Instead of making changes directly to an environment, you change the declaration in Git through a reviewed pull request, and the automation applies it to reconcile the running system to the new desired state. This makes version control the single source of truth: what’s in Git is what should be running, every change is reviewed and recorded, you can see the full history, and you can revert by reverting a commit. It unifies the discipline already applied separately to application code and to infrastructure as code into one model where everything is declared in version control, changed only through review, and reconciled automatically - which gives auditability, reproducibility, consistent environments, and easy rollback across the whole system. Configuration drift is the problem GitOps most directly addresses. Drift is the silent divergence of a running environment from its code definition, and it happens when someone makes a manual change directly to a live environment: now the running system no longer matches its declared state, so the code is no longer the truth, environments diverge from each other, and the next automated apply may revert the manual fix or behave unpredictably, and nobody is sure what the true state is. I prevent drift through discipline and detection. For prevention: I prohibit manual changes to production and require all changes to go through the reviewed pipeline or Git so the declaration stays authoritative, and I restrict direct access so people can’t casually alter live infrastructure, with audited break-glass access reserved for genuine emergencies. For detection: I use drift detection tooling that periodically compares the actual running state to the declared state and alerts on any divergence, so drift is caught and corrected quickly rather than accumulating silently. Keeping the running system continuously matching version control is what makes infrastructure knowable, reproducible, and recoverable - and it also makes rollback and disaster recovery straightforward, because the correct desired state is recorded in Git, so rolling back is reverting to a known-good commit and recovery is re-applying from Git, with no guesswork about what the state should be. So GitOps declares the whole system’s desired state in version control and reconciles the running system to it automatically, and drift is prevented by prohibiting manual changes and routing everything through reviewed Git, and caught by drift detection - which together keep the running system always matching a known, reviewed, recoverable source of truth, exactly the control a bank needs.

Chapter Revision Notes

  • GitOps: declare the desired state of apps and infrastructure in Git; automation reconciles the running system to it; Git is the single source of truth.
  • Change the declaration through reviewed PRs, not the environment directly - giving auditability, reproducibility, and easy revert.
  • Consistent environments come from the same IaC (parameterized) and same promoted artifacts, so staging faithfully predicts production.
  • Drift is manual changes making live systems diverge from their code; prevent by prohibiting manual changes and routing via Git, detect with drift detection.
  • Rollback and DR become reverting to / re-applying a known-good commit - the correct state is recorded, not guessed.