Declarative, imperative, mutable, immutable
Declarative code describes the end state and lets the tool work out the steps, and immutable servers are replaced from a new image instead of patched in place.
Diagrams in this video · 2Free in archhy
The diagrams, full size
Copy any of them into archhy, free, and change it for your own system.
After this lesson you can
- Tell imperative steps from a declarative description of the end state
- Explain why running declarative code twice changes nothing the second time
- Contrast mutable servers patched in place with immutable servers replaced from an image
- Say where Terraform and Packer fit in the immutable approach
Transcript
Declarative, imperative, mutable, immutable
Space Llama here. Last time, we swapped clicking for code. Today, two pairs of big words. Declarative and imperative. Mutable and immutable. They sound like an exam. They're really about dinner, and about pets.
Imperative: list the steps
Imperative means you tell the computer exactly what to do, step by step. Create a network. Start a server. Open a port. In that order. A script like that works the first time. Run it again, and it may try to create everything twice. So you add checks. Does it exist already? Is it the right size? The script grows, and grows.
Declarative: describe the end state
Declarative is the other way round. Think of a recipe, versus a photo of the finished dish. A recipe is imperative. Boil the water, add the pasta, wait ten minutes. A declarative file is the photo. This is dinner. You work out the steps. Terraform configuration files are declarative. They describe the end state of your infrastructure. Run it twice, and the second time nothing happens. It already matches. Very relaxing.
Why declarative suits infrastructure
Why does this matter for infrastructure? Because the file doubles as a description. Read it, and you know what should exist. A reviewer sees the result you're asking for, not a pile of steps to trace in their head. And because you can run it again safely, the tool can compare what's there with what you asked for, and show the difference. That comparison is called a plan. You'll meet it properly in part two.
Mutable: change it in place
Now the second pair. Mutable means changeable. You keep a server, and change it in place. Log in, install a patch, tweak a setting. Do that for a few years. People call these servers pets. They have names, you nurse them when they're sick, and nobody dares reboot them. As a llama, I have mixed feelings about this comparison.
Patch it, or replace it
Here are both ways on one picture. On the left, server zero one. Running since 2019. Patched, hot-fixed by hand, and labelled do not reboot. On the right, the immutable way. You never change a running server. Instead, you build a new image, version two, and launch fresh servers from it. They're identical. The old server, version one, is simply replaced. People call these cattle. No names, no nursing. If one is unwell, you replace it from the same image. Packer builds those images. Terraform launches the servers from them. That's this course, in one picture.
Mutable vs immutable
Mutable servers carry their whole history with them. Every patch, every late-night fix. Immutable servers carry nothing. Each one starts from a known image, so you know exactly what's on it. And if the new image misbehaves, you roll back by launching from the previous one.
Recap
Let's recap. Imperative code lists the steps. Declarative code describes the end state, and the tool works out the steps. Mutable servers are changed in place. Immutable servers are replaced from a new image. Terraform is declarative, and with Packer images, it helps you work immutably.
Practice and what's next
That's four big words, tamed. Next lesson, The tool family. Terraform, Packer, OpenTofu, and a quick word about licenses. See you there.
Gazeek Flash · free
Exam questions on your phone
Free questions on Why infrastructure as code: quick-fire batches and timed exams, on the web or Android.






