What Is Infrastructure as Code?
Crumb, a small bakery, has a website on one cloud server. A contractor built it by clicking through a web console two years ago, and nobody remembers which boxes were ticked. Now they want an identical copy for testing. Infrastructure as code exists so that question never needs a memory to answer.
The rule
Write down what should exist; let a tool make it so.
Infrastructure means the pieces an application runs on: servers, networks, storage buckets, DNS records, databases. Infrastructure as code (IaC) means describing those pieces in plain text files, keeping the files in version control such as Git, and letting a tool read the files and build or change the real thing.
There are two ways to write such files:
- Imperative — a list of steps: "create a server, then open port 80, then attach a disk". Run it twice and you may get two servers.
- Declarative — a description of the end state: "there is one server with port 80 open and a 20 GB disk". The tool compares that with what already exists and works out the steps itself. Run it twice and nothing extra happens.
Terraform is declarative. Here is the flavour, in the language you will learn soon:
resource "aws_instance" "web" {
ami = "ami-0123456789abcdef0"
instance_type = "t3.micro"
}That says "one small server called web should exist". It does not say how to make it.
Because it is text, everything you already do with code now applies to infrastructure: history, review, diffs, rollbacks and copying.
The shortcut
When you meet any infrastructure task, ask "Is this written down somewhere a tool can read?" If the only record is a person's memory or a screenshot, it is not IaC yet.
Find what exists
List the pieces: one server, one storage bucket for cake photos, one firewall rule for web traffic.
The common trap is thinking that a shell script full of cloud commands is the same thing. It is code, but it is imperative: it describes actions, not a state, so running it again does not safely "check and fix".
Drills
Answer fast — trust the rule.
Which of these is infrastructure as code?
A file says "there is one server with port 80 open". What style is it?
You run a declarative configuration twice with no edits in between. What should happen the second time?
Which benefit comes directly from keeping infrastructure files in version control?
Remember
- IaC: describe infrastructure in text files in version control; a tool builds it.
- Shortcut: if the only record is someone's memory, it is not IaC.
- Trap: a script of commands is imperative; declarative files describe the end state and can be safely re-run.
