Terraform Interview Questions
Terraform Interview Questions
Introduction
Terraform is one of the most popular Infrastructure as Code (IaC) tools used by DevOps
engineers to automate and manage infrastructure. Below is a comprehensive list of
Terraform interview questions categorised by difficulty level, along with some explanation to
better prepare for technical interviews.
● Providers: Interface between Terraform and the infrastructure being managed (e.g.,
AWS, Azure, GCP).
● Resources: Define the infrastructure objects like EC2 instances, VPCs, etc.
● Modules: Logical groupings of resources used to create reusable code.
● State: Stores the status of your infrastructure to map your configuration with the
real-world resources.
Providers are plugins responsible for interacting with APIs of cloud platforms or other
services. For example, AWS, Azure, and Kubernetes have their own providers.
terraform init initializes the working directory containing Terraform configuration files. It
downloads necessary provider plugins and prepares the environment for subsequent
commands.
The state file (terraform.tfstate) tracks the current state of the infrastructure. It helps
Terraform determine what changes need to be made in order to achieve the desired state.
● terraform plan: Shows the execution plan by outlining the changes Terraform will
make without actually applying them.
● terraform apply: Executes the plan and applies the infrastructure changes
described in the configuration.
Variables allow the configuration to be dynamic and reusable. You define variables using
variable blocks, and you can pass values through:
Remote state allows Terraform to store its state file in a remote location (e.g., S3, GCS,
Azure Blob Storage). It enables collaboration across teams and ensures the state file is
backed up securely.
Q9. Explain the use of terraform destroy.
Terraform automatically handles dependencies using a Directed Acyclic Graph (DAG). You
can also explicitly specify dependencies using the depends_on attribute in the resource
block.
A Terraform module is a reusable piece of code that groups resources together. Modules
help organize complex configurations, make the code more readable, and promote code
reusability.
Sensitive values such as API keys or database passwords should be stored securely.
Methods to manage secrets include:
● Using environment variables or secret managers (e.g., AWS Secrets Manager, Azure
Key Vault).
● Utilizing Terraform’s built-in sensitive type to mark variables as sensitive.
● Encrypting state files if stored remotely.
Yes, Terraform is cloud-agnostic and can handle multi-cloud environments. You can define
resources for different providers (AWS, Azure, GCP, etc.) in the same configuration, and
Terraform will orchestrate the infrastructure across multiple clouds.
Q14. What is a Terraform workspace and when would you use it?
Using Terraform, you can create two identical environments (blue and green). Route traffic to
the green environment, and after verifying the new version works, switch traffic to the blue
environment. This can be managed via load balancers, and Terraform can orchestrate
infrastructure changes.
● Modify the configuration to the previous state and run terraform apply.
● If using version control, revert to the previous commit and apply the changes.
● Manually restore resources using the state file.
Q18. Your team needs to track infrastructure costs for each project. How
would you achieve this using Terraform?
You can tag resources with metadata such as project name, environment, or owner. Cloud
providers (like AWS) allow cost tracking based on tags. Terraform can automate the process
of adding these tags to resources.
You can integrate Terraform with CI/CD tools (e.g., Jenkins, GitLab CI, CircleCI) by:
Drift occurs when changes are made directly in the infrastructure, bypassing Terraform. To
detect drift:
● Run terraform plan regularly, which will show any discrepancies between the
state file and the actual resources.
● Implement automation to trigger drift checks on a schedule.
5. Best Practices in Terraform
Q21. What are some best practices when writing Terraform code?
Conclusion
These questions provide a solid foundation for any Terraform interview. Being able to explain
the concepts, showcase real-world experience, and understand best practices is crucial.
Ensure to practice these and relate them to your own use cases for a successful DevOps
interview.