Terraform & IaC Interview Questions and Answers
Providers, state, modules, workspaces and drift management.
Whether you are preparing for entry-level Terraform & IaC interview questions for freshers or senior software engineer interview questions addressing concurrency, scalability, and system architecture, this track provides peer-reviewed model answers with syntax walkthroughs, edge cases, and practical interview tips.
1 What is Terraform state and why should it be remote? Easy
Terraform state is a JSON file that maps your configuration to real infrastructure. It stores resource IDs, attribute values, and dependency metadata so Terraform knows what exists and how to change it.
Why remote state:
- Team collaboration: everyone reads and writes the same state instead of overwriting each other.
- Locking: backends such as S3 with DynamoDB, Azure Blob, or Terraform Cloud lock state during operations, preventing concurrent corruption.
- Durability and security: state often contains secrets, so store it in encrypted, access-controlled storage with versioning.
- CI/CD: pipelines can use the same state from any runner.
terraform {
backend "s3" {
bucket = "my-tf-state"
key = "app/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "tf-locks"
encrypt = true
}
}
Never commit state to Git. Back it up and treat it as sensitive.
2 What is the difference between count and for_each? Easy
Both create multiple instances of a resource, but they track them differently.
- count takes a number and identifies instances by index, such as resource[0] and resource[1]. Removing an item from the middle shifts indexes and causes Terraform to recreate or destroy the wrong resources.
- for_each takes a map or set and identifies instances by stable keys, such as resource["web"] and resource["api"]. Adding or removing one key only affects that instance.
resource "aws_instance" "app" {
for_each = toset(["web", "api"])
ami = "ami-12345678"
instance_type = "t3.micro"
tags = { Name = each.key }
}
Use for_each when identity matters, such as per-environment or per-name resources. Use count for identical, interchangeable resources or simple conditional creation with count = var.enabled ? 1 : 0. Note that for_each keys must be known at plan time.
Frequently Asked Questions About Terraform & IaC Interviews
What do hiring managers evaluate in Terraform & IaC technical rounds?
Technical interviewers look for foundational fluency, idiomatic syntax, clarity when communicating complex logic, and awareness of performance trade-offs (e.g. memory footprint, render performance, and network latency) in production environments.
What are the best interview tips for practicing Terraform & IaC questions?
Use active recall: summarize each answer in your own words before revealing the model solution. Focus on explaining why a certain approach is chosen rather than just memorizing code syntax.