Article written by Shashi Kadapa, under the guidance of Neeraj Jhawar, a Senior Software Development Manager and Engineering Leader. Reviewed by Mrudang Vora, an Engineering Leader with 15+ years of experience.
Terraform interview questions at top technology firms are not about definitions or reciting what the different commands are. Interviewers evaluate whether you have worked with the application, managed remote state at scale, debugged a corrupted state file, and designed a module structure for a large codebase.
The Terraform interview questions guide presents actual questions asked in interviews. Terraform interview questions and answers in this guide are arranged for beginner, intermediate, and advanced levels.
Terraform interview questions and answers in this guide cover several key topics such as IaC fundamentals, core commands, state management, modules, workspaces, security, anti-patterns, and real troubleshooting scenarios.
This section presents foundational concepts and Terraform interview questions asked in all Terraform interviews.
Terraform is a tool that lets you describe your infrastructure in code, so you can create and manage resources in a clear, repeatable way. It removes the need for manual setup, helping teams avoid mistakes and keep environments consistent across development, testing, and production.
Infrastructure as Code (IaC) means setting up and managing infrastructure through code rather than doing it manually, step by step. Terraform follows this approach by letting you define what you want in simple config files and then taking care of creating or updating resources to match it.
Terraform and CloudFormation help you manage infrastructure with code. However, Terraform works across multiple cloud providers, while AWS CloudFormation is built specifically for AWS and fits closely within its ecosystem.
The following table presents differences between Terraform and CloudFormation.
| Feature | Terraform | AWS CloudFormation |
|---|---|---|
| Cloud support | Works with multiple cloud providers and services | Designed only for AWS services |
| Language | Uses HCL, which is simple and easy to understand | Uses JSON or YAML templates |
| State management | Keeps a separate state file to track changes | AWS handles the state internally |
| Reuse model | Uses modules to reuse and organize code | Uses nested stacks for reuse |
| Drift detection | Needs manual checks or extra setup | Has built-in drift detection |
| Community ecosystem | Large community with plenty of shared resources | More AWS-focused, smaller in comparison |
| When to choose it | Good choice for multi-cloud or flexible setups | Ideal when everything runs on AWS |
Terraform is used to create and manage infrastructure like servers and networks, while Ansible is used to configure and set up what runs inside those systems. Terraform builds the environment, and Ansible takes care of configuring it.
Terraform has a set of core CLI commands to manage infrastructure as code across the lifecycle. The following table presents Terraform core commands.
| Command | What It Does | When You Run It |
|---|---|---|
| terraform init | Prepares your project by setting up providers and backend | At the start of a project or after setup changes |
| terraform plan | Shows what Terraform is about to change before doing anything | When you want to review changes safely |
| terraform apply | Applies the changes and updates your infrastructure | After confirming the plan looks correct |
| terraform destroy | Removes everything Terraform has created | When you no longer need the infrastructure |
| terraform validate | Checks if your configuration is written correctly | While developing or before running a plan |
| terraform fmt | Cleans up and standardizes your code formatting | During development to keep code readable |
| terraform show | Displays the current state or details of a plan | When you want to inspect what exists |
| terraform state list | Lists all resources Terraform is tracking | When checking or debugging your state file |
| terraform import | Adds existing resources into Terraform management | When bringing manual resources under control |
| terraform output | Prints useful values defined in your setup | After apply to get things like IPs or URLs |
A Terraform provider is what connects Terraform to a specific platform, like a cloud service or API. It works behind the scenes by turning your configuration into API calls so resources can be created, updated, or removed.
The Terraform state file is a snapshot of the resources Terraform is currently managing and their real-world details. It exists so Terraform can keep track of what’s already created and how it matches your configuration. This allows it to make safe, accurate changes instead of guessing or duplicating resources.
terraform plan lets you see what changes are going to happen before anything is touched, so you can review safely. terraform apply actually goes ahead and makes those changes in your real infrastructure.
HashiCorp Configuration Language (HCL) is a clean, easy-to-read language used to describe infrastructure in tools like Terraform. It helps you write configurations in a way that feels natural to read while still being precise enough for systems to follow.
The following table presents the HCL block types.
| Block Type | Purpose | Example |
|---|---|---|
| terraform {} | Defines basic settings Terraform needs, like versions and providers | terraform { required_providers { aws = { source = “hashicorp/aws” } } } |
| provider {} | Tells Terraform which cloud or service to connect to and how | provider “aws” { region = “ap-south-1” } |
| resource {} | Describes the infrastructure you want to create or manage | resource “aws_instance” “web” { ami = “ami-123” instance_type = “t2.micro” } |
| variable {} | Lets you pass and reuse values to make configs flexible | variable “instance_type” { default = “t2.micro” } |
| output {} | Displays key information after the run completes | output “instance_ip” { value = aws_instance.web.public_ip } |
| data {} | Reads details from existing resources instead of creating new ones | data “aws_ami” “latest” { most_recent = true } |
| locals {} | Holds temporary values to simplify and reuse expressions | locals { env = “dev” } |
Terraform variables are placeholders that let you make your configuration reusable and easy to adjust without editing the main code. You can pass values using a .tfvars file, command line (-var), environment variables, or by setting defaults inside the variable block.
Terraform variables are like inputs that let you tweak your setup without changing the main configuration every time. You can give them values using a .tfvars file, command line options, environment variables, or simple defaults in the code itself.
The Terraform Registry is a place where you can find ready-made modules and providers created by others. It saves time by letting you reuse existing building blocks instead of writing everything from scratch.
This section presents intermediate Terraform interview questions and answers. Topics covered for the Terraform interview questions and answers are on state management, modules, workspaces, and real infrastructure patterns.
Remote state means keeping the Terraform state file in a shared place instead of on one person’s machine. It lets everyone on the team work from the same, latest view of the infrastructure. This helps avoid clashes, keeps things consistent, and reduces the risk of overwriting each other’s changes.
State locking makes sure only one person or process can update the Terraform state at a time.
If it doesn’t work, changes can overlap and leave your state or infrastructure in a messy, inconsistent state.
Terraform modules are reusable chunks of infrastructure code that bundle related resources into one place. They save time by letting you write something once and use it in multiple projects instead of repeating the same setup.
Using modules keeps your code cleaner, more consistent, and much easier to manage as your infrastructure grows.
The root module is your main working folder — it’s the one Terraform runs when you execute commands. A child module is a smaller, reusable piece that the root module calls to handle specific parts of the setup.
Data sources in Terraform are used to fetch details about things that already exist, without managing or creating them. You use them when your setup needs to refer to existing resources, like pulling IDs or configurations into your code.
terraform import lets you bring an existing resource under Terraform’s control by adding it to the state. You use it when something is already created outside Terraform, and you want to manage it without rebuilding it.
Terraform workspaces are a way to keep separate state files for the same configuration, so one setup can represent multiple environments. They’re handy when you want to reuse the same code for environments like dev, test, and prod without copying everything.
The following table compares workspaces and separate directories.
| Approach | What It Isolates | When to Use It | When to Avoid It |
|---|---|---|---|
| Terraform Workspaces | Keeps only the state separate while using the same code | Good when your environments are almost the same and you just need a simple way to switch between them | Not ideal if environments start needing different configs or tighter control—it can become confusing |
| Separate Root Modules / Directories | Separates everything—code, variables, and state | Best when environments have clear differences or need strong isolation and independent management | Overkill if everything is identical, since you’ll end up repeating the same setup in multiple places |
depends_on tells Terraform to create or update one resource only after another one is done. You add it when Terraform can’t naturally figure out the dependency, but the order still matters.
The lifecycle meta-argument in Terraform gives you control over how a resource behaves during creation, updates, and deletion. The following table presents lifecycle settings.
| Setting | What It Does | Example Use Case |
|---|---|---|
| create_before_destroy | Builds the new resource first, then removes the old one | Useful when you want to avoid downtime during updates, like replacing a server |
| prevent_destroy | Stops Terraform from deleting a resource | Helps protect important resources such as production databases |
| ignore_changes | Skips tracking changes for certain attributes | Handy when some values are updated outside Terraform, like tags or timestamps |
| replace_triggered_by | Forces a resource to be recreated when something else changes | For example, redeploy a server when its configuration or image is updated |
Terraform can mark certain values as sensitive so they don’t get displayed in the terminal output. Even then, those values are still stored in the state file, so keeping that state secure is very important.
In practice, teams often use external secret managers like Vault or cloud secret services instead of putting secrets directly in Terraform.
output in Terraform is used to show or expose useful values after resources are created, like an IP address or resource ID. You can then pass these values into other modules so different parts of your setup can work together.
count and for_each are both used to create multiple instances of a resource, but they work in slightly different ways. The following table compares count and for_each.
| Feature | count | for_each |
|---|---|---|
| Index type | Uses simple numbers like 0, 1, 2 | Uses meaningful keys like names or map values |
| Behaviour when list changes | If the order changes, resources may get recreated | More stable because each resource is tied to a specific key |
| Best for | When you just need multiple similar resources without much variation | When each resource needs a unique identity or config |
| Common pitfall | Reordering items can accidentally recreate resources | Slightly harder to use since you need a map or set instead of a list |
This section presents advanced Terraform interview questions. The questions focus on architecture-level judgment, multi-cloud strategy, security, and production Terraform patterns. The Terraform interview questions and answers are asked in senior and principal-level interviews.
In big organisations, Terraform code is usually organised into reusable modules for common infrastructure, while each team has its own separate root configuration for what they manage.
Remote state with locking is used so multiple teams don’t overwrite each other’s work.
Clear ownership is defined per team or environment, along with access controls to keep things safe. Standards like naming rules and CI/CD pipelines help keep everything consistent and easy to maintain.
Drift is when your real infrastructure gets changed outside of Terraform, so it no longer matches your code. You usually spot it by running terraform plan, which highlights any differences between state and reality.
To resolve it, you either apply the Terraform changes to fix it or update your configuration/state if the new setup is intended.
Keep one Terraform setup, but connect it to different cloud providers by configuring each provider where needed. Break your infrastructure into small, reusable modules so each cloud part stays organized and easy to manage.
Store state remotely and use variables or workspaces to handle differences between environments and providers.
We typically separate each cloud (AWS, Azure, GCP) into its own provider configuration so they don’t interfere with each other. Common patterns like tagging, naming, and networking are put into shared Terraform modules so we don’t repeat code everywhere.
To keep things safe and manageable, we also maintain separate state files per environment and sometimes per provider.
We write rules that check Terraform plans before anything gets applied, using tools like Sentinel or Open Policy Agent (OPA). These rules help enforce things like mandatory tags, approved resource types, or extra approvals for production changes.
We plug them into the CI/CD pipeline so every change is automatically validated before it reaches the cloud.
Provisioners are basically a way to run scripts or commands on a resource after it’s created in Terraform. They’re best kept as a last resort, mainly for small setup tasks when tools like cloud-init or Ansible can’t handle it cleanly.
The downside is that they can make your setup less predictable and harder to manage because they introduce external steps outside Terraform’s normal flow.
We start by checking the provider changelog to see what’s changed and what might break in our existing setup. Then we try the upgrade in a non-production environment first to make sure everything still works as expected.
We also pin provider versions and move up in small steps so we don’t introduce sudden surprises in production.
terraform_remote_state lets one Terraform setup read outputs from another state file, usually stored in a remote backend like S3 or Terraform Cloud. It’s useful when different projects or teams manage infrastructure separately but still need to share values like VPC IDs or subnet info.
We use it instead of direct module outputs when stacks are independent, but we try not to overuse it since it can create hidden dependencies.
We usually start with terraform validate to catch basic mistakes, then use terraform plan to see exactly what changes will happen. For more confidence, we run automated tests with tools like Terratest or kitchen-terraform to check real infrastructure behaviour.
Before going live, we always try out changes in a staging or sandbox environment to make sure nothing breaks in production.
The Terraform backend is basically where your state file lives, like locally, in S3, or in Terraform Cloud, so everyone works with the same source of truth. When you migrate a backend, you’re just moving that state from one place to another without rebuilding any infrastructure.
This is usually done with terraform init -migrate-state, which shifts the state safely so nothing gets lost or recreated.
This section presents Terraform interview questions and answers that evaluate what candidates have seen go wrong and how they fixed it.
1. Hardcoding secrets in configuration files
2. Using local state in team environments
3. Giant root modules
4. Overusing provisioners
5. Using the wrong looping construct for named resources
6. Not pinning provider versions
7. Direct state manipulation without a plan
8. Misusing workspaces as full environment isolation
9. No drift detection in CI
10. Committing state files to version control
This section presents Terraform interview questions on scenario testing. Interview questions on Terraform evaluate if you can diagnose and resolve real problems.
The steps are:
The steps are:
The steps are:
The steps are:
The steps are:
When two engineers trigger deployments at the same time, Terraform blocks one run because the state is locked. If locking is not properly enforced, you can end up with failed applies or conflicting changes to infrastructure.
Terraform core concepts reference table.
| Concept | What it is | Key detail |
|---|---|---|
| Terraform | Tool for managing infrastructure as code | You describe what you want, it builds it for you |
| Provider | Connector to a cloud or service | Each platform like AWS or Azure needs its own provider |
| Resource | A single piece of infrastructure | Things like servers, databases, or networks |
| State | Terraform’s memory of what it built | Keeps track of real infrastructure vs code |
| Backend | Where the state file is stored | Often remote for teams (S3, Terraform Cloud, etc.) |
| Plan | Preview of changes before applying | Shows what will change without making updates |
| Apply | Executes the planned changes | Actually creates or modifies infrastructure |
| Module | Reusable chunk of Terraform code | Helps avoid repeating the same setup |
| Variable | Input values for your config | Makes setups flexible and reusable |
| Output | Information returned after apply | Useful for passing values between modules or tools |
Terraform quick reference table
| Concept | What it is | Key detail |
|---|---|---|
| terraform init | Sets up a working directory | Downloads providers and configures backend |
| terraform plan | Shows what will change | Safe preview before any real action |
| terraform apply | Makes changes live | Creates or updates real infrastructure |
| terraform destroy | Tears everything down | Removes all managed resources |
| terraform validate | Checks config correctness | Catches syntax and basic logic issues |
| terraform fmt | Cleans up code style | Formats files consistently |
| terraform import | Brings existing resources under Terraform | Links real infra to state without recreating it |
| terraform state mv | Moves resources in state | Used when reorganizing code safely |
| terraform force-unlock | Breaks a stuck lock | Used only when state lock is stale |
| State file | Terraform’s tracking file | Maps code to real infrastructure |
| Remote backend | Shared state storage | Enables team access and locking |
| Provider | Cloud connector | Lets Terraform talk to AWS, Azure, etc. |
| Module | Reusable code block | Helps structure and reuse infrastructure |
| Workspace | Separate state environment | Commonly used for dev/test/prod separation |
| count | Creates multiple copies | Index-based scaling of resources |
| for_each | Creates multiple copies | Key-based, safer for dynamic sets |
| depends_on | Forces ordering | Manually defines resource dependency |
| lifecycle | Controls resource behavior | Can prevent destroy or ignore changes |
| sensitive = true | Hides output values | Prevents secrets from showing in logs |
| terraform_remote_state | Reads another state file | Shares outputs between separate stacks |
The Terraform interview questions and answers guide is structured for beginners, intermediate, and advanced-level developers. Terraform interview questions and answers present cover IaC fundamentals, core commands, state management, modules, workspaces, security, anti-patterns, and real troubleshooting scenarios.
Terraform interview questions test your practical code development and implementation, along with theory. To crack interview questions on Terraform, you should demonstrate practical skills in using the application. Learn to use all the commands and functions.
To crack interview questions on Terraform, read case studies extensively, practice coding, register for reputed interview preparation courses like Interview Kickstart, and take mock interviews.
Recommended Reads:
Time Zone:
Master ML interviews with DSA, ML System Design, Supervised/Unsupervised Learning, DL, and FAANG-level interview prep.
Get strategies to ace TPM interviews with training in program planning, execution, reporting, and behavioral frameworks.
Course covering SQL, ETL pipelines, data modeling, scalable systems, and FAANG interview prep to land top DE roles.
Course covering Embedded C, microcontrollers, system design, and debugging to crack FAANG-level Embedded SWE interviews.
Nail FAANG+ Engineering Management interviews with focused training for leadership, Scalable System Design, and coding.
End-to-end prep program to master FAANG-level SQL, statistics, ML, A/B testing, DL, and FAANG-level DS interviews.
Learn to build AI agents to automate your repetitive workflows
Upskill yourself with AI and Machine learning skills
Prepare for the toughest interviews with FAANG+ mentorship
Time Zone:
Join 25,000+ tech professionals who’ve accelerated their careers with cutting-edge AI skills
25,000+ Professionals Trained
₹23 LPA Average Hike 60% Average Hike
600+ MAANG+ Instructors
Webinar Slot Blocked
Register for our webinar
Learn about hiring processes, interview strategies. Find the best course for you.
ⓘ Used to send reminder for webinar
Time Zone: Asia/Kolkata
Time Zone: Asia/Kolkata
Hands-on AI/ML learning + interview prep to help you win
Explore your personalized path to AI/ML/Gen AI success
The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants
The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer
The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary
Join 25,000+ tech professionals who’ve accelerated their careers with cutting-edge AI skills
Join 25,000+ tech professionals who’ve accelerated their careers with cutting-edge AI skills
Webinar Slot Blocked
Time Zone: Asia/Kolkata
Hands-on AI/ML learning + interview prep to help you win
Time Zone: Asia/Kolkata
Hands-on AI/ML learning + interview prep to help you win
Explore your personalized path to AI/ML/Gen AI success
See you there!