Infrastructure as code: declarative vs imperative
Why clicking servers into existence fails at scale, what writing infrastructure as code buys you, and why declaring the end state beats scripting the steps.
The Linux server from an operator's seatWhy orchestration exists
Infrastructure as code: declarative vs imperative
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Infrastructure built by hand in a console cannot be reviewed, repeated or audited, and nobody can say exactly how it differs from last month.
- Infrastructure as code keeps the definition of servers, networks and permissions in files under version control, applied by a tool, so changes go through review like application code.
- An imperative approach lists the steps to take. A declarative one describes the end state and lets the tool work out the steps by comparing it with what exists.
- Declarative tools are idempotent: applying the same definition twice changes nothing the second time, and applying it after someone changed things by hand repairs the drift.
- The two styles mix in practice. Pulumi uses general-purpose languages to build a declarative model, and Ansible runs tasks in order but each task declares a state.
The environment nobody can rebuild
The production environment was built over two years by four people clicking in a cloud console. There is a network, some subnets, a load balancer, a database, a dozen firewall rules, and an IAM role with a policy someone edited during an incident. It works. Then you need a staging environment that matches it, or the region has an outage and you need production somewhere else. Nobody can say exactly what is in it, let alone reproduce it.
This is ClickOps, and its problems are the ones version control solved for code: no history, no review, no way to rebuild from scratch, and no way to see what changed since last Tuesday.
Infrastructure as code
Infrastructure as code (IaC) means the definition of your infrastructure lives in text files in a Git repository, and a tool applies those files to the real world. Servers, networks, DNS records, firewall rules, Kubernetes objects and permissions all become code. That buys four things at once:
- Review. A change is a pull request. Someone reads
size = "large"before it costs money. - History.
git logshows who opened port 5432 to the internet, when, and in which commit message they explained why. - Repeatability. Staging and production come from the same files with different inputs.
- Recovery. If a region is lost, the files rebuild it elsewhere.
MLOps’ IaC lesson makes the same case for machine learning platforms. The question for an operator is how the files describe the infrastructure, and there are two answers.
Imperative: the steps
An imperative definition is a list of actions: create a network, then create two servers in it, then open port 443. Shell scripts that call a cloud CLI work this way. They are easy to write and they read like a runbook. The trouble starts the second time you run them.
Declarative: the end state
A declarative definition describes what should exist: two web servers of size small, in this network, with port 443 open. A tool compares that with what actually exists and works out the actions itself. This is the same declare-and-reconcile loop from why orchestration exists, applied to infrastructure.
Think of directions versus an address. “Turn left, go two blocks, turn right” only works from one starting point. An address works from anywhere, because the route is computed from where you are.
Both, side by side
To see the difference run, I made a fake cloud: a JSON file of servers, with create, delete and resize functions. Then two scripts that use it. The imperative one creates two web servers. The declarative one takes a desired count and size, compares with what exists, prints a plan, and applies it. Both are short Python, and both ran on this Mac:
$ python3 imperative.py # run 1
servers now: ['web-1', 'web-2']
$ python3 imperative.py # run 2, same script
servers now: ['web-1', 'web-2', 'web-3', 'web-4']
Run it twice, get four servers. The script did exactly what it said, which was the problem. Now the declarative one, from an empty cloud:
$ python3 declarative.py 2 small
+ web-1 -> small
+ web-2 -> small
servers now: ['web-1', 'web-2']
$ python3 declarative.py 2 small
No changes. Actual matches desired.
$ python3 declarative.py 3 medium
+ web-3 -> medium
~ web-1 small -> medium
~ web-2 small -> medium
servers now: ['web-1', 'web-2', 'web-3']
The second run did nothing, because nothing differed. That property is idempotence: applying the same definition any number of times gives the same result. Changing the definition produced a plan of exactly the needed changes, one create and two resizes. Then I deleted web-2 by hand, behind the tool’s back, the way an engineer might during an incident:
$ python3 declarative.py 3 medium
+ web-2 -> medium
servers now: ['web-1', 'web-2', 'web-3']
$ python3 declarative.py 1 medium
- web-3 medium -> (gone)
- web-2 medium -> (gone)
servers now: ['web-1']
The tool noticed the drift and put it back. Lowering the count deleted the extras. My toy reads the real state directly from the JSON file. Real tools like Terraform also keep their own record of what they created, which is the subject of the next lesson.
The tools, and where they sit
| Tool | Style |
|---|---|
| Terraform, OpenTofu | declarative HCL files, plan then apply |
| AWS CloudFormation, Azure Bicep | declarative, one cloud each |
| Kubernetes manifests | declarative objects, reconciled continuously |
| Pulumi, AWS CDK | general-purpose languages that build a declarative model |
| Ansible | ordered tasks, but each task declares a state |
The line blurs. Pulumi lets you write a for loop in TypeScript, and the result is still a desired state that the engine diffs. An Ansible task says “this package should be installed”, not “run apt-get install”, so it is idempotent too. What matters is whether running it twice is safe and whether it can show you what it will change before it does.
About the video
“What is Infrastructure as Code?” is from IBM Technology, presented by Sai Vennam on a lightboard, published August 2019, 8 minutes 51 seconds. The description says it covers what IaC is, imperative versus declarative approaches, and mutable versus immutable infrastructure. The concepts have not changed since 2019, though tool names mentioned in passing may be dated. There are no chapters. English was judged from the title, description, channel and its English caption tracks. PulumiTV’s four-minute video in deeper gives the declarative-versus-imperative distinction from a tool vendor’s side.
Where this fits
Next, the most widely used declarative IaC tool and the file that makes it work: Terraform and OpenTofu: plan, apply and state. Mutable versus immutable gets its own lesson later, in immutable infrastructure and golden images.
Check yourself
A teammate runs the setup script a second time by mistake and you now have four servers instead of two. What property was the script missing?
Idempotence. The script describes actions, create two servers, not an end state, so every run repeats the actions regardless of what already exists.
What does a declarative tool need to know to work out what to do?
The desired state from your files and the current state of the real infrastructure. The difference between them becomes the plan.
Someone deletes a server by hand. What happens the next time a declarative tool applies the unchanged definition?
It sees the server is missing compared with the definition and recreates it. Manual drift is reverted to what the code says.
Why is reviewing an infrastructure change in a pull request better than reviewing it in a ticket?
The reviewer sees the exact change to the definition, the history shows who changed what and why, and the same file can recreate the environment later.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.