Infrastructure as Code

Infrastructure as Code: Concepts Before Tools

By StackSpiral Editorial Team · · 5 min read

Infrastructure as code, often shortened to IaC, means describing servers, networks, databases and other infrastructure in text files that a tool can read and apply, instead of clicking through consoles or typing commands by hand. The files live in version control, get reviewed like application code and can be run again whenever you need to build or change an environment.

Many people start by comparing tools. That is understandable, but the tools change while the underlying concepts stay stable. If you understand the ideas first, choosing and learning any tool becomes far easier. This article walks through those ideas in plain language.

The Problem It Solves

Picture an environment built by hand over two years. Settings were changed during emergencies, nobody wrote them down, and the one person who knew how it fit together has left. Rebuilding it after a failure would be guesswork. Creating an identical test copy is nearly impossible.

Infrastructure as code answers these problems:

  • Repeatability: the same definition produces the same result each time.
  • Documentation: the code itself describes what exists and why.
  • History: version control records who changed what and when.
  • Review: changes can be examined by others before they take effect.
  • Speed: environments can be created or recreated quickly.
  • Consistency: staging and production can be built from the same source.

Declarative and Imperative Styles

The most important distinction is between two ways of expressing intent.

An imperative approach lists the steps to perform: create a server, then install a package, then open a port. You are describing how to get there.

A declarative approach describes the desired end state: there should be two servers of this size, with this firewall rule. The tool works out which steps are needed to reach that state from whatever currently exists.

Declarative definitions are easier to reason about and re-run, because you describe what, not how. Imperative scripts give fine control but can be fragile if run twice or from an unexpected starting point. Many real tools mix both, and the choice matters less than understanding which style you are writing.

Idempotence

A key property is idempotence: applying the same definition repeatedly produces the same outcome, without causing duplicates or errors. If the server already exists with the right settings, running the code again changes nothing.

This matters because you will run your code many times, perhaps from an automated pipeline. Without idempotence, every run is a risk. When you write or choose automation, ask what happens if it runs twice.

State and Drift

Tools need to know what they have already created. Some keep a state record, a file or service that maps your code to real resources. Others query the live environment each time. State allows the tool to calculate what should change, but it also becomes something you must protect. It may contain sensitive values, and if it is lost or corrupted, the tool can lose track of what it manages. Store it in a secure, shared and backed-up location, with locking to stop two people changing things at once.

Drift is what happens when real infrastructure no longer matches the code, often because someone made a manual change. Drift undermines the whole idea. Detect it by running a comparison regularly, and decide on a policy: either bring the code up to date with a valid change or revert the manual edit. The best prevention is a team rule that nobody changes managed resources by hand.

Provisioning and Configuration

Two related jobs are often confused:

  • Provisioning creates the infrastructure itself: networks, virtual machines, storage, load balancers.
  • Configuration management sets up software and settings inside those machines: packages, users, services.

Some tools do both, but they usually have a main focus. Another approach is to build immutable images that already contain the needed software, then replace servers instead of changing them in place. Immutability reduces drift, since a running machine is never modified after creation.

Structure and Reuse

As definitions grow, copy and paste becomes a burden. Use modules or similar building blocks to package a repeatable pattern, such as a network or a web service, with inputs for the parts that vary. Keep modules small, give them clear interfaces and document what each input means.

Use variables and separate files for environment differences, so that development, staging and production share the same structure but differ in size or names.

Secrets and Safety

Never commit passwords, keys or tokens to the repository. Use a dedicated secrets manager or encrypted variables, and pass values to the tool at run time. Also limit the permissions of the account the automation uses. It should be able to change only what it needs, since a leaked or misused credential could affect everything.

The Workflow

A healthy workflow resembles normal software development:

  1. Change the definition on a branch.
  2. Run a plan or preview step that shows exactly what would be created, changed or destroyed.
  3. Have a teammate review both the code and the preview.
  4. Apply the change through an automated pipeline, not from a personal laptop.
  5. Verify the result and record the change in version control.

The preview step is especially valuable. Pay close attention to anything marked for deletion, because some changes force a resource to be destroyed and recreated, which can mean downtime or data loss.

Choosing a Tool Later

With the concepts clear, compare tools by practical questions: which clouds and services you use, whether your team prefers a declarative language or a general-purpose one, and how state is handled. Learn on a small, low-risk project first.

Frequently Asked Questions

Is infrastructure as code only for large teams or cloud platforms?

No. Even a small team with a few servers benefits from having a rebuildable, documented setup. The effort scales with your needs, and a simple script or short definition in version control already gives you history and repeatability. Cloud environments make it especially natural, but it also works on traditional servers.

What is the difference between declarative and imperative approaches?

Declarative code states the desired end result and lets the tool decide the steps. Imperative code lists the steps explicitly. Declarative styles are usually easier to repeat and review, while imperative scripts give more direct control but need more care to make them safe to run again.

What happens if someone changes a resource manually?

The environment drifts from the code. The next time the tool runs, it may overwrite the manual change or flag a difference, depending on the tool and settings. To avoid surprises, set a policy against manual edits, detect drift regularly and fix the code or the resource so they match again.

Related reading