Skip to content
TryHired.
Interview prep · Technical · checked against the official docs

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.

Updated Nov 15, 2026 · Facts checked against the official documentation · questions: TryHired
Quick answer

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

Basic Terraform interview questions

Advice

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 page
terraform {
  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 page
data "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 page
module "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

Intermediate Terraform interview questions

Advice

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 page
terraform {
  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 page
resource "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 page
resource "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

Advanced Terraform interview questions

Advice

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 page
import {
  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 page
moved {
  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

Practice

How to practice with this page

Advice
  • 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.
Next

Related interview prep and tools

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.

FAQ

Questions people ask

What Terraform topics come up often in interviews?
State and remote backends, plan versus apply, providers and version pinning, modules, count versus for_each, lifecycle rules, dependencies, importing and refactoring with moved blocks, drift, workspaces versus separate configurations, and running Terraform in CI.
Do these answers apply to OpenTofu?
OpenTofu is a fork of Terraform with the same core workflow, and these concepts apply to it, but features and behavior can diverge between versions. Check the documentation for the tool and version a team uses.
Do I need a cloud account to practice Terraform?
No. You can practice the workflow with providers that do not need a cloud account, such as local or random, and read plans carefully. For cloud-specific roles, a free tier or sandbox account helps, but watch what you create and destroy it afterward.
Method

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.

What this page can't tell you
  • 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.
Sources
  1. Terraform docs: terraform apply (read 2026-11-15)
  2. Terraform docs: backends (read 2026-11-15)
  3. Terraform docs: count (read 2026-11-15)
  4. Terraform docs: data sources (read 2026-11-15)
  5. Terraform docs: depends_on (read 2026-11-15)
  6. Terraform docs: for_each (read 2026-11-15)
  7. Terraform docs: import (read 2026-11-15)
  8. Terraform docs: lifecycle (read 2026-11-15)
  9. Terraform docs: dependency lock file (read 2026-11-15)
  10. Terraform docs: state locking (read 2026-11-15)
  11. Terraform docs: modules (read 2026-11-15)
  12. Terraform docs: moved blocks (read 2026-11-15)
  13. Terraform docs: outputs (read 2026-11-15)
  14. Terraform docs: terraform plan (read 2026-11-15)
  15. Terraform docs: providers (read 2026-11-15)
  16. Terraform docs: S3 backend (read 2026-11-15)
  17. Terraform docs: sensitive data (read 2026-11-15)
  18. Terraform docs: state (read 2026-11-15)
  19. Terraform docs: input variables (read 2026-11-15)
  20. Terraform docs: workspaces (read 2026-11-15)