Stackbook Logo
deploymentestablished · medium operational burden

Infrastructure as Code

Also known as: iac, declarative-infrastructure

Intent

Define and provision infrastructure through version-controlled code so environments are reproducible, reviewable and testable.

Problem

Infrastructure created by clicking through consoles cannot be reproduced, reviewed or audited. Staging and production diverge, knowledge lives in individual heads, and disaster recovery means guessing what used to exist.

Forces

  • •Environments must be reproducible on demand
  • •Changes need review and an audit trail
  • •Cloud APIs are large and change often
  • •State of real resources must be tracked accurately

Solution

Overview

Infrastructure as code replaces manual provisioning with files that describe networks, clusters, databases and permissions. The same definitions create development, staging and production, which removes a large class of it-works-here bugs.

How it works

The workflow has three stages: write, plan, apply. The plan compares code against recorded state and the real provider, then lists creates, updates and destroys. A human or policy engine reviews it.

code --> plan --> review/policy --> apply --> state backend
  ^                                   |
  +------ drift check (scheduled) ----+

A minimal resource looks like this:

resource "aws_s3_bucket" "logs" {
  bucket = "acme-logs-prod"
}

State must live in a remote backend with locking so two applies cannot run concurrently.

Pitfalls

  • Local state files that get lost or diverge between engineers.
  • Giant monolithic stacks where one change plans thousands of resources. Split by lifecycle and blast radius.
  • Renaming resources without a move declaration, which destroys and recreates data stores.
  • Leaving console access open, so drift accumulates. Run scheduled plans to detect it.
  • Secrets stored in state in plain text. Treat the backend as sensitive.
  • No lifecycle protection on databases and buckets.

Key takeaways

  • Plan before apply, and review the plan, not just the code.
  • Remote locked state is mandatory for teams.
  • Split stacks by blast radius and change frequency.
  • Detect drift on a schedule.
  • Protect stateful resources against accidental destruction.

✓ When to Use

  • •Any non-trivial cloud footprint or multiple environments
  • •Needing repeatable disaster recovery or per-branch environments
  • •Compliance regimes requiring change records

✗ When Not to Use

  • •One-off throwaway experiments
  • •Resources managed fully by another system of record
  • •Teams with no capacity to maintain modules and state

Pros

  • +Reproducible environments and fast rebuilds
  • +Peer review and history for infrastructure changes
  • +Enables policy checks before apply

Cons

  • −State file management is a hazard
  • −Abstraction leaks when providers change
  • −Drift still occurs if consoles remain writable

Cost Profile

Infrastructure

Low - state storage and CI runners

Operational

Medium - module maintenance, upgrades, state hygiene

Cognitive

Medium - provider semantics and dependency graphs

Failure Modes

  • Corrupted or concurrently modified state file

  • Apply destroys a stateful resource because of a rename

  • Console drift makes the next plan unexpectedly destructive

Real-World Examples

Alternatives

    Related Patterns

    • gitops
    • immutable-infrastructure
    • disaster-recovery

    Competency Domains

    reliability opseconomics evolution