Skip to content

Infrastructure as Code

Infrastructure as Code defines servers, networks, and cloud resources in machine-readable files that are versioned, tested, and applied automatically.

Definition

What it is

Infrastructure as Code (IaC) is the practice of describing computing infrastructure — servers, networks, load balancers, databases, permissions — in files that a machine can read and a tool can apply. Instead of logging into a console and clicking "create instance", you write a file that says "create this instance" and run a tool that reads it. The file is the source of truth. The console becomes a viewer of the result.

The practice has three components: declarative definitions, version control, and automated application. Declarative definitions describe the desired end state, not the sequence of commands to get there. Version control makes every change reviewable, revertible, and attributable. Automated application removes the human hand from the execution path, so the same file produces the same result every time it runs.

Why it matters

Manual infrastructure does not scale. A single engineer can provision a dozen servers by hand; ten engineers provisioning a hundred servers by hand will produce a hundred snowflakes. No two environments stay identical. Configuration drift accumulates silently. Rollback becomes an archaeology project: nobody remembers exactly what was changed, by whom, or why. Recovery from a disaster depends on one person's memory.

IaC replaces this with a workflow that mirrors software engineering. Changes go through pull requests. Reviews catch destructive diffs. Tests validate the plan. Deployment is one command. Rollback is a revert. The same code that provisions a staging environment provisions production, differing only in input variables. Environments stay identical because they are built from the same definition.

How it works

The mechanism rests on three phases: read the definitions, diff them against reality, apply the difference. The tool keeps a record of what it believes the world looks like (the state) and compares it to what the definitions say it should look like. The difference is presented as a plan. The engineer reviews the plan, approves it, and the tool executes it.

graph LR
  D[Definition files] --> P[Plan]
  S[Current state] --> P
  P --> R{Review}
  R -->|approve| A[Apply]
  R -->|reject| D
  A --> NS[New state]
  NS --> S

The declarative model is what distinguishes IaC from classic provisioning scripts. A script says "run these commands in this order". A declarative file says "the world should look like this". The tool figures out the order. If you re-run a script, it may create duplicate resources or fail. If you re-apply a declarative definition, it does nothing because the world already matches. That property is called idempotency, and it is what makes IaC safe to run repeatedly.

Adopting IaC in an existing project

  1. Start with a single resource

    Import one existing resource into IaC. Configure the tool, describe the resource, run import, then verify no changes appear in the next plan.

  2. Version the state

    Store the state file in a remote backend with locking. Never commit it to Git alongside the definitions.

  3. Pipeline the apply

    Move the plan and apply steps into CI. Require a reviewer for any plan that destroys or replaces a resource.

  4. Layer the estate

    Split definitions into layers: network, compute, data, IAM. Apply layers in order; keep each layer's blast radius small.

Most tools maintain a state file that maps definitions to real resources. Terraform keeps terraform.tfstate. Pulumi keeps a stack state. CloudFormation keeps a stack. The state file is the tool's memory: without it, the tool cannot know what exists and what needs to change. Losing the state file is expensive because you have to import every existing resource manually.

Common failures

A second failure is state file mismanagement. Committing the state file to the same repository as the definitions exposes secrets (many state files contain plaintext credentials). Sharing one state file across multiple engineers without locking produces corruption when two applies run simultaneously. The fix is a remote backend with locking (S3 + DynamoDB, Terraform Cloud, GCS, Azure Blob with leases).

A third failure is silent drift. Someone opens the console, edits a security group rule, and forgets. The next apply reverts the change, sometimes with unintended consequences. Scheduled drift detection (plan on a cron, or a dedicated tool) catches the problem before it becomes an incident.

Origin

Infrastructure as Code has two roots. The first is configuration management: CFEngine (1993), Puppet (2005), Chef (2009), and Ansible (2012) all treated server configuration as code before the term existed. The second is cloud APIs: Amazon EC2 (2006) turned infrastructure into a programmatic interface for the first time. Combining the two — defining cloud resources in files and applying them automatically — is what became IaC.

The term itself was popularized by Kief Morris's book Infrastructure as Code (O'Reilly, 2016), which formalized the practice and gave it a coherent methodology. HashiCorp's Terraform (2014) turned the practice into a widely adopted tool. OpenTofu (2024) is a fork of Terraform created after HashiCorp changed the license.

Infrastructure as Code is the process of managing and provisioning computer data centers through machine-readable definition files, rather than physical hardware configuration or interactive configuration tools.

— Kief Morris, *Infrastructure as Code*, O'Reilly, 2016

Evidence

Timeline

  1. 1993 — CFEngine released; first configuration-management tool.

  2. 2005 — Puppet published. Configuration as code becomes a discipline.

  3. 2006 — Amazon EC2 launches; infrastructure becomes an API.

  4. 2009 — Chef released.

  5. 2010 — Vagrant 0.1 (development environments as code).

  6. 2012 — Ansible released. AWS CloudFormation launches.

  7. 2014 — HashiCorp releases Terraform 0.1.

  8. 2016 — Kief Morris publishes Infrastructure as Code (O'Reilly).

  9. 2019 — Pulumi 1.0 brings general-purpose languages to IaC.

  10. 2023 — HashiCorp changes Terraform's license to BUSL.

  11. 2024 — OpenTofu 1.0 fork goes GA under the Linux Foundation.

FAQ

Does IaC replace manual configuration entirely?

In mature teams, mostly. In practice, most organizations keep manual access for emergencies (break-glass procedures) and audit every such access afterward. The goal is not zero manual actions; it is zero undocumented manual actions. The definition file remains the source of truth, and any drift caused by manual intervention is detected and reconciled.

Terraform or Ansible?

They solve different problems. Terraform is declarative provisioning: "create this VPC, this database, this DNS record". Ansible is procedural configuration: "install these packages, edit these files, restart these services". Many teams use both — Terraform for infrastructure, Ansible for host configuration. Pulumi and CloudFormation occupy the Terraform role. Choose by lifecycle, not by popularity.

What is drift and how do I handle it?

Drift is the difference between the declared state and the real state. It happens when someone edits a resource outside IaC (console, CLI, manual script). Detecting drift requires running a plan on a schedule and alerting on any unexpected diff. Handling it requires deciding, per case, whether to revert the drift or codify it — sometimes the manual change was the right one and the definition needs updating.

Does IaC only work for cloud?

No. IaC works for any infrastructure with an API: physical data centers, networking equipment, Kubernetes clusters, DNS providers, even SaaS configuration. The term is often associated with cloud because cloud APIs made the practice mainstream, but on-premises environments benefit from the same model.

What is the state file and why does it matter?

The state file maps declared resources to real resource IDs. Without it, the tool cannot know that aws_instance.web is the specific EC2 instance in production, so it cannot plan changes safely. Losing the state file means every resource has to be manually re-imported. The standard practice is a remote backend with locking (S3 + DynamoDB, GCS, Terraform Cloud) and versioning enabled.

Connections

InfrastructuContinuousOpenTofuAnsibleKubernetesDockerGitHub ActDeployIdempotentGit

● broader ● narrower ● related

Used in the garden

Sources

Etymology

Origin: acronym

  • infrastructure (noun): from Latin infra, below, + structura, structure; the underlying foundation
  • as (preposition): in the manner of
  • code (noun): from Latin codex, a book or systematic collection of laws

Semantic network

InfrastructuContinuousOpenTofuAnsibleKubernetesDockerGitHub ActDeployIdempotentGit

● broader ● narrower ● related

Used in the garden

Sources