Sign in
← Infrastructure as Code
11 min · easy · Lesson 1 of 5

Infrastructure as Code: the idea

Why servers and networks get described as code in git: declarativity, reproducibility, review, and drift.

Why servers and networks get described as code in git: declarative state, reproducibility, review, and drift.

How servers used to be set up

The classic way to set up a server: SSH in and install packages by hand, write configs, open ports. Works fine until you have five servers, then ten. That's when the problems start:

Infrastructure as Code (IaC) is an approach where you don't configure infrastructure by hand, you describe it as text instead. The description lives in git, and a tool brings reality in line with it.

Declarative, not step-by-step

The core idea of IaC: describe the desired state, not a sequence of steps. You don't write "create a network, then a subnet, then a VM." You write "there should be a network, a subnet, and a VM with these parameters," and the tool figures out what to do to get there.

A fragment of a virtual machine description in Terraform for Yandex Cloud (the required disk and network blocks are left out; the full example comes in the next lesson):

resource "yandex_compute_instance" "web" {
  name = "web-01"

  resources {
    cores  = 2
    memory = 4
  }
}

This is the same approach as in Kubernetes manifests: you describe what should exist, and a controller brings the cluster in line with it. The tool compares the description with what already exists and does only the diff: it creates what's missing, changes what differs, and Terraform also removes what was dropped from the description. That's why you can apply the description as many times as you want. If reality already matches it, running it again changes nothing. This property is called idempotency.

What this gets you

Configuration drift

The main enemy of IaC: drift. Someone logged into a server by hand and changed something outside the code. Now reality has diverged from the description in git, and the description is lying.

That's why mature teams have a rule: no touching prod by hand, every change goes through code. If you fix something by hand in an emergency, you move that same fix into the code right away, otherwise the next apply will roll it back.

The IaC tool itself helps you spot drift. terraform plan re-reads the real objects in the cloud before building a plan and shows the difference between the code and reality before anything is applied. The command terraform plan -refresh-only shows only what changed from outside, without any changes from the code. But not every tool can do this: some compare the code not with the live system, but with whatever they themselves applied last time.

Where this fits in the DevOps stack

IaC is a description layer. Specific tools split up the responsibilities:

All these descriptions live in git. From there, CI applies them automatically, like any regular pipeline.

Common mistakes

What you'll need for the tasks

In the labs, you'll work through the same ideas with Docker Compose. A Compose file is a declarative description too: which services, from which images, with which ports and limits. The command docker compose -f <file> up -d brings the containers in line with it.

name: demo
services:
  web:
    image: nginx:1.27.3-alpine
    deploy:
      resources:
        limits:
          memory: 64M
          cpus: "0.25"

You'll use:

The next lesson walks through your first Terraform resource in practice.

Done reading?

Mark the lesson as read to move your track progress forward.

Go through it with an AI agent

The agent explains anything unclear in plain words, gives you 2 labs for this lesson and drills you on interview questions. $49 one-time, lifetime access.

Get access for $49

Tell the agent: use MCP izi, give me lesson iac-basics-01-iac-intro