Terraform Training in Kolkata for working professionals

25 critical Terraform interview questions categorized by domain, complete with detailed, consultant-level answers.

As an AWS and Terraform professional, I often remind candidates that cracking a senior DevOps or Cloud Engineer interview in 2026 requires moving beyond basic syntax. Interviewers want to see your battle scars—how you handle state file corruption, secure sensitive data, and integrate Terraform within enterprise AWS environments.

To help you prepare, I have compiled 25 critical Terraform interview questions categorized by domain, complete with detailed, consultant-level answers.


Part 1: Core Concepts & Architecture

1. What is the fundamental difference between Declarative (Terraform) and Imperative (CLI/Scripts) infrastructure management? Answer: Imperative tooling (like Bash/AWS CLI) tells the system exactly how to do something step-by-step (e.g., “create this VPC, then create this subnet”). If a step fails, the script breaks. Declarative tooling (Terraform) defines the end state (e.g., “I need a VPC with these subnets”). Terraform calculates the dependency graph and figures out the “how.” If a resource already exists and matches the state, Terraform does nothing (idempotency).

2. Explain the exact lifecycle of a terraform apply. Answer:

  1. Initialization: Loads providers and modules.
  2. Refresh: Queries AWS to get the current live state of resources.
  3. Plan: Compares the desired state (code) with the current state (refreshed) and creates an execution plan.
  4. Approval: Prompts the user for confirmation.
  5. Apply: Executes the API calls to AWS in the correct topological order based on the dependency graph.
  6. State Update: Writes the updated AWS metadata back into the terraform.tfstate file.

3. How does Terraform map dependencies in your code without you explicitly defining them? Answer: Terraform builds a Directed Acyclic Graph (DAG). It implicitly maps dependencies by analyzing references in your code. For example, if an EC2 instance references a Security Group ID (vpc_security_group_ids = [aws_security_group.web.id]), Terraform automatically knows the Security Group must be created before the EC2 instance.

4. Why are Provisioners considered an anti-pattern, and what should you use instead? Answer: Provisioners (like local-exec or remote-exec) push configuration after a resource is created. This breaks the idempotency model. If the provisioner fails, Terraform marks the resource as “tainted” but the resource still exists in AWS. Instead, you should use AWS native user-data, AWS Systems Manager (SSM), or configuration management tools like Ansible triggered by a CI/CD pipeline.

5. What is the difference between terraform workspace and Terraform Environments? Answer: terraform workspace is a feature within a single configuration that uses the same backend but switches state files using a prefix. It is great for quick testing. “Environments” is an architectural pattern where you have entirely separate directories (e.g., /env/dev, /env/prod), each with its own backend configuration. In enterprise AWS setups, separate directories are highly preferred to prevent cross-environment state contamination.


Part 2: State Management (The Most Critical Domain)

6. Why is the Terraform State file considered a security liability, and how do you mitigate it? Answer: The state file stores all AWS resource attributes in plain text by default, including sensitive data like RDS passwords, IAM secrets, and private IPs. Mitigation:

  1. Enable Server-Side Encryption (SSE-KMS) on the S3 backend bucket.
  2. Use the sensitive = true argument in variable/output blocks to hide values in CLI logs.
  3. Never store secrets in TF variables; fetch them dynamically at runtime via aws_secretsmanager_secret_version.

7. How exactly does State Locking work in an AWS S3 backend? Answer: When you run terraform apply, Terraform puts a lock in a DynamoDB table associated with your S3 backend. The lock contains the lock ID, the user who locked it, and a timestamp. If another engineer tries to run an apply, Terraform queries DynamoDB, sees the lock, and exits with an error, preventing concurrent state writes and corruption.

8. What is State Drift, and how do you detect and remediate it? Answer: State drift occurs when the actual AWS infrastructure differs from what is recorded in the terraform.tfstate file (usually due to manual changes via the AWS Console).

  • Detect: Run terraform plan.
  • Remediate: If the console change was a mistake, run terraform apply to revert it. If the console change was intentional, you must manually update the state file using terraform state rm (to remove the old) and terraform import (to add the updated resource), or update the code to match the console.

9. Explain the difference between terraform state mv and terraform import. Answer:

  • terraform import is used to bring an existing AWS resource that is not currently tracked by Terraform into the state file.
  • terraform state mv is used to move a resource that is already tracked by Terraform from one state file to another, or to rename it within the same state file (e.g., moving a resource from a root module to a child module).

10. You accidentally ran terraform destroy and lost your state file. Can you recover your infrastructure? Answer: No. The state file is the single source of truth. Without it, Terraform does not know the AWS resource IDs, ARNs, or configurations of the infrastructure it previously built. The AWS resources are now “unmanaged” (orphaned). You must use terraform import to manually map every single surviving AWS resource back into a new state file based on your code.


Part 3: Modules & Reusability

11. What is the difference between a Root Module and a Child Module? Answer: The Root Module is the directory where you run terraform init/plan/apply. It contains the backend configuration and calls other modules. Child Modules are separate directories packaged with .tf files that define reusable infrastructure components (e.g., an “EC2 module”). Root modules call child modules using module blocks.

12. Compare count and for_each for resource iteration. When should you not use count? Answer: count creates resources based on an integer. for_each creates resources based on a map or set of strings. You should never use count if there is a chance an item in the middle of your list will be removed. Because count relies on array indexes (count.index), removing index 1 will shift index 2 down to 1, causing Terraform to destroy and recreate the resource associated with index 2. for_each uses unique keys, so removing an item only destroys that specific item.

13. How do you pass sensitive data (like database passwords) into a child module without exposing it in the state file? Answer: Do not pass the secret as a variable. Instead, pass an IAM Role ARN or a Secret ARN as a standard string variable to the child module. Let the child module use the aws_secretsmanager_secret_version data source to fetch the password dynamically at plan/apply time.

14. What are Dynamic Blocks, and what is their main drawback? Answer: Dynamic blocks allow you to dynamically construct repeatable nested blocks inside a resource (like multiple ingress blocks in a Security Group) using a for loop. Drawback: They cannot use for_each or count directly on the dynamic block’s iterator to reference resources created within the same resource. They are also harder to read and debug than explicitly writing out the blocks if the logic is highly complex.

15. How do you pin module versions in an enterprise environment, and why is it critical? Answer: You pin module versions using the version argument in the source block (e.g., version = "~> 1.2"). This is critical because Terraform automatically downloads modules during init. If the module maintainer pushes a breaking change to the main branch and you aren’t pinning versions, your next terraform init will download the broken code, potentially taking down production AWS infrastructure.


Part 4: AWS-Specific Terraform Scenarios

16. How do you securely authenticate Terraform running in an AWS CI/CD pipeline (e.g., GitHub Actions) without long-lived Access Keys? Answer: Use OIDC (OpenID Connect). You configure an IAM Identity Provider in AWS that trusts GitHub’s OIDC issuer. You then create an IAM Role with a trust policy that allows the specific GitHub repo to assume it. In the GitHub Action, you use the aws-actions/configure-aws-credentials action with the OIDC token. This grants temporary, scoped credentials with zero long-lived keys to leak.

17. How do you achieve a Zero-Downtime Deployment for an EC2 ASG behind an ALB using Terraform? Answer: You utilize the create_before_destroy lifecycle block in the aws_autoscaling_group resource. Terraform will create the new ASG, wait for it to be fully provisioned, attach it to the ALB target group, and wait for health checks to pass before detaching and destroying the old ASG.

18. You need to deploy an RDS database, but your team’s Terraform code doesn’t have the VPC/Subnet code. How do you reference existing network resources? Answer: Use Terraform Data Sources. You use the aws_vpc and aws_subnets data sources, filtering by tags or VPC ID, to fetch the existing network configurations dynamically at runtime. You then pass the data.aws_subnets.this.ids to the RDS resource.

19. How do you handle AWS API Rate Limiting (Throttling) during a massive terraform apply? Answer: AWS throttles API requests if you create too many resources too quickly. In Terraform, you handle this by:

  1. Using the provider block’s max_retries setting.
  2. Implementing targeted applies (terraform apply -target=...) to break up large batches.
  3. Adding time_sleep resources between heavy resource creations to intentionally slow down Terraform.

20. Explain how to use terraform_remote_state to share data between two completely separate AWS accounts. Answer: Account A (Network) stores its state in an S3 bucket in Account A. Account B (App) needs the VPC ID. In Account B’s code, you configure a terraform_remote_state data source. You provide an IAM Role ARN from Account B that has cross-account read access to the S3 bucket in Account A. Terraform assumes that role, reads the state file, and maps the outputs (like VPC ID) for Account B to use.


Part 5: Security, Testing & Advanced Workflows

21. What is Sentinel, and how does it differ from standard terraform validate? Answer: terraform validate only checks syntax and internal consistency (e.g., ensuring a required string is provided). Sentinel is HashiCorp’s Policy-as-Code framework. It enforces organizational rules after the plan is generated but before the apply. For example, a Sentinel policy can deny an apply if the plan shows an S3 bucket being created without encryption, regardless of whether the syntax was valid.

22. How do you structure a Terraform codebase for a massive enterprise AWS organization? Answer: I use a structured approach, often leveraging tools like Terragrunt to keep the code DRY:

  • /modules: Reusable components (VPC, EC2, RDS).
  • /environments: Broken down by account (e.g., /prod-network, /prod-app).
  • /prod-network/main.tf: Calls the VPC module and passes in production-specific variables. This strictly separates what we build (modules) from where we build it (environments).

23. How do you mock AWS resources for Terraform unit testing without spending money? Answer: Use Terratest combined with LocalStack or AWS MVP (Mock Vault Provider). You configure the AWS provider endpoint to point to a LocalStack Docker container running locally. Terratest writes Go code to deploy the TF code to LocalStack, validates the output, and then destroys it, ensuring zero AWS costs and fast execution.

24. What happens if a Terraform plan shows a change to a computed attribute that you didn’t explicitly define in your code? Answer: A computed attribute is a read-only value generated by AWS (e.g., an auto-generated API Gateway URL, or an EKS cluster endpoint). If this shows as a change in your plan, it usually means AWS updated something internally, or you are looking at a false diff. It rarely causes infrastructure destruction, but you should investigate why AWS changed that attribute to ensure it doesn’t break application dependencies.

25. Walk me through your strategy for upgrading a massive Terraform codebase from version 0.12 to 1.6+. Answer: You never jump versions. The upgrade path must be sequential:

  1. Upgrade to 0.13 (introduces dependency lock file). Run terraform init -upgrade. Test heavily.
  2. Upgrade to 0.14/0.15 (introduces sensitive variables, provider version constraints). Test.
  3. Upgrade to 1.0 (mostly semantic versioning guarantees, but checks deprecations). Test.
  4. Upgrade to 1.5+ (introduces import blocks). At every step, run terraform plan in a cloned dev environment. Pay special attention to the .terraform.lock.hcl file to ensure provider versions don’t auto-upgrade and cause breaking changes.

Conclusion: From Theory to Terraform Mastery

Mastering Terraform in 2026 is no longer just about knowing the difference between a resource and a data source. As cloud architectures become more complex, AWS interviewers are actively filtering out candidates who only possess theoretical knowledge. They want engineers who understand the nuances—how to recover from a corrupted state file, how to secure CI/CD pipelines with OIDC, and how to prevent a midnight outage caused by state drift.

The 10 real-world scenarios and these 25 critical questions are curated based on my years of experience as an AWS consultant. They represent the exact hurdles you will face when managing enterprise-grade infrastructure.

A Final Piece of Advice for Candidates: Do not attempt to rote-memorize these answers. Instead, use them as a blueprint to build your own muscle memory. Spin up a free-tier AWS account, intentionally lock a state file, manually change a Security Group via the console to see the drift, and practice breaking and fixing a count index.

For those of you aiming for top-tier DevOps and Cloud Engineering roles in competitive tech hubs like Kolkata’s Salt Lake Sector V, this level of hands-on understanding is exactly what will set you apart from the crowd. Infrastructure as Code is the backbone of modern cloud engineering—own it, practice it, and walk into your next interview with the confidence of a seasoned specialist.

Ready to crack your next interview? Bookmark this complete guide to Terraform Interview Questions and Answers for 2026, share it with your peers, and let your technical depth do the talking!

Leave a Reply

Your email address will not be published. Required fields are marked *