Terraform/OpenTofu are core tools in many DevOps teams.
Even if you are using Terraform daily, you might struggle to come up with meaningful answers to some of these questions at interviews. Don’t let that happen. Practice and be prepared.
Here we go.
Core Concepts & Architecture
1. How does Terraform differ from Ansible or CloudFormation?
- The intent: Testing if you understand the right tool for the job.
- The answer: Terraform is an Infrastructure as Code (IaC) tool focused on declarative provisioning across multiple cloud providers. AWS CloudFormation does the same but is locked strictly to the AWS ecosystem. Ansible is primarily a configuration management tool—meaning Terraform is best for building the servers and network infrastructure, while Ansible is best used afterward to configure the software and OS inside those servers.
2. Explain the core Terraform workflow.
- The intent: Verifying foundational hands-on experience.
- The answer: The standard deployment workflow relies on four key commands:
terraform init: Initializes the working directory, downloads provider plugins, and sets up the backend.terraform plan: Compares the desired code to the actual state and outputs an execution plan of what will change.terraform apply: Executes the plan to provision, modify, or delete the real-world resources.terraform destroy: Tears down all managed infrastructure.
State Management & Collaboration
3. What is the state file (terraform.tfstate) and why is it critical?
- The intent: State is the brain of Terraform. Misunderstanding it is an instant red flag in an interview.
- The answer: The state file acts as a local database that maps your real-world infrastructure to your configuration code. It tracks resource dependencies and metadata. Because it often stores sensitive information (like database passwords or API keys in plain text), it must be secured and should never be committed to a local Git repository.
4. How do you manage state in a multi-engineer team?
- The intent: Checking your enterprise-level experience. Using local state files on a team causes massive conflicts.
- The answer: You must use a Remote Backend (like AWS S3, Azure Blob Storage, or Terraform Cloud) to centralize the state file. Crucially, you must also enable State Locking (If using AWS S3, it natively supports state locking) to prevent multiple engineers or CI/CD pipelines from modifying the state simultaneously, which prevents state corruption.
HCL & Coding Practices
5. What is the difference between count and for_each?
- The intent: A classic “gotcha” question that proves you’ve actually written production Terraform code.
- The answer: Both are used to dynamically create multiple instances of a resource, but they behave differently.
countuses a numerical list index. If you delete an item in the middle of acountlist, all subsequent resources shift index, causing Terraform to destroy and recreate them.for_eachuses a map or set of strings with unique keys, allowing you to add or remove specific resources cleanly without impacting the others.
6. What are Terraform Modules and why do we use them?
- The intent: Testing your ability to write scalable, DRY (Don’t Repeat Yourself) code.
- The answer: Modules are self-contained, reusable packages of Terraform configurations. They allow teams to standardize infrastructure (e.g., building a secure, compliant “Company VPC” module) and deploy it repeatedly across different environments (dev, staging, prod) simply by passing in different input variables.
Real-World Scenarios
7. How do you handle secrets (like API keys or passwords) in Terraform?
- The intent: Assessing your knowledge of DevSecOps and security best practices.
- The answer: Emphasize that you never hardcode secrets in
.tffiles. Instead, pass them securely at runtime using environment variables (e.g.,TF_VAR_db_password), or better yet, use data sources to fetch them dynamically from a secrets manager like AWS Secrets Manager, Azure Key Vault, or HashiCorp Vault during the apply phase.
8. What is Terraform drift, and how do you resolve it?
- The intent: Day-to-day operations. Someone manually changed a setting in the cloud console, bypassing your code.
- The answer: Drift occurs when the real-world infrastructure diverges from the Terraform state. Running
terraform plandetects this drift. To resolve it, you either update your Terraform code to match reality (if the manual change was necessary and permanent), or you runterraform applyto overwrite the manual change and force the infrastructure back to the desired state defined in your code.
9. How do you force a specific resource to recreate?
- The intent: Testing if your knowledge is up-to-date with modern versions of the tool.
- The answer: Historically, engineers used the
terraform taintcommand to mark a resource for recreation on the next apply. However, you should point out thattaintis deprecated. The correct modern approach is to use theterraform apply -replace="resource_address"flag, which forces recreation cleanly in a single step.