Overview
Curated: · Written: · Reviewed:
Infrastructure as Code
In one line
Describe infrastructure as version-controlled configuration that a tool converges toward from recorded state, so changes are reviewed, repeatable and recoverable. Reach for it whenever a change must be approved before it runs and repeated without someone remembering the console steps.
Key definitions
- Idempotent: applying unchanged configuration produces no changes. Convergence is different: repeated runs move toward the desired state, sometimes over several passes.
- State: a record mapping configuration addresses (
aws_db_instance.orders) to real objects and their attributes. Sensitive, and never hand-edited. - Plan: the diff of configuration against state and observed remote objects. Evidence of what would happen, not a guarantee, and it expires.
- Force replacement (
-/+): an attribute change the provider decides cannot be applied in place, so the object is destroyed and recreated. - Provider: the plugin that maps resource types to API operations and decides which changes are in place.
- Drift: a difference between the expected configuration and the observed infrastructure.
How it works
The tool reads state, refreshes observed objects through the provider, and diffs both against configuration. Each resource is written to state as its operation completes, so a failed run leaves earlier resources created and recorded. A saved plan binds the reviewed operation set to the apply.
When to use and when not to
| Situation | Fit | Deciding trade-off |
|---|---|---|
| Long-lived shared infrastructure with review gates | Strong | Plans make changes auditable |
| One-off throwaway sandbox | Weak | State and pipeline overhead exceed the value |
| Logic-heavy setup needing real loops and tests | DSL strained | A general-language tool such as Pulumi or CDK fits better, but the program must be deterministic |
| Rebuilding stateful data stores casually | Dangerous | Replacement can lose data; rollback is not a rewind |
| Mostly console-managed estate | Adopt gradually | Import first, then check the plan for unexpected changes |
Traps interviewers probe
- Counting resources instead of reading verbs. "7 to add" hides a destroy. Fix: read
-/+,+/-,-and any~on a force-new attribute, and check what is attached. - Treating
sensitive = trueas secret protection. It redacts CLI output only; the value stays in state. Fix: restrict who can read state, encrypt it with a KMS key, and keep secrets out of configuration. - Renaming a resource without a
movedblock. The plan shows a destroy and a create. Fix: add amovedblock or runterraform state mv, and confirm the plan shows no destroy. - Assuming a failed apply rolls back. Fix: state the current real state, then write a forward plan that reconciles it.
- Using
ignore_changesto silence drift. It hides material change, such as an autoscaler's count, from every later apply. Fix: scope it to one attribute, record why, and review it. - Loose version ranges with no lockfile. A caret range accepts a new minor release, and an unlocked CI build pulls it on the next run. Fix: pin versions, commit the lock file, and upgrade through reviewed pull requests.
Interview tips
Open with the model: "Desired state in version control, state records what the tool manages, and the plan is the diff I review before apply. Apply is idempotent but not atomic."
Expect these follow-ups:
- What happens on a second run? Nothing, if reality matches the configuration.
- A run fails on resource 41. Resources 1 to 40 exist and are in state. Re-run to converge; do not expect rollback.
- A console change was made. Classify it: revert, codify, or import. Preview before any automatic fix.
- Renaming or moving a resource. Use
movedblocks so the object survives. - Two people apply at once. State locking serializes applies; a stale lock needs its holder confirmed dead before force-unlock.
- Splitting a large state. Move components into separate states that share outputs through remote state reads, and keep network and app blast radius apart.
- Environment isolation. Separate accounts, credentials and state. CLI workspaces with shared credentials are a weak boundary.
- CI credentials. Use OIDC with audience and subject restrictions. Match the subject exactly: a branch pattern does not match a tag or an environment-scoped job.
30-second answer outline:
- Define it: declarative config, a plan diff against state, and an approved saved plan applied by CI.
- Name the guarantee: idempotent, not atomic; failures leave partial state.
- Name the risk: replacements and state access; read verbs and restrict state.
- Name the recovery: a forward reconciliation with backups and deletion protection on stateful resources.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.