Terraform Interview Questions Cheat Sheet
Terraform Interview Questions Cheat Sheet
Basic Questions:-
What is Terraform, and how does it differ from other IaC tools?
Answer: Terraform is an open-source Infrastructure as Code (IaC) tool used to define, provision, and manage infrastructure across multiple
cloud providers. Unlike other IaC tools like AWS CloudFormation, Terraform is multi-cloud, meaning it supports AWS, Azure, Google
Cloud, and other providers.
Intermediate Questions:-
What are Terraform modules, and how do you create and use them?
Answer: Modules in Terraform are reusable units of infrastructure code
that encapsulate resources and can be shared or reused. A module is
typically stored in a directory with its own configuration files.
Advanced Questions:-
How do you write reusable Terraform modules? What are the best practices?
Answer: Reusable Terraform modules should be written to abstract common resources, such as
VPC, EC2, or RDS. Best practices include using input variables for customization, output variables
to expose values, and keeping module logic clean and simple.
What are dynamic blocks, and how are they used in Terraform?
Answer: Dynamic blocks allow for the generation of nested blocks in Terraform based on variables
or conditions.
Scenario-Based Questions:-
1. You have a Terraform module for deploying EC2 instances. How would you handle
deploying to multiple environments (dev, staging, prod) with minimal code
duplication?
Answer: Use Terraform workspaces, or create separate tfvars files for each environment
(e.g., dev.tfvars, prod.tfvars) to manage different configurations while reusing the
same module.
2. What would you do if you accidentally deleted the .tfstate file? How would you
recover?
Answer: If the .tfstate file is deleted, attempt to restore it from a backup. If
unavailable, use terraform import to bring resources back under Terraform
management.
3. Your team is using Terraform, but two developers simultaneously ran terraform
apply. How would you avoid this conflict?
Answer: Use remote state with state locking (e.g., using DynamoDB for S3 backend) to
prevent concurrent changes to the state file.
4. How would you migrate existing infrastructure into Terraform?
Answer: Use terraform import to import existing resources into Terraform’s state,
and then define them in Terraform configuration files.
5. A production deployment failed because of a misconfiguration in Terraform. How
would you debug and resolve it?
Answer: Run terraform plan to identify discrepancies, check the state file for
inconsistencies, and use Terraform logs for troubleshooting. Rollback if necessary.
6. How would you design a multi-region architecture in Terraform with disaster recovery
in mind?
Answer: Use modules to manage VPC, EC2, and RDS resources across multiple regions,
along with conditional logic (e.g., count or for_each) to enable failover scenarios.
7. How do you optimize Terraform configurations for large-scale infrastructure?
Answer: Use modules for reusable components, break down infrastructure into smaller
chunks, and use backend storage for managing state efficiently.
8. What would you do if a resource you want to delete in Terraform is still required by
another team’s infrastructure?
Answer: Use lifecycle blocks with prevent_destroy to protect critical resources
from accidental destruction.