Interview Q&A · questions with model answers
Terraform Interview Questions
Part of the study plan for: DevOps Engineer · Cloud Engineer · Site Reliability Engineer (SRE)
Ordered by how often they're asked. Most-asked answers are free to read in full; members see every answer and can rate their confidence on each.
-
Most asked count or for_each: when do you use each?
Use for_each (over a map or set of strings) when creating several similar resources identified by meaningful keys; use count mainly for conditionally creating zero or one resource (count =…
-
Most asked When do you need depends_on in Terraform, and when is it a code smell?
You need depends_on when Terraform cannot see the dependency from references alone — most commonly when a resource reads from something created by another resource through a mechanism outside…
-
Most asked Terraform and Ansible: how do they differ, and do you need both?
Terraform is declarative provisioning of infrastructure through APIs, tracking state to manage the full lifecycle (create, update, destroy). Ansible is primarily configuration management and…
-
Most asked Compare Terraform with CloudFormation, CDK and Pulumi.
Terraform is multi-cloud and multi-service with a huge provider ecosystem and HCL configuration; CloudFormation is AWS's native IaC service (state managed by AWS, deep AWS integration, AWS-only);…
-
Most asked Terraform or OpenTofu: what is the difference, and how would you choose?
In August 2023 HashiCorp relicensed Terraform from MPL 2.0 to the Business Source License, which restricts offering competing services. OpenTofu forked the last MPL version and is developed under…
-
How should nonprod and production Terraform configurations differ, and how do you enforce those differences?
Production typically enables Multi-AZ, deletion protection, higher instance counts, and longer backup retention. Enforce these with Terraform variable validation rules and prevent_destroy lifecycle…
-
What is the difference between an ECS task execution role and a task role, and how do you define them in Terraform?
The execution role is used by the ECS agent to pull container images and write logs. The task role is assumed by the application code inside the container to access AWS services. Both are defined…
-
How do variable validation rules work in Terraform, and when should you use them?
A validation block inside a variable block defines a condition that must be true for the value to be accepted. Terraform evaluates validations during plan and rejects invalid values with a custom…
-
What is the difference between variables, locals and outputs?
Input variables are a module's parameters (with types, defaults, descriptions, validation and sensitive); locals are named expressions computed inside a module to avoid repetition; outputs expose…
-
What are data sources, and when should you use them?
Data sources read information that exists outside the current configuration or state: the latest AMI, an existing VPC, an IAM policy document, a secret, outputs published by another team. They let…
-
What is the difference between terraform refresh, plan -refresh-only and apply -refresh-only?
terraform refresh (deprecated) updated state to match real infrastructure without showing what would change. plan -refresh-only shows how the state would change if updated to match reality, and…