Declarative infrastructure
Describe the end state, let the tool compute the change, and understand what "converge" really promises.
Two ways to say what you want
There are two broad ways to automate infrastructure. A procedural script lists steps: create a network, create three servers, attach them. Run it twice and you get six servers, unless you write all the checking yourself. A declarative configuration describes the end state: "there are three servers of this size on this network". A tool compares that description with reality and computes the steps.
Terraform: Up & Running opens with this comparison, together with two others that shape tool choice. Configuration management versus provisioning separates configuring software on existing hosts (Ansible, Puppet, Chef) from creating the hosts, networks, and services themselves (Terraform, OpenTofu, CloudFormation). Mutable versus immutable separates changing servers in place from replacing them with new ones built from an updated image. Most real platforms combine them, for example Terraform to provision, an image pipeline for immutable hosts, and Ansible where in-place configuration is still needed.
Plan, then apply
The declarative cycle has three steps:
- Refresh: read the real state of every managed resource through the provider's API.
- Plan: compare real state with the configuration and produce a list of actions. Symbols mark each one:
+create,~update in place,-/+destroy and re-create,-destroy. - Apply: execute those actions in dependency order.
The plan is the most important artifact in infrastructure as code. It is the change request a reviewer should read, more than the diff of the code, because small code changes can produce large plans. The “Reading plans and refactoring safely” notes are devoted to reading them.
Idempotence and convergence
A good declarative run is idempotent. Applying the same configuration again changes nothing, and plan reports "No changes". That property is what makes automation safe to re-run after a failure, on a schedule, or from CI. It is also a test. If a second plan immediately after an apply still shows changes, something is fighting the tool, such as a provider bug, a value computed differently each time, or another system modifying the resource.
Drift
When someone changes managed infrastructure by hand, reality drifts from the code. The next refresh notices, and the plan proposes changing it back. That is a decision point, not an automatic correction. If the manual change was an emergency fix that should stay, update the code. If it was a mistake, let the apply revert it. Running plans regularly, for example nightly in CI, turns drift from an occasional surprise into a routine report.
Providers and the graph
The core engine knows nothing about clouds. Providers implement create, read, update, and delete for each resource type against an API. The engine builds a dependency graph from references between resources (a server referencing a network's ID depends on that network), and that graph decides the order of operations and what can run in parallel. Understanding that references create dependencies explains most ordering questions, and the rare need for an explicit depends_on.
Key terms
- Desired state
- A description of how infrastructure should be, as opposed to the steps to make it so.
- Plan
- The computed difference between desired state and real state. It lists what will be created, changed, replaced, or destroyed.
- Idempotence
- Applying the same configuration twice has the same effect as applying it once. The second run changes nothing.
- Drift
- Real infrastructure that no longer matches the code, usually because someone changed it by hand.
Read further
- Terraform Up & Running, 3rd edition, Ch. 1, "Why Terraform" (Purchase)
The comparison of configuration management and provisioning tools, mutable and immutable infrastructure, procedural and declarative languages, and the argument for declarative provisioning. - Terraform Up & Running, 3rd edition, Ch. 2, "Getting Started with Terraform" (Purchase)
Providers, resources,planandapply, variables and outputs. Run the examples locally if you can, or read the plan output carefully. - UNIX and Linux System Administration Handbook, 5th edition, Ch. 23, "Configuration Management" (Purchase)
The general concepts (operations, variables, facts, change handlers) and the comparison of tools. It shows that these ideas predate any one product.