Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Introduction to Terraform  ›  Lesson

What Is Infrastructure as Code?

What is Infrastructure as Code? 12 minInfrastructure as code means describing servers, networks and storage in version-controlled text files that a tool reads to build them, instead of clicking through a console.

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.

Turning Crumb's hand-built server into codeStep 1 of 4
  1. 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.

quick drill

Which of these is infrastructure as code?

quick drill

A file says "there is one server with port 80 open". What style is it?

quick drill

You run a declarative configuration twice with no edits in between. What should happen the second time?

quick drill

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.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhy not just click?

Crumb's first server was built by hand in a cloud console. Explain to the owner why describing it as code instead would help, and whether the code should say what to do step by step or what should exist.