Terraform Scenario Based Interview Questions: Read the Plan, Then Answer

Branko Petric
Branko Petric

- 13 min read

The short answer

Terraform scenario questions test whether you read the plan before you act. Find the line that says forces replacement, check state with terraform state list and show, adopt existing resources with import blocks instead of recreating them, and shrink blast radius by splitting state. Name the risk out loud and hold your position under pushback.

Junior Terraform questions ask for definitions: what is state, what is a module, what does terraform plan do. Experienced candidates usually get a different kind of question. The interviewer pastes a plan, or describes a mess, and asks what you would do next. Then they argue with you.

Below are five common Terraform scenarios, each with the prompt, the commands you would run, the pushback to expect, and a weak answer next to a strong one. For the general shape of a DevOps loop, see how to pass a DevOps interview.

What these questions are really testing

Three things, in every scenario below:

  1. Do you read the plan before you act? The plan is a diff between config, state and reality.
  2. Do you understand what state is? State maps a resource address like aws_db_instance.main to a real object ID. Almost every scenario is a state question in disguise.
  3. Do you reason about blast radius? What is the worst thing this apply can do, and how do you make that smaller?

A weak answer names a command. A strong one says what the command will show and what you will decide from it.

Scenario 1: "The plan wants to destroy the production database"

The prompt: "You open a PR that changes one tag on the production RDS instance. CI runs terraform plan and it says the instance must be replaced. It is peak traffic and the apply button is one click away. What do you do?"

This is the classic terraform destroy in plan interview question. Say you will not apply, then show you can read the plan.

terraform plan -out=tfplan
terraform show tfplan

The lines that matter look like this:

  # aws_db_instance.main must be replaced
-/+ resource "aws_db_instance" "main" {
      ~ storage_encrypted = false -> true # forces replacement
      ~ tags              = {
          ~ "team" = "payments" -> "platform"
        }
        # (40 unchanged attributes hidden)
    }

Plan: 1 to add, 0 to change, 1 to destroy.

-/+ means destroy and then create a replacement. (+/- means create the replacement first, which you get with create_before_destroy.) The attribute marked # forces replacement is the real cause. In this example it is not the tag at all. Something else in the config or the provider changed an attribute that cannot be updated in place.

On a big plan, do not scroll and hope. Pull out every destructive action from the saved plan:

terraform show -json tfplan \
  | jq -r '.resource_changes[] | select(any(.change.actions[]; . == "delete")) | .address'

Likely causes: a config value that differs from the live instance, a renamed resource address, a module refactor, or a provider upgrade that changed a default. Each has a different fix, and none of them is "apply and see".

The pushback: "We take hourly snapshots. If anything goes wrong we restore. So applying is low risk, right?"

Weak answer Strong answer
"Backups exist, so I would apply in a quiet window." "A restore still means downtime and losing everything since the last snapshot. The plan is not telling me about my change, it is telling me about a different one. I find that attribute first."
"I would run it with -auto-approve to unblock the team." "Schedule pressure is not a reason to apply a plan I do not understand. I will unblock the other PRs some other way."

The rest of the argument, how to fix the cause without a destroy, which guardrails to add, and what to do if replacement is genuinely required, is what the challenge scores you on.

Practice this one: /challenges/terraform-destructive-apply

Scenario 2: "Someone changed it in the console and now the plan is a mess"

The prompt: "During an incident last night, the on-call engineer changed a load balancer and a security group by hand. Today your plan wants to revert several settings, and nothing in your code changed. How do you reconcile it?"

This is the terraform state drift interview question. The key point: a normal terraform apply here does not "fix drift". It silently reverts whatever the on-call engineer did, and you do not yet know whether that change was a deliberate fix.

First, see the drift on its own, separated from any config changes:

terraform plan -refresh-only

That shows what changed outside Terraform, without proposing infrastructure changes. Then inspect what Terraform holds:

terraform state list
terraform state show aws_security_group.app

Now decide resource by resource:

  • The manual change was correct and should stay. Write it into the .tf files, then run terraform plan until it reports No changes. Your infrastructure matches the configuration.
  • The manual change was a temporary hack. Confirm with the person who made it, then let a normal apply revert it on purpose, at a time you chose.

terraform refresh still exists, but it is deprecated and behaves like terraform apply -refresh-only -auto-approve: it rewrites state with no review step. Accepting reality into state also leaves your code disagreeing with production, so the next person sees the same confusing diff. If an attribute is legitimately managed outside Terraform, such as an autoscaling group's desired capacity, lifecycle { ignore_changes = [...] } is the honest way to say so.

Then fix the process: limit console write access in production, run a scheduled terraform plan -detailed-exitcode (exit code 2 means changes) to catch drift, and give on-call a fast path through Terraform.

The pushback: "Terraform is the source of truth. Just apply so everything matches the code."

Weak answer Strong answer
"Yes, apply. The console change broke the rules." "Apply would undo a change I have not understood. It may have been the incident fix. I check with the engineer, then keep it in code or revert it deliberately."
"Run terraform refresh and we are back in sync." "Refresh syncs state with reality, not code with reality. The drift is still there, just hidden from the next plan's reader."
"Tell the engineer not to do that again." "They changed it during an incident for a reason. The fix is a faster path through Terraform, plus drift detection."

Practice this one: /challenges/terraform-state-drift

Scenario 3: "Terraform does not know about the database. Just let it recreate it?"

The prompt: "A production RDS instance was created by hand months ago. You wrote the HCL for it, and the plan shows a create. A teammate says just apply, Terraform will make a matching one. What do you do?"

This is terraform import vs recreate. Terraform does not adopt something it has never seen. A resource that is not in state is, as far as Terraform knows, something that does not exist yet. Depending on the identifier, the apply either fails because the name is taken or creates a second, empty database next to the real one. Neither is "consistent". The real data lives in the instance Terraform does not track.

The teammate's next suggestion is often to delete the console-made one so Terraform can own a clean one. That is the same data loss, done by hand first.

The right tool is import. Since Terraform 1.5 you can declare it in config, so it goes through review like any other change:

import {
  to = aws_db_instance.main
  id = "prod-db"   # for RDS, the DB instance identifier
}

resource "aws_db_instance" "main" {
  identifier = "prod-db"
  # ...
  lifecycle {
    prevent_destroy = true
  }
}
terraform plan    # should show "1 to import", and ideally 0 to change, 0 to destroy

terraform plan -generate-config-out=generated.tf can draft the HCL from the live resource. HashiCorp still labels config generation experimental, so treat the output as a starting point. OpenTofu supports the same import block. The older terraform import aws_db_instance.main prod-db command still works, but it writes to state immediately with no plan to review.

After import, the plan shows the real differences between your HCL and the live instance: parameter group, backup window, storage type, tags. Converge them one by one. Watch for any attribute you left to a default, because a default that differs from the live value can itself show up as # forces replacement. Keep iterating until the plan is clean.

prevent_destroy = true makes any plan that would destroy this resource fail with an error. One caveat worth saying in an interview: it lives in the resource block, so if someone deletes the whole block, the guard goes with it. Pair it with deletion_protection = true on the RDS instance itself, which the AWS API enforces regardless of what Terraform plans.

The pushback: "Import always fights you with a hundred diffs. Recreating is cleaner. Just do it off-hours."

Weak answer Strong answer
"Fair, I will recreate it and migrate the data over at night." "The diffs after import are the useful part. They show how the live database differs from my HCL. Recreate plus migrate adds downtime and risk to avoid an hour of config work."
"If it is not in state it should not exist, so recreate." "State is Terraform's record, not reality. An untracked resource is a gap in the record, and import fills it without touching the database."

Practice this one: /challenges/terraform-import-vs-recreate

Scenario 4: "Let's just commit the state file to git"

The prompt: "Two engineers applied from their laptops with local state and got conflicting results. A teammate proposes committing terraform.tfstate to the repo so everyone shares it. Do you agree?"

For experienced candidates, the second half matters more: what you do about state already in history.

Two reasons it is wrong:

  1. State holds secrets in plaintext. Generated passwords, private keys and tokens end up in state. Marking an output sensitive hides it from the CLI output, not from the state file. Commit it once and every clone has the secret, forever.
  2. Git has no lock. Pulling before every apply does not stop two applies from racing, and merging conflicting state JSON by hand can silently drop or duplicate resource mappings.

The fix is a remote backend with locking, encryption and versioning:

terraform {
  backend "s3" {
    bucket       = "acme-terraform-state"
    key          = "prod/network/terraform.tfstate"
    region       = "eu-west-1"
    encrypt      = true
    use_lockfile = true   # S3-native locking, GA in Terraform 1.11
  }
}

Older setups lock with a DynamoDB table (dynamodb_table), which still works but is deprecated in favor of use_lockfile. HCP Terraform (formerly Terraform Cloud) is the managed alternative, and OpenTofu adds client-side state encryption. Turn on bucket versioning so you can recover from a bad write, and run terraform init -migrate-state to move the existing local state in.

Then add *.tfstate and *.tfstate.* to .gitignore. If state was already committed, gitignore only stops future commits. Purge it from history with a tool like git filter-repo, and rotate every secret it contained, because you have to assume someone already cloned it.

The pushback: "It is a two-person project. We will just agree not to apply at the same time."

Weak answer Strong answer
"For a small team that is fine." "That agreement is exactly what just failed. A backend lock enforces it for almost no cost, and it keeps the database password out of git."
"Encrypt the file, then commit it." "Encryption adds no locking and adds key management. A remote backend solves locking, encryption and sharing at once."

Practice this one: /challenges/terraform-state-in-git

Scenario 5: "One state runs everything and nobody wants to touch it"

The prompt: "All infrastructure is in one Terraform state. Plan takes 10 minutes, every apply touches production-critical resources, and people avoid making changes. A teammate says just keep applying carefully. How do you fix it?"

"Be careful" does not change the structure. One state means a mistake anywhere can reach everything, and a slow, scary plan pushes people toward big batched changes, which are riskier still.

The strong answer is to split state by lifecycle and ownership, incrementally:

  • Networking and shared foundations change rarely. They get their own state.
  • Data stores are stateful and high-risk. They get their own state, out of the path of frequent app changes.
  • App infrastructure changes often. It gets its own state, per service if ownership is split that way.

The mechanics matter, because a naive split recreates live infrastructure. moved blocks (Terraform 1.1+) only work inside one state, so they help when you reorganize into modules before the split. To move a resource to a different state without destroying it, import it into the new configuration and tell the old one to forget it:

# In the new configuration (e.g. data-stores/)
import {
  to = aws_db_instance.main
  id = "prod-db"
}

# In the old configuration, after deleting the resource block
removed {
  from = aws_db_instance.main
  lifecycle {
    destroy = false
  }
}

The removed block needs Terraform 1.7 or later. OpenTofu accepts a lifecycle block inside removed from 1.10. Older OpenTofu versions do not support it there, and their removed block always forgets the resource without destroying it. On older versions, terraform state rm does the forgetting step, but without a reviewed plan. Either way, the order is: import into the new state and confirm a clean plan, then remove from the old one. Confirm both plans show no destroy before you apply either.

Cross-component dependencies, like a subnet ID the app needs, move to outputs read through terraform_remote_state or a data source that looks the resource up directly.

The pushback: "Just use -target on every apply so each change only touches what we want. That contains the blast radius, right?"

Weak answer Strong answer
"Yes, -target limits what gets applied." "Terraform itself warns that -target is for exceptional cases. It skips dependencies and can leave state inconsistent. It is a workaround, not structure."
"Let's rewrite it all into clean modules in one go." "A big-bang rewrite of live infrastructure is the highest-blast-radius change there is. I would carve out one component at a time, starting with the stateful ones."

Practice this one: /challenges/terraform-monolith-blast-radius

The same drift instinct gets tested outside Terraform too: a teammate wants to hand-edit /etc on an Ansible-managed fleet. Argue that version in the hotfix outside config management challenge.

A pattern you can reuse in any Terraform scenario

  1. Do not apply yet. Say it first.
  2. Read the plan. Find -/+, # forces replacement and the summary line. Use terraform show -json on large plans.
  3. Check state. terraform state list and terraform state show tell you what Terraform thinks exists.
  4. Separate drift from change with terraform plan -refresh-only.
  5. Fix the cause, not the symptom. A moved block for a rename, an import block for an untracked resource, a config change for a real difference.
  6. Add a guard. prevent_destroy, provider-level deletion protection, plan review in CI, a scheduled drift check.
  7. Name the trade-off. If a replacement is truly required, it is a planned migration with a cutover, not a routine apply.

The free state guide and lifecycle guide cover the commands and meta-arguments used above in more depth.

Practicing under pushback

Reading these answers is the easy part. In the interview, someone pushes back with a reasonable-sounding argument and watches whether you fold. A general chatbot makes up an answer on the spot. DevOps Ledger grades you against an answer key written before you started. Each scenario above has a challenge where you defend your call against that kind of pushback and see which arguments held up.

FAQ

What are Terraform scenario based interview questions?

They describe a situation instead of asking for a definition: a destructive plan, drift after a console change, an untracked resource, or a state file nobody dares touch. You are scored on what you check first, what you decide, and how you defend it.

What Terraform questions are asked for experienced engineers?

Expect state questions more than syntax ones: reading a plan with forces replacement, importing existing resources, moving resources between states without a destroy, remote backends and locking, and how to split a large state. Interviewers also push back on your answer to see whether you hold a correct position.

Should I use terraform import or recreate the resource?

For anything stateful, import. Recreating creates a new, empty object, while the real data stays in the untracked one. Use an import block (Terraform 1.5+ or OpenTofu), review the plan, and converge the config until the plan is clean.

How do you detect Terraform state drift?

Run terraform plan -refresh-only to see changes made outside Terraform without proposing infrastructure changes. For continuous detection, run terraform plan -detailed-exitcode on a schedule in CI and alert when it exits with code 2.

Does prevent_destroy fully protect a resource?

No. It makes any plan that would destroy the resource fail, but it lives in the resource block, so removing the block removes the protection. Pair it with deletion protection on the cloud resource itself where the provider supports it.

Practice these scenarios

Free guides to read next

Practice defending your decisions

DevOps Ledger is a scenario-based DevOps and SRE interview practice platform: you pick a real production incident, defend your decision in a chat against a senior-engineer AI that pushes back, and get scored against an answer key written before you started.

One payment. 12 months of access. No subscription, nothing renews.

Get full access - $19