Infrastructure as Code

Treat servers, networks and databases like source code. Write them, review them, version them — and let a pipeline apply them.

Apply One file becomes real cloud resources

A new engineer asks: "How was the production database set up?"

The team leader thinks. There’s a wiki page, but it’s from 2021. There’s a shell script someone wrote, but it doesn’t match what’s actually running. The truth lives in 27 screens of the cloud console, and three of them have a "do not change" sticky note on the monitor.

When the same team needs a second environment for a customer in another region, nobody really knows where to start. They re-click 27 screens. They forget one of them, in a way they only discover at 2 a.m. three weeks later.

Infrastructure as Code (IaC) makes this story impossible. Every server, every firewall rule, every DNS entry lives in a text file. The text file is the source of truth. Click-ops becomes a code smell.

01What is Infrastructure as Code?

At its core, IaC is the idea that infrastructure should be created the same way application code is — written in a file, reviewed in a pull request, applied by an automated pipeline.

The simple version

Infrastructure as Code means describing your servers, networks and databases in plain text files instead of clicking around in a console. The files live in Git, so every change is reviewed, versioned and reversible.

The declarative version

You write down the end state you want — "an S3 bucket called X with encryption on" — and a tool figures out the steps to make reality match. Run it twice and it does nothing the second time. That’s called idempotency.

The operational version

IaC turns infrastructure changes into ordinary software changes. The same review, same CI, same audit trail, same rollback story you already use for application code — applied to the things that run the application.

02Why teams adopt it

IaC sounds like extra work at first — "why write a file when I can just click a button?" Three forces tip the balance, and they only get stronger as the team grows.

Reason 01

Reviewable change

A console click leaves no record. A pull request leaves a complete one: who, what, when, why, who reviewed it. When something goes wrong on Friday, the team finds the cause in 60 seconds — not 60 minutes.

  • Git log = audit log. Compliance teams love it.
  • Reverts work. Roll the file back, re-apply.
Reason 02

Identical environments

Dev, staging and prod come from the same code, with a different variables file. The "works in staging, breaks in prod" excuse stops being valid — because the two environments are now literally the same shape.

  • One module, many envs. Stamp it out per stage.
  • Disaster recovery. A new region is one command away.
Reason 03

Speed at scale

One environment is fine to click. Twenty environments — per developer, per branch, per ephemeral test — is not. IaC lets you spin up a complete environment in minutes and tear it down when you’re done.

  • Ephemeral PR envs. Every pull request gets its own.
  • Cost goes down. No "we kept it around just in case".
Reason 04

Knowledge survives people

When the senior who set up the cluster leaves the company, the cluster doesn’t become a mystery. The code is the documentation. New hires read it, understand it, change it — the same way they would any other codebase.

  • No tribal knowledge. If it’s not in the repo, it doesn’t exist.
  • Onboarding gets faster. Read the code, understand the system.

03The two halves of IaC

"Infrastructure as Code" actually covers two related but different jobs. Knowing the difference helps you pick the right tool for the right task — and stops you from building a screwdriver out of a hammer.

Provisioning — "create the box"

Terraform / OpenTofu / Pulumi / Crossplane

Provisioning tools talk directly to cloud APIs. They create the virtual machines, networks, databases, queues, IAM roles — the empty boxes your code will run in.

  • Declarative. You describe what you want, not the steps.
  • Cloud-aware. Speaks AWS, Azure, GCP, Cloudflare, Datadog …
  • State-driven. Remembers what it created, so it knows what to change next time.
Configuration management — "paint the box"

Ansible / Chef / Puppet / Salt

Configuration management tools install packages, drop config files, start services and tweak kernel parameters on an existing machine.

  • Idempotent steps. "Make sure nginx is installed" — safe to run repeatedly.
  • Agentless (Ansible) or agent-based (Chef/Puppet).
  • Shrinking footprint. Containers absorb most of this work today.

04What it looks like in real life

Here is a complete Terraform file that creates a VPC and an EC2 instance in AWS. Twenty lines. Run terraform apply and a network and a server appear, repeatably, with a state file remembering what it owns.

main.tfHCL
terraform {
  required_version = ">= 1.7.0"
  required_providers {
    aws = { source = "hashicorp/aws", version = "~> 5.0" }
  }
  backend "s3" {
    bucket = "course-tfstate"
    key    = "envs/dev/terraform.tfstate"
    region = "eu-west-1"
  }
}

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

resource "aws_vpc" "main" {
  cidr_block = "10.10.0.0/16"
  tags       = { Name = "course-dev" }
}

resource "aws_instance" "web" {
  ami           = "ami-0abcd1234567890ab"
  instance_type = "t3.micro"
  tags          = { Name = "course-web" }
}

output "public_ip" {
  value = aws_instance.web.public_ip
}

That’s the whole thing. Notice what is not there: no installation steps, no retry logic, no "if exists" checks. You declare the end state. Terraform figures out what to create, update or delete to match.

05The day-to-day workflow

A team using IaC well follows the same loop every day. It looks almost identical to the loop they use for application code — and that is exactly the point.

Edit a file

You change a .tf file in your branch.

Open a PR

The bot runs terraform plan and posts the diff as a comment.

Review

Humans read the plan. CODEOWNERS gate the sensitive paths.

Merge & apply

The pipeline runs terraform apply with the approved plan.

Audit

Plan, apply log, and state version land in the audit store.

06Anti-patterns — "we’re using IaC, but…"

These are the small habits that turn IaC from a force multiplier into a frustration. Spot them early.

Anti-pattern
Click-ops on the side. Someone changes a setting in the console "just this once". The state file no longer matches reality. Run a nightly terraform plan and alert on drift — the only way to keep everyone honest.
Anti-pattern
The state file in Git. The state file contains secrets and resource IDs. It also needs locking, which Git doesn’t do. Always use a remote backend (S3 + DynamoDB lock, GCS, Terraform Cloud, Azure Storage).
Anti-pattern
One giant root module. A 4,000-line Terraform run that takes 40 minutes to plan. Split by lifecycle: networking changes slowly, applications change daily. Keep them in separate states.
Anti-pattern
Workspaces as environments. Workspaces share a backend and configuration. Use them for ephemeral experiments. For long-lived dev / stage / prod, use separate directories or repos.
Healthy
Plan-in-PR, apply-on-merge. The plan is the review artifact. Humans see it, approve it, and the pipeline applies exactly that plan. No surprises.

07Putting it together

Take-aways

1. IaC means describing infrastructure in text, versioning it in Git, and applying it with a pipeline — the same way you handle application code.

2. It splits into provisioning (Terraform & friends, for creating boxes) and configuration management (Ansible & friends, for painting the boxes). Containers absorb a lot of the second job.

3. Work in small PRs. Plan in the PR, apply on merge. Never edit the state by hand if you can help it.

4. The console becomes read-only. The repo is the truth. Drift is the alert.

Next chapter: the artifact your pipeline ships and your IaC runs — a container.