Infrastructure as Code Explained: Managing Cloud With Code

Infrastructure as code explained in plain terms is the practice of defining cloud resources, such as servers, networks and databases, in text files, rather than creating them by clicking through a console. The central question is why teams bother writing code for something a console can do with a few clicks, and the short answer is that code can be reviewed, versioned, tested and repeated, while clicks cannot, and that difference decides whether infrastructure is reliable or a mystery.

Every cloud system is made of resources that someone created, configured and connected. When those resources are created by hand, the only record of how they were made is the memory of the person who clicked. When that person leaves, or when the system needs to be rebuilt, the knowledge is gone. Infrastructure as code fixes that by making the definition of the system a file that anyone can read, change and rebuild from. Our guide to Terraform explains the most widely used tool for writing infrastructure as code.

Key takeaways

  • Infrastructure as code defines cloud resources in text files, instead of creating them by hand through a console.
  • Code can be reviewed, versioned, tested and repeated, while clicks cannot, which makes infrastructure reliable and rebuildable.
  • Declarative tools describe the desired end state; imperative tools describe the steps to reach it.
  • State files track what exists in the cloud, and they must be stored safely and shared carefully.
  • Good practice means small changes, peer review, testing in a staging environment, and never editing live resources by hand.

What infrastructure as code means

Infrastructure as code, usually shortened to IaC, treats the definition of infrastructure as a software artefact. A file describes the servers, networks, databases and permissions a system needs, and a tool reads that file and creates or updates the real resources to match. Change the file, run the tool, and the infrastructure changes. Delete the file’s contents, run the tool, and the infrastructure is removed.

The idea applies the habits of software development to infrastructure. Code lives in version control, so every change has an author, a date and a reason. Changes go through review, so a second person checks them before they apply. Code can be tested, so a change can be tried in a safe environment before it touches production. And code can be reused, so a pattern that works for one service can be shared with the next. None of that is possible when infrastructure is built by clicking.

Declarative and imperative approaches

Infrastructure as code comes in two styles. Declarative tools describe the desired end state: what resources should exist, and how they should be configured. The tool works out what needs to be created, changed or deleted to reach that state. Terraform is the best-known declarative tool, and it is used across many cloud providers. Imperative tools describe the steps: run this command, then that one, in this order. Scripts and configuration management tools often work this way.

Declarative tools are usually easier to reason about, because the file says what the system should look like, and the tool handles the details of getting there. They also make drift visible: if someone changes a resource by hand, the next run of the tool will report that reality no longer matches the file. Imperative tools give more control over the order of operations, which matters for complex sequences, but they leave it to the writer to handle every edge case, such as what to do when a step fails halfway through. Most teams start with a declarative tool and use scripts only where the tool cannot reach.

A photograph of a developer in a dark office, a monitor showing lines of infrastructure configuration code in green light
Infrastructure as code turns the definition of a system into a file anyone can read, review and rebuild from.

How a typical workflow runs

A typical IaC workflow starts with a change to a file: a new database, a larger server, a different network rule. The author opens a pull request, and a colleague reviews the change, just as they would review application code. An automated check then runs the tool in plan mode, which reports what would change without changing anything. That report is part of the review, so everyone can see the effect before it happens.

Once the change is approved, it is applied, usually by an automated pipeline rather than by a person running the tool on their own machine. The pipeline applies the change to a staging environment first, where tests check that the system still works, and only then to production. Every step is logged, and the version control history records exactly what changed, when, and by whom. If something goes wrong, the previous version of the file can be reapplied to roll back.

Comparing the main approaches

Approach Main benefit Main risk Best for
Declarative IaC Describes the end state clearly Less control over ordering Most cloud infrastructure
Imperative scripts Full control over steps Edge cases fall on the writer Complex sequences
Configuration management Keeps servers consistent Needs an agent on each server Long-lived servers
Container definitions Portable and repeatable Does not cover the cloud around them Application packaging
ClickOps console work Fast for one-off experiments Not reviewable or repeatable Learning and quick tests

State files and drift

Declarative tools keep a state file: a record of what resources exist, and how they map to the definitions in the code. The state file is how the tool knows what to create, what to change, and what to delete. It is also the most sensitive part of the setup, because it can contain secrets, and because a damaged or lost state file leaves the tool unable to manage the resources it created.

State must be stored safely, in shared storage that the whole team can reach, with locking so that two people cannot apply changes at the same time. It should never live only on one person’s laptop. Drift is the other half of the problem: when someone changes a resource by hand, through the console, reality stops matching the code. The next run of the tool will detect the difference, and the team must decide whether to update the code to match reality, or to reapply the code and undo the manual change. The discipline of IaC is that manual changes are treated as exceptions to be fixed, not as a normal way of working.

Testing and reviewing infrastructure changes

Infrastructure code can be tested, and it should be. The simplest test is the plan output, which shows what would change and is reviewed like any other change. Stronger testing applies the code to a staging environment that mirrors production, and runs checks against the result: are the right resources present, are the network rules correct, can the application reach its database. Some teams also run policy checks, automated rules that reject changes breaking an agreed standard, such as a storage bucket left open to the public.

Review matters as much as testing. A second pair of eyes catches mistakes the author missed, such as a resource in the wrong region, or a permission that is broader than intended. Review also spreads knowledge, so that more than one person understands how the system is built. The review habit is what turns IaC from a personal convenience into a team practice: the code is shared, the changes are visible, and nobody is the only person who knows how production works. Our guide to CI/CD pipelines explains the automation that carries infrastructure changes from review to production.

What good practice looks like

Good IaC practice starts with small changes. A change that creates one database is easy to review, easy to test, and easy to roll back. A change that rebuilds half the system is none of those things. Small changes, applied often, keep the system moving without drama. Good practice also means never editing live resources by hand. Once a resource is managed by code, the code is the only way to change it, and a manual fix that bypasses the code will be undone or forgotten.

Good practice also means organising code for reuse. Common patterns, such as a standard network or a standard database, belong in shared modules that every service uses, so that a fix in one place improves every service. And it means keeping secrets out of the code: passwords and keys belong in a secret manager, referenced by the code but never written in it. A repository that contains a real password is a security incident waiting to happen.

Infrastructure as code checklist

  • Define every production resource in code, and stop creating resources by hand.
  • Keep infrastructure code in version control, with every change reviewed by a second person.
  • Check the plan output before applying any change, and read what it would do.
  • Store state in shared, locked storage, never only on one person’s machine.
  • Apply changes through a pipeline, to staging first and production second.
  • Test infrastructure changes in a staging environment that mirrors production.
  • Keep secrets in a secret manager, never written into infrastructure code.
  • Treat manual changes as drift to be fixed, not as a normal way of working.
  • Reuse common patterns through shared modules, so a fix improves every service.

Frequently asked questions

What is infrastructure as code?

Infrastructure as code is the practice of defining cloud resources, such as servers, networks and databases, in text files, and using a tool to create and update the real resources from those files. It applies the habits of software development, such as version control and review, to infrastructure.

What is the difference between declarative and imperative IaC?

Declarative tools describe the desired end state, and the tool works out how to reach it. Imperative tools describe the steps to take, in order. Declarative tools are usually easier to reason about, while imperative tools give more control over ordering.

What is a state file?

A state file is the record a declarative tool keeps of what resources exist and how they map to the code. It is how the tool knows what to create, change or delete. It must be stored in shared, locked storage, because a lost or damaged state file leaves the tool unable to manage its resources.

What is drift in infrastructure as code?

Drift is the difference between what the code says should exist and what actually exists, usually because someone changed a resource by hand through the console. The next run of the tool detects the difference, and the team must either update the code or reapply it to undo the manual change.

Can infrastructure code be tested?

Yes. The simplest test is the plan output, which shows what would change. Stronger testing applies the code to a staging environment and checks the result, and policy checks can automatically reject changes that break agreed standards, such as a storage bucket left open to the public.

Is infrastructure as code worth it for small teams?

Yes, and arguably more so, because small teams cannot afford to lose the only person who knows how production was built. Code is a record that survives people leaving, and it lets a small team rebuild a system from scratch with confidence.

The bottom line

Infrastructure as code explained in one line is this: when the definition of a system is a file, it can be reviewed, versioned, tested and rebuilt, and that is what turns infrastructure from a mystery held in one person’s memory into a reliable system the whole team can understand and trust.

Latest articles

Scroll to Top