Interview questions

Terraform interview questions

45 questions, 45 with model answers. Every member practises all of them free.

Samples

3 of 45 questions.

How does Terraform dependency graph (DAG) work internally?

Show the answer

Terraform builds a Directed Acyclic Graph (DAG) of all resources defined in configuration files. Edges in the DAG represent dependencies:

  • Explicit: depends_on meta-argument.
  • Implicit: reference to another resource's attribute (e.g., aws_subnet.main.id in an EC2 resource).

During terraform plan/apply:

  1. Terraform parses all .tf files and builds the resource graph.
  2. It performs a topological sort — resources with no dependencies are processed first (in parallel up to -parallelism=N, default 10).
  3. Resources downstream in the graph wait for their dependencies to be created/updated first.
  4. For destroy, the graph is traversed in reverse order.

terraform graph | dot -Tsvg > graph.svg renders the full dependency graph visually.

How does Terraform handle state locking and consistency?

Show the answer

State locking prevents concurrent plan/apply operations from corrupting the state file.

  • S3 backend + DynamoDB: S3 stores the state file; DynamoDB provides locking via a LockID item. Before any write operation, Terraform attempts to create/update the lock item. If it exists, the operation fails with a lock error (includes the lock owner's identity and timestamp).
  • Terraform Cloud/Enterprise: uses its own locking mechanism with run queues.
  • Azure Blob: uses blob lease-based locking.

If a lock is orphaned (crash mid-apply), use terraform force-unlock <LOCK_ID> — only after confirming no other operation is running.

Terraform remote_state backend suddenly times out. What's your recovery and damage containment strategy?

Show the answer
  1. Immediate: Stop any running apply to avoid partial state writes. Check if an apply is in progress via DynamoDB lock table or Terraform Cloud run queue.
  2. Diagnose: Check S3 bucket availability, DynamoDB endpoint reachability, VPC endpoint health (if using private endpoints), and IAM permissions.
  3. Fallback: Switch to a local backend temporarily:
terraform init -reconfigure -backend-config="path=./terraform.tfstate"

Pull the last known state: terraform state pull > terraform.tfstate.

  1. Restore: Fix the backend issue (S3 throttling, DynamoDB capacity, network), re-init with the remote backend.
  2. Prevent recurrence: Enable S3 versioning for state recovery, set DynamoDB on-demand capacity to avoid throttling, use VPC endpoints for private access.

42 more in the member app, with practice sets and your own marks. Join free

More topics

Other interview topics.