Infrastructure as code
Three pictures of infrastructure: configuration in Git, the tool's state and reality via the API; plan as a diff; apply; idempotency; modules and environments; secrets kept out of the repository.
Three pictures
Clicking a cloud console does not scale, does not review, and does not reproduce last Thursday's network. Infrastructure as code keeps three pictures in sync: configuration (files in Git saying what should exist), state (the tool's record of which real cloud object maps to which config node), and reality (whatever the provider API returns right now). A plan diffs configuration against reality through state; apply executes the plan and rewrites state. Unchanged config against matching reality → empty plan (idempotent). That empty plan is the green build of infrastructure.
Lose state without backup and the tool may try to create duplicates or refuse to manage orphans — treat state backends (S3 + lock table, Terraform Cloud, Pulumi backend) as production data: versioning, encryption, restricted IAM, lock timeouts that humans know how to clear.
Key idea. Infrastructure changes become reviewed commits with a machine-readable diff of creates/changes/destroys — the same change discipline as application code, aimed at blast radius measured in subnets and databases.
Plan before apply
Every change is a reviewed commit. CI runs plan on the pull request and posts the diff; a human reads creates, changes and especially destroys before apply. Auto-apply after merge to a protected branch is optional and needs strong review rules, required checks and often an environment approval for production. Console edits cause drift: reality moves while configuration does not. Refresh/state reconcile and policy-as-code (OPA, Sentinel, cloud SCPs) stop silent divergence — or at least detect it before the next apply surprises you with a destroy.
Worked habit. A PR that replaces an instance group shows - resource "…" "old" and + resource "…" "new" or an in-place update. If the plan says it will recreate a database, stop — that is a data event, not a rename. Require two reviewers for destroys touching stateful resources; no amount of green unit tests saves a dropped RDS.
Drift detectors that run nightly plan against production and open tickets when the diff is non-empty catch the "quick fix in the console" within 24 hours instead of at the next intentional change.
Modules and environments
Modules package repeated patterns: a VPC with public/private subnets per AZ, a service with instance role + load balancer + alarms, a database with backups and parameter groups. Version modules; pin module versions in each root like you pin container digests — floating main of a shared module is how one team’s "cleanup" recreates another team’s network.
Each environment (dev, staging, prod) is a root stack with its own state backend and usually its own cloud account or project. Shared state across prod and dev is a blast-radius bug: a bad apply can touch both. Secrets never sit cleartext in Git — reference a secret manager by ARN/name; CI assumes short-lived OIDC roles that can read only what that environment needs. Parameterise region, instance sizes and replica counts; do not fork entire trees for "prod is like staging but bigger" when a tfvars or overlay will do.
Remote state for outputs (VPC id consumed by a service stack) creates dependencies between roots — document them; avoid cycles; prefer data sources and well-known naming over copy-paste IDs in three places.
Imperative vs declarative
| Imperative script | Declarative IaC | |
|---|---|---|
| Says | "create these resources" | "these resources should exist" |
| Re-run | may duplicate or fail | converges / empty plan |
| Review | the script text | the plan diff of real changes |
| Drift | invisible | detectable with plan |
| Rollback | write compensating script | apply previous config (with care for data) |
Imperative tools still appear for one-off surgery and for providers with weak declarative coverage — wrap them tightly and prefer eventual expression in the declarative graph. The goal is not purity; it is a reviewed, idempotent path from Git to cloud reality.
Policy-as-code gates catch classes of mistakes before apply: forbidden public object storage, missing required tags, instance types outside the allow-list, opens SSH from 0.0.0.0/0. A failing policy in CI is cheaper than an emergency lockdown after a scanner finds exposure. Start with a few high-severity rules and grow; a thousand noisy warns teach people to ignore the gate.
Import of existing console-built resources is a one-time tax that unlocks the three-picture loop — better than living with permanent drift. After import, immediately plan and expect empty (or understand every residual diff). State surgery (rm of a resource from state without destroy) is occasionally needed; pair it with a written reason and a follow-up that recreates management correctly.
Workspaces or separate roots per region prevent a single apply from spanning failure domains you meant to isolate. Break glass procedures for unlocking state and reverting a bad apply belong in the same runbooks as application rollback — infrastructure outages are incidents too.
Next: Cloud building blocks — choosing compute, network, data and identity along blast radius and cost axes, with the numbers that make those choices concrete.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.