Glenn Lum Software Engineering

2.7 Infrastructure as Code (IaC)

Infrastructure as Code is the practice of defining and managing your infrastructure (servers, networks, databases, load balancers, DNS records, storage systems) through machine-readable definition files that can be version-controlled, reviewed, tested, and automatically applied, rather than through manual clicks in a console or manual commands on a server. The motivating problem it solves is called configuration drift: over time, manually managed infrastructure tends to diverge from its intended state. An operator makes a one-time change to a firewall rule to fix an urgent incident and forgets to document it. A server gets a security patch applied that changes a library version. Two nominally identical servers end up subtly different from each other, which means that code that works on one doesn’t reliably work on the other, and debugging becomes a matter of archaeology rather than engineering.

The declarative versus imperative distinction is foundational to understanding how IaC tools work. An imperative approach says “do this, then do that, then do this other thing.” An imperative script might say “create a server, then install this software on it, then start this service.” The problem with this is that if you run it twice, you might end up with duplicate servers or conflicting software versions, because the script doesn’t know what state the system is already in. A declarative approach says “the world should look like this.” A declarative IaC definition says “there should be one server of this type, with this software installed, in this state.” If you apply it twice, the IaC tool compares the declared desired state to the current actual state and only makes the changes necessary to reconcile them. This idempotency (the property of producing the same result when applied multiple times) is what makes IaC safe to run repeatedly and what makes it the foundation of reproducible infrastructure.

The immutability principle goes further: rather than modifying existing infrastructure to bring it in line with the desired state, you destroy and replace it. If a server needs a new software version, you do not SSH into it and update the software. You build a new server image with the new software and replace the old server with a new one. This eliminates entire categories of problems: the “but what state was the old server in before the update?” question is irrelevant because the new server starts from a known, clean baseline. Containers have made this principle tractable for application deployments; IaC tools extend it to the infrastructure layer.

State management is where most IaC complexity and most IaC problems actually live, and it is the aspect most often glossed over in introductory treatments. An IaC tool needs to know what the infrastructure currently looks like in order to calculate what changes are needed to reach the desired state. This knowledge is stored in a state file, a representation of the infrastructure as the IaC tool understands it. The state file must be stored in a shared, centrally accessible location (not on a developer’s local machine, where it would be inaccessible to other team members and to your CI/CD pipeline) and it must be locked when an operation is in progress so that two concurrent applications of infrastructure changes don’t produce a corrupted or inconsistent state. The state file often contains sensitive information (database passwords, access keys) that was created as part of provisioning, which means it must be stored securely with appropriate access controls. If the state file is lost or corrupted, your IaC tool’s ability to manage your infrastructure is compromised, potentially requiring manual reconciliation.

Modularization is the IaC equivalent of good software architecture. Just as you wouldn’t write all your application’s logic in a single function, you shouldn’t define all your infrastructure in a single IaC file. Modules encapsulate a reusable piece of infrastructure (a standard network setup, a standard database deployment pattern, a standard observability bundle) with well-defined inputs and outputs. Teams can compose modules to build their specific environments, and improvements to a module propagate to all environments that use it. This promotes consistency and reduces the cognitive overhead of managing complex infrastructure.

← Back to Home