Terraform Interview Questions and Answers

27 questions with short, clear answers · Updated December 2026

Terraform is the most asked-about infrastructure-as-code tool in cloud and DevOps interviews. These questions go from the core workflow to state management, modules and the team practices interviewers use to separate people who have only run terraform apply from people who have run Terraform in a team.

The answers apply to Terraform 1.x and, for nearly everything, to OpenTofu, the open-source fork.

Terraform basics

Q1. What is Terraform, and why is it declarative?

Terraform is an infrastructure-as-code tool by HashiCorp that creates and manages cloud and other resources from configuration files written in HCL. It is declarative: you describe the end state you want (three subnets, one database), and Terraform works out the create, update and delete steps needed to get there, instead of you writing each step.

Q2. Explain the core Terraform workflow.

terraform init downloads providers and modules and configures the backend; terraform fmt and validate check formatting and syntax; terraform plan compares the configuration with the state and the real infrastructure and shows what will change; terraform apply makes those changes; terraform destroy removes everything the configuration manages. In teams, plan runs on every pull request and apply runs after review.

Q3. What is a provider?

A provider is a plugin that lets Terraform talk to an API, such as aws, azurerm, google, kubernetes or github. It defines the resource and data source types you can use. Pin provider versions in a required_providers block, and commit the .terraform.lock.hcl file so everyone uses the same versions.

Q4. What is the difference between a resource and a data source?

A resource block creates and manages an object, such as an aws_instance; Terraform owns its lifecycle. A data block only reads existing information, such as the latest AMI ID or a VPC created by another team, so you can reference it without managing it.

Q5. What are input variables, outputs and locals?

Input variables (variable blocks) are parameters set through tfvars files, -var flags or TF_VAR_ environment variables, with types, defaults and validation rules. Outputs expose values after apply, for other modules or for people (mark secrets sensitive). Locals are named expressions inside a module, used to avoid repeating values such as common tags.

Q6. What is the difference between Terraform and OpenTofu?

In 2023 HashiCorp moved Terraform from the open-source MPL licence to the Business Source Licence. OpenTofu is a community fork under the Linux Foundation that stays open source (MPL 2.0) and is largely compatible, with some features of its own such as state encryption. HashiCorp itself is now part of IBM. Both use HCL and the same provider ecosystem.

State management

Q7. What is the Terraform state file, and why is it needed?

terraform.tfstate maps each resource in your configuration to the real object it created (its ID and attributes), records dependencies and caches values for performance. Without it Terraform cannot tell what it manages. State can contain secrets in plain text, so it must be stored securely and never committed to Git.

Q8. What is a remote backend, and how does state locking work?

A remote backend stores state centrally, for example in an S3 bucket, an Azure storage account, a GCS bucket or HCP Terraform, so a team shares one copy with encryption and versioning. Locking stops two people applying at the same time and corrupting state. The S3 backend now supports native locking with a lock file in the bucket (use_lockfile), replacing the older DynamoDB lock table; Azure and GCS lock natively.

Q9. What do terraform state list, state mv and state rm do?

state list shows the resources in state. state mv renames or moves a resource address, for example into a module, without destroying it; a moved block in code now does the same in a reviewable way. state rm makes Terraform forget a resource without deleting the real object; a removed block in code is the declarative equivalent.

Q10. How do you bring existing infrastructure under Terraform?

Write an import block with the resource address and the real ID, then run terraform plan -generate-config-out=generated.tf to have Terraform write the configuration, review and clean it up, and apply. The older terraform import command still works but does not generate code and is not reviewed through plan.

Q11. What is drift, and how do you handle it?

Drift is when the real infrastructure no longer matches the state, usually because someone changed it in the console. terraform plan shows it (plan -refresh-only shows only the drift). Either apply to put the infrastructure back as the code says, or update the code to accept the change. Prevent it by restricting console write access and running scheduled drift-detection plans.

Q12. How do you keep separate dev, staging and production environments?

The most common approach is a separate root configuration (directory) and separate state per environment, each calling the same modules with different variables, often in separate cloud accounts. Terraform CLI workspaces give several states for one configuration, which suits short-lived copies, but are risky for production because it is easy to apply to the wrong workspace. Tools such as Terragrunt reduce the repetition.

Modules and the language

Q13. What is a module, and why use one?

A module is a folder of Terraform files used as a unit; every configuration is the root module, and it can call child modules from local paths, the Terraform Registry or Git. Modules package a standard pattern (a VPC, an EKS cluster) once with inputs and outputs, so teams reuse it consistently. Pin module versions, and keep modules focused rather than building one giant module.

Q14. What is the difference between count and for_each?

count creates N copies addressed by index (resource[0], resource[1]); removing an item from the middle of a list shifts the indexes and makes Terraform destroy and recreate resources. for_each creates one instance per key of a map or set (resource["web"]), so adding or removing one item affects only that item. Use count for a simple on/off switch (count = var.enabled ? 1 : 0) and for_each for collections.

Q15. What are lifecycle meta-arguments?

create_before_destroy creates the replacement before deleting the old resource, avoiding downtime; prevent_destroy makes any plan that would delete the resource fail, protecting databases; ignore_changes tells Terraform to ignore attributes changed outside it, such as an Auto Scaling group's desired count; replace_triggered_by forces replacement when another resource changes.

Q16. How does Terraform decide the order in which to create resources?

It builds a dependency graph from references: if a subnet uses aws_vpc.main.id, the VPC is created first. Independent resources are created in parallel (10 at a time by default). depends_on adds an explicit dependency when there is no reference, such as an IAM policy that must exist before an application starts, but should be used sparingly.

Q17. What are dynamic blocks?

A dynamic block generates repeated nested blocks inside a resource from a collection, for example one ingress block per rule in a security group from a variable list. It keeps configuration short, but overusing it makes code hard to read, so use it where the number of blocks really varies.

Q18. How should secrets be handled in Terraform?

Do not hard-code them in .tf or committed .tfvars files. Read them at run time from a secrets manager through a data source or pass them as environment variables, mark variables and outputs as sensitive, and protect the state backend with encryption and tight access, because sensitive values are still written to state. Ephemeral values and write-only arguments in recent Terraform versions keep some secrets out of state entirely.

Commands and features

Q19. How do you force Terraform to recreate one resource?

Use terraform apply -replace="aws_instance.web", which plans a destroy and create for just that resource. It replaced the older terraform taint command, because the replacement shows up in the plan for review instead of silently changing state.

Q20. What are provisioners, and why are they a last resort?

Provisioners (local-exec, remote-exec, file) run scripts during resource creation or deletion. Terraform cannot track what they did, they make plans unpredictable, and a failure leaves a tainted resource. Prefer user data or cloud-init, pre-built images (Packer), configuration tools such as Ansible, or a native provider resource.

Q21. What do terraform output, terraform console and terraform graph do?

terraform output prints the root module's outputs (add -json for scripts). terraform console opens an interactive prompt to test expressions and functions against the current state, handy for debugging complex for expressions. terraform graph prints the dependency graph in DOT format for visualising.

Q22. What is HCP Terraform (formerly Terraform Cloud)?

HashiCorp's hosted service that stores state, runs plan and apply remotely, connects to version control so pull requests trigger plans, manages variables and secrets, and adds a private module registry, policy checks with Sentinel or OPA, and drift detection. Terraform Enterprise is the self-hosted version.

Team practice and scenarios

Q23. Someone deleted a resource manually. What happens on the next plan?

The refresh finds the object missing, so the plan shows it will be created again to match the configuration. If the deletion was intentional, remove it from the code (or from state) instead. Either way, find out why it was changed outside Terraform.

Q24. The state is locked and nobody is running Terraform. What do you do?

Make sure no pipeline or colleague is still running (a crashed run can leave the lock behind), then run terraform force-unlock <LOCK_ID> using the ID from the error message. Never force-unlock while an apply is in progress, because two writers can corrupt the state.

Q25. A plan shows that a database will be destroyed and recreated. What do you do?

Stop and read why: the plan marks the attribute that "forces replacement", such as a changed name, engine or subnet group. Find a way to make the change in place, use a moved block if only the address changed, or plan a controlled migration with a backup. Add prevent_destroy to critical resources so this can never be applied by accident.

Q26. How do you run Terraform safely in a CI/CD pipeline?

Run fmt, validate, a linter (tflint) and a security scanner (Checkov or Trivy) on every pull request; run plan and post it to the pull request; apply only after approval, using the saved plan file so exactly the reviewed changes are applied. The pipeline authenticates with OIDC roles rather than stored keys, and each environment has its own state and permissions.

Q27. How would you structure Terraform code for a large organisation?

Reusable, versioned modules in their own repositories or a registry; separate root configurations per environment and per area (network, platform, applications) so each state stays small and a mistake has a limited blast radius; outputs shared through remote state data sources or parameter stores; and policy as code (Sentinel, OPA or Checkov) to enforce tagging, regions and encryption.

Learn Terraform hands-on

Knowing the answers is half the job; interviewers also ask what you have built. Technical Guftgu's Terraform training is taught in Hindi with live classes, labs and project work, plus resume and mock-interview help. We do not promise or guarantee job placement.

More interview questions

TECHNICAL GUFTGU

© 2026 Technical Guftgu. All rights reserved.