What is Infrastructure as Code (IaC)? Tools, Examples and Pitfalls

A practical DevOps definition, with an example you can use.

Infrastructure as code (IaC) is the practice of defining infrastructure, such as networks, virtual machines, databases, DNS records and Kubernetes clusters, in text files that live in version control. A tool reads those files and creates, changes or deletes real resources so they match what the code describes. Instead of clicking through a cloud console, you review and apply a change like any other code change.

Why infrastructure as code matters

Manually built infrastructure has three big problems. Nobody can say exactly how it was configured. It cannot be recreated reliably after a disaster or for a new region. And every change is a one-off, unreviewed risk.

IaC fixes all three:

  • Repeatable: the same code builds identical staging and production environments.
  • Reviewable: changes go through a pull request, so a colleague sees "this will delete the database" before it happens.
  • Auditable: Git history shows who changed what, when and why.
  • Recoverable: if an account or region is lost, you can rebuild from code.

How IaC works

Most provisioning tools are declarative: you describe the desired end state and the tool works out the steps. The usual loop with Terraform is:

  1. Write configuration describing resources.
  2. Run terraform plan to compare the code with the real world and show the changes.
  3. Review the plan, ideally in a pull request.
  4. Run terraform apply to make the changes.

Terraform remembers what it manages in a state file, which maps each block of code to a real resource ID.

A realistic example

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = "eu-west-2"
}

resource "aws_s3_bucket" "artifacts" {
  bucket = "acme-release-artifacts"

  tags = {
    Team        = "platform"
    Environment = "prod"
  }
}

resource "aws_s3_bucket_versioning" "artifacts" {
  bucket = aws_s3_bucket.artifacts.id

  versioning_configuration {
    status = "Enabled"
  }
}
terraform init
terraform plan -out=tfplan
terraform apply tfplan

If someone later disables versioning in the console, the next terraform plan shows the drift and offers to put it back.

Provisioning vs configuration

IaC tools broadly split into two groups:

  • Provisioning tools create cloud resources: Terraform and OpenTofu, CloudFormation, Bicep, Pulumi.
  • Configuration tools set up software on servers that already exist: Ansible, Chef, Puppet.

Many teams use both. In container-based systems, Kubernetes manifests and Helm charts are also a form of IaC, and GitOps extends the idea by having a controller continuously apply what is in Git.

Common mistakes

  • Mixing console changes and code. Every manual change creates drift and surprises the next person who runs plan. Make the code the only way changes happen.
  • One giant configuration. A single state for everything makes plans slow and every change risky. Split by environment and by component.
  • Hardcoded values everywhere. Use variables and modules so environments differ only where they should.
  • Secrets in code. Passwords in .tf files end up in Git and in state. Pull them from a secrets manager.
  • Applying without reading the plan. The plan is the safety check. A line saying "must be replaced" on a database deserves attention.

Infrastructure as code in day-to-day work

DevOps and platform engineers write and review Terraform modules, run plans automatically on pull requests, manage remote state, import existing resources into code and build reusable modules other teams can consume. The Terraform for beginners post walks through a first project, the DevOps tools guide shows where IaC fits in the stack, and the CloudPros curriculum covers it across several weeks of labs.

Reference

Read the primary documentation for the underlying concept and further detail.

Frequently asked questions

What is infrastructure as code?

Infrastructure as code (IaC) is the practice of defining and managing infrastructure, such as networks, virtual machines, databases and Kubernetes clusters, in machine-readable files stored in version control. A tool reads those files and creates or updates the real resources to match, instead of engineers clicking through a console.

What are the most common infrastructure as code tools?

Terraform and its open-source fork OpenTofu are the most widely used multi-cloud provisioning tools. Cloud-specific options include AWS CloudFormation and CDK, Azure Bicep and Google Cloud Deployment Manager. Pulumi lets you write IaC in general-purpose languages. Ansible, Chef and Puppet focus on configuring software on servers.

What is the difference between declarative and imperative IaC?

Declarative IaC describes the end state you want, for example three subnets and one database, and the tool works out the steps. Terraform, CloudFormation and Kubernetes manifests are declarative. Imperative IaC lists the steps to run in order, like a script. Declarative tools make drift detection and repeatable changes much easier.

Is Ansible infrastructure as code?

Yes, in the broad sense: Ansible playbooks are code that define how servers should be configured. In practice, teams usually use Terraform to provision cloud resources and Ansible to configure the software on them once they exist.