,

Top 20 Terraform Interview Questions and Answers (IaC Guide)

By the HiReXL Career Team

Terraform is the most widely used infrastructure as code tool across AWS, Azure and Google Cloud, so Terraform interview questions are standard for DevOps, cloud, platform and site reliability engineering roles. This guide covers the top 20 Terraform interview questions and answers, from core workflow and state to modules, policy as code and real-world troubleshooting.

It is useful whether you are preparing for the HashiCorp Terraform Associate exam or a senior infrastructure engineer interview.

Terraform basics

1. What is Terraform and why use it?

Terraform is an open-source infrastructure as code tool by HashiCorp. You describe infrastructure in declarative HCL, and Terraform plans and applies the changes through provider APIs. It is cloud-agnostic, repeatable, reviewable in pull requests, and supports thousands of providers. OpenTofu is a community fork with the same core workflow.

2. Explain the Terraform workflow.

terraform init downloads providers and modules and configures the backend; terraform fmt and validate check code; terraform plan shows proposed changes; terraform apply makes them; terraform destroy removes managed resources.

3. What are providers?

Providers are plugins that let Terraform talk to an API, such as aws, azurerm, google, kubernetes, github or datadog. Pin provider versions in a required_providers block and commit the .terraform.lock.hcl file for consistent builds.

4. What is the difference between variables, locals and outputs?

Input variables parameterize a configuration; locals are named expressions to avoid repetition; outputs expose values to the caller, another module or remote state consumers. Mark sensitive values with sensitive = true.

5. What are data sources?

Data sources read existing information without managing it, for example looking up the latest AMI, an existing VPC, a Key Vault secret or the current account ID.

State management

6. What is the Terraform state file and why is it important?

State maps your configuration to real resources and stores metadata and attribute values. Terraform uses it to compute diffs. It can contain secrets, so it must be stored securely and never edited by hand.

7. Why use a remote backend and state locking?

A remote backend such as S3 (with native locking or DynamoDB), Azure Storage, Google Cloud Storage or HCP Terraform shares state across a team, enables encryption and versioning, and locks state so two applies cannot run at once and corrupt it.

8. What is drift and how do you handle it?

Drift is when real infrastructure differs from code because of manual changes. terraform plan (or plan -refresh-only) detects it. Either update the code to match intended changes or apply to revert them, and reduce drift by limiting console access and running scheduled drift checks.

9. How do you bring existing resources under Terraform?

Use import blocks (Terraform 1.5 and later) with terraform plan -generate-config-out to create configuration, or the terraform import command. Review the generated code and run plan until it shows no changes.

10. What do terraform state mv, state rm and moved blocks do?

state mv or a moved block renames or relocates a resource address (for example when refactoring into a module) without destroying it. state rm stops managing a resource without deleting it. removed blocks do the same declaratively.

Modules and code structure

11. What are modules and how do you design good ones?

A module is a reusable group of resources. Good modules have a clear purpose, minimal and well-documented inputs, sensible defaults, useful outputs, versioned releases in a private registry or Git tags, and examples and tests.

12. What is the difference between count and for_each?

count creates N copies indexed by number, so removing a middle item shifts addresses and can recreate resources. for_each iterates over a map or set with stable keys, which is safer for collections that change.

13. Workspaces vs. separate directories for environments?

CLI workspaces use one configuration with multiple state files, which is fine for identical environments. Most teams prefer separate root modules or directories per environment (or Terragrunt), which allow different settings, providers and access controls for dev, test and prod.

14. What are lifecycle meta-arguments?

create_before_destroy avoids downtime on replacement, prevent_destroy protects critical resources, ignore_changes skips attributes managed elsewhere, and replace_triggered_by forces replacement when something else changes.

Terraform in teams and CI/CD

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

Run fmt, validate, tflint and security scanning (Checkov, tfsec/Trivy) on pull requests, post the plan for review, require approval, then apply the saved plan from the main branch. Authenticate with OIDC to AWS, Azure or GCP instead of long-lived keys. HCP Terraform, Terraform Enterprise, Atlantis and Spacelift are common orchestrators.

16. What is policy as code with Terraform?

Policy as code evaluates plans against rules before apply, for example requiring tags, encryption, approved regions or blocking public buckets. Tools include Sentinel and OPA in HCP Terraform and Terraform Enterprise, OPA/Conftest in pipelines, and Checkov.

17. How do you handle secrets in Terraform?

Do not hardcode them. Read secrets from Vault, AWS Secrets Manager, Azure Key Vault or Google Secret Manager via data sources, pass them as sensitive variables from the pipeline, encrypt and restrict access to state, and use ephemeral resources or write-only arguments where supported so secrets are not persisted.

Scenario questions

18. Scenario: the state file is locked and the pipeline fails. What do you do?

Confirm no other apply is running and check who holds the lock. If it is a stale lock from a crashed job, release it with terraform force-unlock LOCK_ID, then re-run plan. Investigate why the job crashed and add timeouts and concurrency controls.

19. Scenario: a plan wants to destroy and recreate a production database. How do you respond?

Stop and read the plan to find which attribute forces replacement. Often it is a renamed resource (fix with a moved block), a changed immutable argument, or a provider upgrade. Use prevent_destroy on critical data stores and never apply until the plan is safe.

20. What is the difference between terraform taint, replace and refresh?

-replace=ADDRESS on plan or apply forces recreation of a resource (it replaced the deprecated taint command). -refresh-only updates state from real infrastructure without changing resources, which helps review drift safely.

How to prepare for a Terraform interview

  • Build a small multi-environment project: VPC, compute, database and IAM with modules and a remote backend.
  • Practice import, moved blocks, state commands and debugging with TF_LOG.
  • Set up a pipeline with plan on pull request, policy checks and approval before apply.
  • Review the Terraform Associate exam objectives.

Related guides: DevOps interview questions, AWS, Azure and GCP interview questions.

Frequently asked questions

Is the Terraform Associate certification worth it?

It is a good way to validate fundamentals and pass recruiter screens, especially early in a DevOps career. Hands-on module and pipeline experience matters more for senior roles.

What is the difference between Terraform and Ansible?

Terraform provisions infrastructure declaratively and tracks state. Ansible is mainly configuration management and orchestration using procedural playbooks. Many teams use both together.

Terraform vs. CloudFormation vs. Bicep?

CloudFormation and Bicep are native to AWS and Azure respectively. Terraform works across clouds and SaaS providers with one language and workflow.

Got the interview? Make sure your resume tells the same story.

Interviewers read your resume right before they talk to you. HiReXL aligns it with the job description’s keywords so your experience and your answers match. HiReXL is built by 4As Consulting, a cloud and DevOps consulting firm. Looking for your next role? Submit your resume for open positions with PanAmerica Inc.

See how HiReXL helps →