Skip to the page

CI/CD and infrastructure as code · mid

Terraform state, safe plans, drift and OpenTofu

State links code to real resources. Plans must be read for destroys, refactors need moved blocks, and drift needs a decision. Terraform and OpenTofu are close but not the same product.

The notes

What you must know

  • State maps each resource in the config to a real object, so Terraform knows what exists. It often holds secrets in plain text, so keep it encrypted, restricted and versioned in a remote backend, never in Git.
  • Locking prevents two applies at once. The S3 backend now uses a lock file (use_lockfile); DynamoDB locking is deprecated.
  • Terraform tracks resources by address. Moving a resource into a module looks like destroy and create. Fix with a moved block (or terraform state mv) so the plan shows no change.
  • Some attributes force replacement, such as storage_encrypted or identifier on an RDS instance. Add prevent_destroy for critical resources and read every destroy line in a plan before merge. Turn off auto-apply for risky changes.
  • Drift is a difference between real infrastructure and state or config, for example from a console change in an incident. terraform plan -refresh-only shows only drift.
  • Decide per change: keep it (update the code first), remove it, or replace it with a proper rule. Do not apply blindly.
  • Terraform is under the BSL licence since 2023. OpenTofu is the open-source fork under the Linux Foundation. Both use HCL and providers. Check feature differences, state encryption, tooling and your policy before moving.
  • Pin versions: required_version, provider constraints and the committed lock file.

The red pen

Common traps: mistake → fix

  • Mistake: State is only a cache

    Fix: it is the link to real resources; losing it causes duplicates.

  • Mistake: A plan with a destroy is fine if approved twice

    Fix: read the destroy yourself.

  • Mistake: Console fixes during an incident need no follow-up

    Fix: reconcile the code.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is Terraform state and where do you keep it?
  2. What happens if two people apply at once?
  3. A refactor plan wants to destroy the production database. What do you do?
  4. Someone changed it in the console. What do you do?
  5. Terraform or OpenTofu?

Practise

Practise with these questions

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview