Terraform Interview Questions
Core Concepts
Q: What is Terraform and how does it work?
Terraform is an Infrastructure as Code (IaC) tool by HashiCorp that lets you define, provision, and manage infrastructure across cloud providers using a declarative language (HCL — HashiCorp Configuration Language).
How it works — 3-step workflow:
terraform init — initialises working directory, downloads providers and modulesterraform plan — compares desired state (config files) with current state (state file) → shows what will be created/changed/destroyedterraform apply — executes the plan, updates real infrastructure, updates state fileCore files:
*.tf — configuration files (resources, variables, outputs, providers)terraform.tfstate — state file (maps config to real resources).terraform.lock.hcl — provider version lock fileQ: What is Terraform state? Why is it important? How do you manage it in a team?
State file (terraform.tfstate) maps Terraform resources to real infrastructure. Without it, Terraform can't know what already exists.
Problems with local state in teams:
Remote state (best practice):
S3 stores state, DynamoDB provides locking (prevents simultaneous applies).
Alternatives: Terraform Cloud, GCS (GCP), Azure Blob Storage.
Q: Explain Terraform modules.
Modules are reusable, encapsulated sets of Terraform configurations. Every Terraform configuration is a module — the working directory is the "root module."
Benefits: DRY code, tested configurations, team-wide standards, versioning.
Q: What are Terraform workspaces?
Workspaces allow multiple state files in a single configuration. Good for managing multiple environments (dev, staging, prod) with same code.
Limitation: All workspaces share same backend bucket — one compromise affects all. Many prefer separate directories or separate backends for strict environment isolation.
Q: Difference between terraform taint, terraform import, terraform refresh?
terraform taint (deprecated, use -replace): Marks resource for destruction and recreation on next apply. Used when resource is in bad state.
terraform import : Brings existing infrastructure under Terraform management WITHOUT recreating it. Adds to state file only — does NOT generate config.
terraform refresh (deprecated): Updates state file to match real infrastructure. Now incorporated into plan/apply with -refresh-only flag.
Q: Explain count, for_each, and dynamic blocks.
count: Creates N copies of a resource (indexed 0,1,2...).
for_each: Creates one resource per map key or set element (named, not indexed).
Prefer for_each over count — removing middle item from count list shifts indices and destroys/recreates resources.
dynamic blocks: Generate repeated nested blocks.
Q: What are data sources in Terraform?
Data sources let Terraform read existing infrastructure (not managed by this Terraform config) to use in your configuration.
Common data sources: aws_ami, aws_vpc, aws_subnet, aws_route53_zone, aws_caller_identity.
Q: What is terraform plan -out and why should you use it?
Why: In CI/CD pipelines, you want the exact plan that was reviewed to be what gets applied — not a new plan that might differ if infrastructure changed between plan and apply.
Q: How do you handle sensitive values in Terraform?
Best practices:
sensitive = true on variables/outputs.tf files.gitignore to exclude terraform.tfvars with sensitive valuesQ: Explain Terraform lifecycle meta-arguments.
prevent_destroy = true is excellent for protecting production databases.
Q: How do you structure a large Terraform codebase?
Flat structure (small projects):
Directory-per-environment:
Terragrunt for DRY: Reduces repetition when same module used across environments with different variables.
Q: What's the difference between depends_on and implicit dependencies?
Implicit (preferred): Terraform automatically detects dependencies when you reference resource attributes.
Explicit (depends_on): When there's a hidden dependency not expressed in attributes.
Only use depends_on when implicit dependencies can't capture the relationship.

