Skip to content
Tech Interview Prep
Technical interview guide

Infrastructure as Code

Defining infrastructure in version-controlled configuration instead of clicking through a console: reproducible, reviewable, and diffable.

Minutes to read
4
Practice MCQs
25
Interview Q&A
25
Edition
v7
Editorial status
Reviewed

Scope: Terraform language and CLI documentation accessed 2026-08-30; OpenTofu 1.13 state encryption; current AWS CloudFormation change-set and drift behavior..

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.

Config change in PR Saved plan in CI Reviewer reads verbs Approved Apply saved plan State written per resource Next plan diffs state

When to use and when not to

SituationFitDeciding trade-off
Long-lived shared infrastructure with review gatesStrongPlans make changes auditable
One-off throwaway sandboxWeakState and pipeline overhead exceed the value
Logic-heavy setup needing real loops and testsDSL strainedA general-language tool such as Pulumi or CDK fits better, but the program must be deterministic
Rebuilding stateful data stores casuallyDangerousReplacement can lose data; rollback is not a rewind
Mostly console-managed estateAdopt graduallyImport 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 = true as 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 moved block. The plan shows a destroy and a create. Fix: add a moved block or run terraform 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_changes to 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 moved blocks 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:

  1. Define it: declarative config, a plan diff against state, and an approved saved plan applied by CI.
  2. Name the guarantee: idempotent, not atomic; failures leave partial state.
  3. Name the risk: replacements and state access; read verbs and restrict state.
  4. 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.