Terraform interview questions: answers checked against the docs
Infrastructure-as-code questions for DevOps, cloud and platform roles, answered in plain words and checked against the Terraform documentation.
Terraform interviews cover state and why it matters, plan and apply, providers and version locking, resources versus data sources, variables, outputs and modules, remote backends and state locking, count versus for_each, lifecycle rules, dependencies, drift, importing existing resources, refactoring with moved blocks, workspaces and running Terraform in CI. Below are 14 questions; each answer lists the Terraform documentation pages it was checked against.
Basic Terraform interview questions
Basic1. What is Terraform state, and why does it matter?
State is Terraform's record of which real objects belong to which resources in your configuration, plus metadata such as dependencies. On each plan, Terraform compares configuration, state and the real infrastructure to work out what to change. If state is lost or wrong, Terraform may try to recreate things that exist or lose track of things it created, so state is treated as critical data.
Checked against: Terraform docs: state
Basic2. What do terraform plan and terraform apply do?
plan refreshes what it knows about existing objects, compares them with the configuration, and shows the changes it would make, without making them. apply carries out a plan: either the one it computes and asks you to confirm, or a saved plan file from plan -out. In pipelines, save the plan, review it, then apply exactly that file.
Checked against: Terraform docs: terraform apply, Terraform docs: terraform plan
Basic3. What is a provider?
A provider is a plugin that lets Terraform talk to an API, such as a cloud platform, a SaaS product or Kubernetes, and defines the resources and data sources you can use. You declare required providers with version constraints; terraform init installs them and records the chosen versions in .terraform.lock.hcl, which should be committed so everyone uses the same versions.
HCL snippet · illustrative, not run on this pageterraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}Checked against: Terraform docs: dependency lock file, Terraform docs: providers
Basic4. What is the difference between a resource and a data source?
A resource block describes something Terraform creates and manages. A data block only reads existing information, for example the ID of a network created by another team or the latest image matching a filter, and never creates or changes anything.
HCL snippet · illustrative, not run on this pagedata "aws_vpc" "shared" {
tags = { Name = "shared-network" }
}
resource "aws_subnet" "app" {
vpc_id = data.aws_vpc.shared.id
cidr_block = "10.0.1.0/24"
}Checked against: Terraform docs: data sources
Basic5. What are input variables, outputs and modules?
Variables are a module's inputs, outputs are its return values, and a module is a folder of configuration you call with a module block. The configuration in your working directory is the root module; modules it calls are child modules. Outputs are how a child module passes values such as an ID back to the caller.
HCL snippet · illustrative, not run on this pagemodule "network" {
source = "./modules/network"
cidr_block = var.cidr_block
}
output "subnet_id" {
value = module.network.subnet_id
}Checked against: Terraform docs: modules, Terraform docs: outputs, Terraform docs: input variables
Intermediate Terraform interview questions
Intermediate6. Where should state live for a team, and what is state locking?
Not on one laptop. By default the local backend writes terraform.tfstate to disk; teams use a remote backend (object storage, HCP Terraform and others) so everyone works from the same state. Backends that support locking stop two runs writing state at once, which could corrupt it; locking is not always on by default, so for the S3 backend you enable it with use_lockfile = true. State can also contain secrets in plain text, so restrict who can read it and keep it out of Git.
HCL snippet · illustrative, not run on this pageterraform {
backend "s3" {
bucket = "example-terraform-state"
key = "network/terraform.tfstate"
region = "us-east-1"
use_lockfile = true
}
}Checked against: Terraform docs: backends, Terraform docs: state locking, Terraform docs: S3 backend, Terraform docs: sensitive data
Intermediate7. When do you use count and when for_each?
count creates a number of nearly identical copies, addressed by index ([0], [1]). for_each creates one instance per key of a map or set, addressed by key. Prefer for_each when instances differ or when items may be removed from the middle of the list: with count, removing an item shifts every later index and Terraform plans to change or replace those instances.
HCL snippet · illustrative, not run on this pageresource "aws_iam_user" "team" {
for_each = toset(["ava", "ben", "chen"])
name = each.key
}Checked against: Terraform docs: count, Terraform docs: for_each
Intermediate8. What do the lifecycle settings create_before_destroy and prevent_destroy do?
When a change forces replacement, Terraform normally destroys the old object first. create_before_destroy creates the new one first, which avoids downtime for things like certificates or launch templates. prevent_destroy makes any plan that would destroy the resource fail with an error. It is not a complete safety net: removing the resource block from the configuration still destroys the object.
HCL snippet · illustrative, not run on this pageresource "aws_db_instance" "main" {
# ...
lifecycle {
prevent_destroy = true
}
}Checked against: Terraform docs: lifecycle
Intermediate9. How does Terraform order resources, and when do you need depends_on?
Terraform builds a dependency graph from references: a subnet that uses vpc.id is created after the VPC, and independent resources are created in parallel. depends_on is only for hidden dependencies Terraform cannot see, such as a policy that must exist before an application starts using it, although nothing in the configuration references it. Use it as a last resort: the Terraform docs warn that it can make plans more conservative, treating more values as known only after apply and replacing more resources than necessary.
Checked against: Terraform docs: depends_on
Intermediate10. What is drift, and how do you handle it?
Drift is when the real infrastructure no longer matches state, usually because someone changed it outside Terraform. A normal plan reads current settings and proposes changes to bring things back in line with the configuration. A refresh-only plan updates state to match reality without changing infrastructure, which helps when you want to accept the manual change and then update the code.
Checked against: Terraform docs: terraform plan
Advanced Terraform interview questions
Advanced11. How do you bring existing infrastructure under Terraform?
Write an import block with the object's ID and the resource address it should have, plus a matching resource block, then plan and apply. Terraform adds the object to state without recreating it. Review the plan carefully: any difference between your resource block and the real settings shows up as a change.
HCL snippet · illustrative, not run on this pageimport {
to = aws_s3_bucket.logs
id = "example-logs-bucket"
}
resource "aws_s3_bucket" "logs" {
bucket = "example-logs-bucket"
}Checked against: Terraform docs: import
Advanced12. How do you rename a resource or move it into a module without destroying it?
Add a moved block from the old address to the new one. Terraform updates the address in state as part of the next plan instead of planning a destroy and a create. This is safer than editing state by hand, and it documents the refactor in code. Older workflows used terraform state mv for the same job.
HCL snippet · illustrative, not run on this pagemoved {
from = aws_instance.web
to = module.web.aws_instance.this
}Checked against: Terraform docs: moved blocks
Advanced13. Should you use workspaces for dev, staging and production?
CLI workspaces give one configuration several separate states, which is handy for short-lived copies such as a feature-branch environment. The Terraform docs warn that they are not suitable when environments need separate credentials and access controls, which production usually does. Separate root configurations or directories per environment, sharing modules, are a common alternative.
Checked against: Terraform docs: workspaces
Advanced14. How would you run Terraform safely in CI/CD?
Run fmt and validate, then plan on every pull request and post the plan for review; apply the saved plan only after approval, from one pipeline, with a remote backend and locking. Pin provider and module versions, keep secrets out of variables files and state access tight, and limit the blast radius by splitting large configurations. Expect a follow-up on how you would recover from a failed apply.
Checked against: Terraform docs: state locking, Terraform docs: terraform plan, Terraform docs: sensitive data
How to practice with this page
- Explain every answer in terms of state: what Terraform records and what it compares.
- Read real plan output carefully and explain each symbol: create, update, replace, destroy.
- Build a small configuration with a module, a for_each and a remote backend.
- Prepare a story about a risky change and how you limited its blast radius.
Related interview prep and tools
The cloud services Terraform often manages.
Open →QuestionsDevOps interview questionsCI/CD and operations questions.
Open →QuestionsKubernetes interview questionsContainer orchestration questions.
Open →QuestionsGit interview questionsVersion control with real output.
Open →Free toolResume summary generatorDraft a summary for a DevOps Engineer role; the title is filled in, you add your experience.
Open →Free toolLinkedIn headline generatorWrite a headline for a DevOps Engineer search.
Open →Disclosure: TryHired makes HIRED, a paid job-search program, so we have a conflict of interest when we mention it. This question bank is free, with no TryHired signup.
Questions people ask
What Terraform topics come up often in interviews?
Do these answers apply to OpenTofu?
Do I need a cloud account to practice Terraform?
How this page is built
TryHired wrote these questions and answers. Statements about the technology were checked against the official documentation pages listed below, saved when the page was built (2026-11-15). The code snippets are short illustrations and were not run on this page. The question list is general practice: it does not predict what a particular employer will ask.
- Interviewers ask different questions for different roles and levels.
- Answers are short explanations, not complete references.
- No Terraform binary or cloud account was used on the build machine, so no plan output is shown; the HCL snippets are illustrations, and provider resource names are examples.
- Terraform docs: terraform apply (read 2026-11-15)
- Terraform docs: backends (read 2026-11-15)
- Terraform docs: count (read 2026-11-15)
- Terraform docs: data sources (read 2026-11-15)
- Terraform docs: depends_on (read 2026-11-15)
- Terraform docs: for_each (read 2026-11-15)
- Terraform docs: import (read 2026-11-15)
- Terraform docs: lifecycle (read 2026-11-15)
- Terraform docs: dependency lock file (read 2026-11-15)
- Terraform docs: state locking (read 2026-11-15)
- Terraform docs: modules (read 2026-11-15)
- Terraform docs: moved blocks (read 2026-11-15)
- Terraform docs: outputs (read 2026-11-15)
- Terraform docs: terraform plan (read 2026-11-15)
- Terraform docs: providers (read 2026-11-15)
- Terraform docs: S3 backend (read 2026-11-15)
- Terraform docs: sensitive data (read 2026-11-15)
- Terraform docs: state (read 2026-11-15)
- Terraform docs: input variables (read 2026-11-15)
- Terraform docs: workspaces (read 2026-11-15)