A tool reads declarative definition files, computes a plan against the current state, and applies the minimum set of changes needed to close the gap.
The mechanism rests on three phases: read the definitions, diff them against reality, apply the difference. The tool keeps a record of what it believes the world looks like (the state) and compares it to what the definitions say it should look like. The difference is presented as a plan. The engineer reviews the plan, approves it, and the tool executes it.
graph LR
D[Definition files] --> P[Plan]
S[Current state] --> P
P --> R{Review}
R -->|approve| A[Apply]
R -->|reject| D
A --> NS[New state]
NS --> S
The declarative model is what distinguishes IaC from classic provisioning scripts. A script says "run these commands in this order". A declarative file says "the world should look like this". The tool figures out the order. If you re-run a script, it may create duplicate resources or fail. If you re-apply a declarative definition, it does nothing because the world already matches. That property is called idempotency, and it is what makes IaC safe to run repeatedly.
Adopting IaC in an existing project
Start with a single resource
Import one existing resource into IaC. Configure the tool, describe the resource, run import, then verify no changes appear in the next plan.
Version the state
Store the state file in a remote backend with locking. Never commit it to Git alongside the definitions.
Pipeline the apply
Move the plan and apply steps into CI. Require a reviewer for any plan that destroys or replaces a resource.
Layer the estate
Split definitions into layers: network, compute, data, IAM. Apply layers in order; keep each layer's blast radius small.
Most tools maintain a state file that maps definitions to real resources. Terraform keeps terraform.tfstate. Pulumi keeps a stack state. CloudFormation keeps a stack. The state file is the tool's memory: without it, the tool cannot know what exists and what needs to change. Losing the state file is expensive because you have to import every existing resource manually.