β Back to Infrastructure as code (Terraform)
πͺ΄ Infrastructure as code (Terraform): cheat sheet
π¨ The analogyA garden plan and a gardener who lists what to plant, change or pull up, then digs once you agree.
π The regular explanationDescribing servers, networks and storage in versioned files that a tool turns into real infrastructure. Terraform reads declarative HCL, compares it with its state file, shows a plan of what it will create, change or destroy, and applies it in dependency order.
- First the old way. Someone shouts a job at a helper, "build a shed", and the helper builds one. Nobody writes down what the garden should look like.
- Shout the same job again and it goes wrong. There is already a shed, so the helper can't build it, or builds a second one.
- The better way, in a fresh garden. You draw the garden plan, a shed and an apple tree, and hand it to the gardener, who makes both.
- Hand over the same plan again and the gardener walks round, finds everything already matches, and puts the spade down. Nothing to do.
- Before any work, the gardener fetches the right toolkit for this kind of garden from the tool shop.
- The gardener compares your drawing with the garden and writes a to-do list, called the plan. Build a shed, plant an apple tree. Nothing is dug yet.
- You read the list. It is what you wanted, so you say "go ahead".
- Only now does the gardener dig. The shed goes up and the apple tree goes in, just as the list said.
- One day you want the whole garden cleared. The gardener takes out everything on the plan, the tree first, then the shed.
- The garden is empty again, and the gardener's notebook has nothing left in it.
- Ana starts work. She hangs the in-use sign on the shed door, then opens the shared garden notebook to see what is already planted.
- Ben arrives at the same time and sees the sign. He doesn't touch the notebook; he turns back and will try again once the sign comes down.
- Ana plants the apple tree and writes it in the notebook straight away.
- Finished, Ana takes the sign down.
- Ben comes back, hangs up his own sign and reads the notebook, which already includes Ana's tree. Nobody plants it twice.
- The gardener reads the plan and notes what needs what. The bed and the hedge sit inside the fence, and the apple tree needs both. The shed needs nothing.
- The fence and the shed don't depend on anything or each other, so two pairs of hands do them at the same time.
- With the fence up, the flower bed and the hedge can both start, side by side.
- Last, the apple tree goes into the flower bed, behind the hedge.
- Clearing the garden runs the same order backwards. The apple tree comes out before the bed and the hedge.
- The plan says a small apple tree. A helper stops pruning it by hand and lets it grow big, without touching the plan or the notebook. The old shed in the corner was never on the plan at all.
- Next time, the gardener walks round with the plan and the notebook and spots that the tree is too big.
- The to-do list now says "prune the apple tree back to small". The big tree was a mistake, so you say go ahead, and the tree matches the plan again.
- The old shed was there before any plan. You draw it on the plan and the gardener writes it in the notebook, without knocking it down.
- You pin your new drawing on the noticeboard. The gardener sees it and checks it against the notebook.
- The gardener pins the to-do list under your drawing, so everyone can see exactly what would change.
- A neighbour reads the drawing and the list, and agrees. Two people have now checked every change.
- Only now does the gardener dig, and then writes the work in the notebook.
- The notebook also holds the shed's padlock code in plain words. So it stays locked away, and only a few people may open it.
π§© Key questions
- You hand the gardener the same garden plan twice in a row, and nothing in the garden has changed. What happens the second time?
- The gardener walks round, finds everything matches, and does nothing. The plan describes the finished garden, not the jobs, so when the garden already matches there is nothing left to do.
- Gardener Ben wants to start work, but Gardener Ana's in-use sign is hanging on the shed door. What should happen?
- Ben turns back and tries again once the sign comes down, so he sees her work in the notebook. One writer at a time keeps the notebook right, so nobody plants over someone else's work.
- The plan says a small apple tree, but a helper let it grow big by hand. What does the gardener's next walk round show?
- A job to prune the tree back to small, which you can approve or avoid by changing the plan to say big. The gardener checks the real garden too, so hand-made changes show up as jobs on the list.
- Your plan sets the shed padlock code but marks it as hidden. Where can the code still be read?
- In plain words in the garden notebook, so the notebook must be locked away. Hiding it only stops it being read out loud. The notebook still records everything about the garden.
- The plan says the apple tree goes in the flower bed, and the shed needs nothing. In what order does the gardener work?
- The flower bed and the shed at the same time, then the apple tree. The gardener reads who needs whom from the drawing. Jobs that need nothing start together, and the tree waits for its bed.