Home › Interview questions › Terraform
Terraform Interview Questions & Answers
73 Terraform interview questions in 4 levels — junior to senior, plus 16 production scenarios. Read 3 free answers with real-world analogies.
Free sample answers
What is Terraform, and what problem does Infrastructure as Code actually solve?
One-liner
Terraform lets you define your infrastructure — servers, networks, databases — in text files, so you can create, change, and track it the same reliable way you manage application code.
Real-world analogy
It's like building furniture from a written, detailed set of assembly instructions instead of eyeballing it from memory each time — anyone following the exact same instructions ends up with the exact same result, and you can always go back and check exactly what the instructions actually said.
Detailed explanation
- Without Infrastructure as Code, teams often create resources by manually clicking through a cloud provider's console, which is slow, hard to repeat exactly, and leaves no clear record of exactly what was actually done.
- Terraform reads your configuration files and figures out what needs to be created, changed, or removed to match what you've described, rather than you manually tracking every single individual change yourself.
- Because your infrastructure is defined in text files, you can review changes before they happen, track history in version control, and recreate the exact same environment reliably as many times as you actually need to.
Scenario
A team manually creates a set of cloud resources by clicking through the provider's console, and months later, nobody can confidently recall the exact settings that were originally used, since none of it was ever actually written down anywhere. Rebuilding the same environment with Terraform means every setting is explicitly captured in a file, and recreating the exact same environment again later becomes a simple, reliable command instead of a fragile guessing game.
Best practices
- Start using Terraform (or any Infrastructure as Code tool) even for small projects — the habit of defining infrastructure in text pays off quickly.
- Avoid mixing manual console changes with Terraform-managed resources, since manual changes aren't tracked and can quietly cause confusion later.
- Treat your Terraform files as the actual source of truth for your infrastructure, not just documentation describing it.
How do conditional expressions work in Terraform, and what's a common use case for them?
One-liner
A conditional expression (condition ? true_value : false_value) lets you choose between two different values based on some specific condition, commonly used to enable or configure something differently depending on a variable's actual value.
Real-world analogy
It's like a form that says 'if you're a student, fill in your school name; otherwise, fill in your employer's name' — the exact same overall form structure applies to everyone, but the specific value that actually goes into that particular field genuinely depends on your own specific situation.
Detailed explanation
- The syntax var.environment == "production" ? "large" : "small" chooses between two different values based on whether a specific variable actually equals a certain value.
- This is commonly used to vary settings, like resource sizing or a specific feature flag, based on which environment a configuration is actually currently being applied to.
- Conditional expressions can also be combined with count (setting it to either 0 or 1) as a common way to conditionally create or skip a specific resource entirely, based on some particular condition.
Scenario
A team wants their production environment to use a larger virtual machine size than their staging environment, without maintaining two genuinely separate configuration files. Using a conditional expression based on an environment variable to choose the appropriate size lets them use one single, shared configuration for both environments, with the actual size correctly adjusting itself automatically based on the specific environment in each case.
Best practices
- Use conditional expressions to avoid duplicating configuration for small, genuinely predictable differences between environments or specific situations.
- Keep conditions simple and genuinely readable — deeply nested or unnecessarily complex conditional logic can quickly become hard for anyone else to actually follow.
- Consider whether a variable with a genuinely per-environment default value might be an even simpler, more readable alternative for some specific cases.
A terraform plan shows unexpected changes to a resource you're confident you haven't actually touched. How do you investigate?
One-liner
This is almost always genuine drift — check whether someone made a manual change directly through the cloud provider's console, or whether an external, automated process modified the resource outside of Terraform entirely.
Real-world analogy
It's like a shared spreadsheet showing a value you don't remember anyone on your team actually changing — you'd check the sheet's own edit history to see who else might have touched it, rather than assuming the spreadsheet itself is simply malfunctioning somehow.
Detailed explanation
- Checking your cloud provider's own audit log for the specific resource in question often directly reveals exactly who or what actually made a recent manual change outside of Terraform.
- Some cloud services also have their own separate automated processes (like an auto-scaling feature, or a separate automated compliance-related tool) that can modify a resource's specific settings independently, entirely outside of Terraform's own knowledge.
- Once you've actually identified the cause, you can decide whether to update your Terraform configuration to genuinely match the new actual reality, or revert the external change back to what your configuration originally specified.
Scenario
A terraform plan unexpectedly shows a security group rule the team doesn't remember ever actually adding. Checking the cloud provider's own audit log reveals a colleague had manually added it directly through the console during an urgent, unrelated incident a few weeks earlier, and had simply forgotten to ever go back and actually update the corresponding Terraform configuration to properly match. The team decides the rule is genuinely still needed, and adds it properly to their configuration so it's no longer actually just invisible, unmanaged drift.
Best practices
- Check your cloud provider's own audit log as an early, first step when investigating unexpected, unexplained drift.
- Decide deliberately whether to update your configuration or revert the actual drift, rather than blindly applying without genuinely understanding the reason it happened in the first place.
- Reduce how often this happens going forward by discouraging manual console changes to anything genuinely managed by Terraform, even during a genuine, urgent incident.
A preview of the 73 questions in the Terraform guide
Junior (20)
- What is Terraform, and what problem does Infrastructure as Code actually solve?
- What is a Terraform provider, and why does almost every Terraform configuration start by declaring one?
- What is a resource block in Terraform, and how does it actually describe something you want created?
- What do terraform init, plan, apply, and destroy each actually do?
+ 16 more Junior questions, with full answers, in the guide.
Mid-level (20)
- What is a remote state backend, and why does almost every real team use one instead of local state?
- What is state locking, and what problem does it actually prevent?
- How do you create a reusable Terraform module, and what should typically go inside one?
- What are Terraform workspaces, and how are they different from just using separate directories for different environments?
+ 16 more Mid-level questions, with full answers, in the guide.
Senior (17)
- How would you design a module architecture strategy for a large organization with many teams using Terraform?
- How would you approach securing access to Terraform state files at an organizational scale?
- How would you use policy as code (like Sentinel or OPA) to enforce organizational rules across Terraform configurations?
- How would you design a CI/CD pipeline specifically for Terraform, including how you'd handle the plan and apply stages safely?
+ 13 more Senior questions, with full answers, in the guide.
Scenario (16)
- You run terraform apply and it hangs, reporting the state is locked, even though you're not aware of anyone else currently running an apply. How do you investigate and resolve it?
- A terraform plan shows unexpected changes to a resource you're confident you haven't actually touched. How do you investigate?
- You accidentally run terraform apply against production while intending to target staging. What do you do, and how do you prevent this from happening again?
- Your state file appears to be corrupted or unreadable, and terraform commands are failing. How do you recover?
+ 12 more Scenario questions, with full answers, in the guide.
Get the full DevOps / SRE Interview Guide