Introduction
Terraform interviews rarely test whether you have memorised flags. They test whether you have actually run Terraform in a team: whether you understand state, why a remote backend matters, how you refactor without destroying production, and what you do when an apply fails halfway through.
This article gives 20 questions in the form interviewers ask them, grouped from easy to advanced, with model answers that show reasoning — not just definitions. Each answer is written so you can adapt it to your own experience. If you have never used a concept in production, say so and explain how you would approach it; interviewers respect honesty far more than a rehearsed definition that falls apart on the first follow-up.
Version note (25 Nov 2026): the latest Terraform release is 1.17.2 (11 Nov 2026); 1.17.0 was released 21 Oct 2026. S3 native locking uses use_lockfile = true; DynamoDB locking is the older pattern. Terraform 1.6 and later is under the Business Source License 1.1, and OpenTofu is the open-source fork. Check the HashiCorp documentation on the day of your interview — version numbers move quickly.
How to Use This Article
Read the easy questions first and answer them aloud before reading the model answer. For each one, ask yourself the follow-up an interviewer would ask: “What happens when that goes wrong?” That second question is where interviews are won. The advanced section is where senior candidates separate themselves — if you are applying for a junior role, understanding the concepts is enough; you are not expected to have solved all of them in production.
Easy — Core Concepts
1. What is Terraform, and what problem does it solve?
Terraform is HashiCorp’s infrastructure-as-code tool. You describe the infrastructure you want — virtual machines, networks, databases, DNS — in declarative configuration files (HCL), and Terraform creates, changes or deletes real cloud resources to match. It solves the problem of manual, undocumented, unrepeatable infrastructure: instead of clicking through a console, the infrastructure is versioned in Git, reviewable in a pull request, and reproducible in another environment.
Follow-up: “Declarative vs imperative?” — In declarative IaC you state the desired end state and the tool works out the steps. In an imperative script you list the steps yourself. Terraform is declarative; a Bash script that calls the AWS CLI is imperative.
2. Explain the Terraform workflow: init, plan, apply, destroy.
terraform init— downloads providers and modules, and configures the backend. Run it after cloning a repository or changing providers.terraform plan— refreshes state, compares it with your configuration and the real cloud, and prints the actions it would take, without changing anything. Saving it with-outgives you an exact plan to apply later.terraform apply— executes a plan (or creates one and applies it), asking for confirmation unless given a saved plan or-auto-approve.terraform destroy— removes everything managed by the configuration.terraform validateandterraform fmtbelong in the same workflow: validate checks syntax and internal consistency; fmt formats the code consistently.
3. What is Terraform state, and why does it matter?
State is a JSON file that maps each resource address in your configuration to the real object’s ID and attributes, plus dependencies and outputs. Terraform needs it to know what already exists, to compute diffs, and to order operations. Lose the state and Terraform no longer knows it owns the resources — it would try to create duplicates. State can contain secrets in plain text, such as database passwords, so it must live in an encrypted, access-controlled backend and never be committed to Git. Do not edit state by hand; use terraform state commands, moved blocks or import.
4. What is a provider?
A provider is a plugin that lets Terraform talk to a platform — AWS, Azure, Google Cloud, Kubernetes, GitHub and hundreds more. You declare the providers and their versions in a required_providers block, and pin the versions so a teammate or CI run does not silently get a different provider with different behaviour.
5. What are variables, locals and outputs?
- Variables are inputs from outside the configuration (environment, region, instance size). They are typed, can have defaults and descriptions, and are set via
.tfvarsfiles, environment variables or the command line. - Locals are named expressions computed inside the configuration — useful for combining values or avoiding repetition, but not settable from outside.
- Outputs are values returned to the caller — for example, a load balancer’s DNS name — so another configuration or a human can use them.
Medium — State, Backends and Modules
6. Why use a remote backend and state locking? Describe a typical setup.
A remote backend shares state between teammates and CI, adds encryption and versioning, and avoids losing a local file. Locking prevents two applies from writing state at the same time, which could corrupt it.
A typical AWS setup: an S3 bucket with versioning and encryption holds the state, and locking is done with the native lockfile option (use_lockfile = true in the backend block, generally available since Terraform 1.11). The older DynamoDB-table pattern is deprecated — mention it only to show you know the history. Azure uses a storage-account blob container with lease-based locking; Google Cloud uses a GCS bucket; HCP Terraform offers managed state with run history.
terraform {
backend "s3" {
bucket = "my-team-terraform-state"
key = "network/terraform.tfstate"
region = "us-east-1"
encrypt = true
use_lockfile = true
}
}
Note: the backend block cannot reference variables, locals or data sources, so per-environment values go in partial configuration files passed with terraform init -backend-config=prod.s3.tfbackend.
7. An apply fails with “Error acquiring the state lock”. What do you do?
Read the lock information in the error first: it shows the lock ID, the operation, who holds the lock and when it was taken. If a teammate or a CI run genuinely holds it, wait. If it is a stale lock from a crashed run, verify no apply is actually running, then release it with terraform force-unlock <LOCK_ID> — and only after that check, because force-unlocking a live apply can corrupt state. Then investigate why the run crashed.
8. How do you secure state, given it can contain secrets?
Encrypt it at rest (backend encryption, ideally with a customer-managed KMS key), restrict read/write access tightly with IAM — state access is effectively infrastructure access — enable versioning so a bad write can be recovered, never commit state or saved plan files to Git, and mark sensitive variables and outputs as sensitive = true (which hides them from console output but does not encrypt them in state). Rotate any secret that has ever appeared in an exposed state file.
9. count vs for_each — when do you use each?
Use for_each for named items (a map of environments, a set of subnets), because removing one key does not renumber the others. Use count for N identical copies, or as a 0/1 switch to create a resource conditionally. The classic interview trap: with count, deleting the second of three items renumbers the third and forces a destroy-and-recreate; with for_each, only the deleted key is removed.
10. What are Terraform modules, and how do you design a good one?
A module is a reusable package of Terraform configuration — a folder with its own variables, resources and outputs — that encapsulates one piece of infrastructure, such as a VPC or an EKS cluster. A good module has a narrow purpose, typed variables with descriptions and sensible defaults, outputs for what callers need, no hard-coded environment values, and a version pinned by the caller (a Git tag or Registry version). Keep modules small and composable; a module that tries to build an entire application is hard to test and harder to reuse.
11. How do you rename or move a resource without destroying it?
With a moved block, which tells Terraform that the resource at the old address is now at the new address. Terraform updates the state instead of destroying and recreating the resource. For adopting a resource created outside Terraform, use an import block (optionally with terraform plan -generate-config-out to generate the configuration). Always run plan after a move or import and review it before applying.
12. What is the difference between Terraform workspaces and modules?
Workspaces are separate state files for the same configuration — commonly used for short-lived environments such as dev/staging/prod when the infrastructure is nearly identical. Modules are reusable components. In practice, many teams prefer separate folders or separate state files per environment over workspaces, because workspaces share the same code and variables, which makes it easy to apply a change to the wrong environment. If you use workspaces, name them clearly and guard production applies with a separate approval step.
Advanced — Troubleshooting and Team Practice
13. Terraform wants to destroy and recreate a resource you did not change. How do you investigate?
First, read the plan carefully: which attribute changed, and why? Common causes are a provider upgrade changing a default, an attribute that is computed on the server (such as an AMI ID or a generated password), drift from a manual console change, or a resource whose name or key field changed. Check terraform plan with the previous provider version, look at terraform state show for the current values, and check the provider changelog. If the change is spurious, use lifecycle { ignore_changes = [...] } sparingly and deliberately — never as a blanket to hide real drift.
14. How do you handle drift — resources changed outside Terraform?
Run terraform plan -refresh-only to see what has drifted without proposing changes, then decide: either update the configuration to match the intentional change, or apply to bring reality back to the configuration. For important resources, use drift detection in CI (a scheduled plan that alerts on unexpected diffs). The deeper fix is process: restrict direct console access in production so changes go through Terraform and review.
15. How do you structure Terraform for a real team and multiple environments?
Separate state per environment and per concern (networking, platform, applications), so a mistake in one does not affect the others. Use a remote backend with locking and versioning for each state file. Share data between states with terraform_remote_state or, better, with explicit outputs consumed via a data source or a service catalogue — remote-state reads create tight coupling, so prefer passing a small number of stable values (VPC ID, cluster name). Put shared modules in a versioned repository, pin versions in every caller, and run fmt, validate and plan in CI on every pull request, with apply only after approval.
16. What is terraform taint / -replace, and which should you use?
terraform taint marked a resource for recreation on the next apply, but it is deprecated. Use -replace instead (for example, terraform apply -replace=aws_instance.web), because the rebuild appears in a reviewed plan before anything happens. Either way, explain why the resource needs replacing rather than updating — an interviewer wants the reasoning, not the flag.
17. How do you test Terraform code?
At minimum: terraform fmt -check and terraform validate in CI, plus terraform plan on every pull request so reviewers see the exact changes. Beyond that, use policy-as-code (HashiCorp’s Terraform Policy, which reached general availability in Terraform 1.17, or Open Policy Agent / Checkov) to enforce rules such as “no public S3 buckets” or “all resources must be tagged”. For critical modules, use terraform test (the native test framework) with mock providers, and run the module against a disposable environment before promoting it.
18. Terraform vs CloudFormation vs Pulumi vs Ansible — how do you choose?
- Terraform is cloud-agnostic and declarative, with the largest provider ecosystem; it is the default choice for multi-cloud or AWS-heavy teams.
- CloudFormation is AWS-only and native, with deep AWS integration but no portability.
- Pulumi lets you use a general-purpose language (TypeScript, Python, Go) instead of HCL, which suits teams that prefer code to configuration.
- Ansible is primarily configuration management (what happens inside a server) rather than provisioning (creating the server itself); it complements Terraform rather than replacing it. OpenTofu is a fork of Terraform for teams that require an open-source licence.
The honest interview answer is that the best tool depends on the team’s cloud, skills and constraints — and that you would use Terraform where its provider ecosystem and state model fit, not everywhere by default.
19. A plan shows 200 changes and you expected 2. What do you do?
Do not apply it. Stop, read the plan, and find the root cause: usually a variable change, a module version bump, a provider upgrade, or a state problem. Narrow the change with -target only to investigate (never as a routine way to apply), compare against the previous known-good plan, and check recent commits. If you cannot explain every change, do not apply any of them.
20. Tell me about a time Terraform went wrong for you.
Interviewers ask this to test honesty and learning. A good answer has a structure: what happened (for example, an apply that replaced a database because a name changed), what the impact was, what you did immediately (stop, restore from a snapshot or versioned state, communicate), and what you changed afterwards (a moved block habit, a plan-review rule, lifecycle protection on the database, or a backup check). If you have no production story yet, use a lab story and say so — a real lab failure, honestly told, is better than a borrowed war story.
Frequently Asked Questions
What are the most common Terraform interview questions? State, remote backends and locking, count vs for_each, modules, workspaces, moved/import, and how you investigate an unexpected plan. Those six topics cover most of the screen.
Is Terraform hard to learn? The basics — a resource, a plan and an apply — can be learned in days. The hard part is state management, module design and team workflow, which only come from running Terraform against real infrastructure and recovering from mistakes.
Do I need to know Terraform for a DevOps job? For most DevOps, platform and cloud engineer roles in 2026, yes — Terraform is the most commonly listed infrastructure-as-code tool in postings. Learn it after Linux, Git and one cloud, as in our DevOps Engineer Roadmap 2026.
What version of Terraform should I learn? The current stable line — 1.17.2 at the time of writing (Nov 2026). Learn the workflow and state model first; version-specific features are easy to add later, and interviewers will forgive a version gap if the concepts are solid.
Conclusion
Terraform interviews reward people who have thought about failure: lost state, stale locks, surprise destroys, and drift. Learn the workflow, practise state and backends in a disposable AWS account, refactor a small project with moved blocks and modules, and be ready to talk about a time something went wrong and what you changed. That is what interviewers are actually listening for.
Practise this week: build a VPC and an EC2 instance with Terraform, move the state to an S3 backend with locking, then rename a resource with a moved block and confirm the plan shows no destroy. For broader preparation, see the DevOps Interview Questions and Answers for 2026.
