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:
Terraform parses all .tf files and builds the resource graph.
It performs a topological sort — resources with no dependencies are processed first (in parallel up to -parallelism=N, default 10).
Resources downstream in the graph wait for their dependencies to be created/updated first.
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
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.
Diagnose: Check S3 bucket availability, DynamoDB endpoint reachability, VPC endpoint health (if using private endpoints), and IAM permissions.
Pull the last known state: terraform state pull > terraform.tfstate.
Restore: Fix the backend issue (S3 throttling, DynamoDB capacity, network), re-init with the remote backend.
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