⚡ ~/naveed Interview Prep
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Terraform & IaC Interview Questions Scenario 108 of 120 in Terraform & IaC
Senior DevOps / SRE Terraform Terraform State Management Enterprise Azure

Q: A failed Terraform deployment leaves the remote state locked. Would you run "terraform force-unlock"? What would you verify first?

Safe protocol for handling Terraform state lock errors (Error acquiring the state lock) in Azure Blob Storage or AWS DynamoDB without risking catastrophic concurrent state corruption.

#Terraform #State Lock #Azure #Disaster Prevention #SRE
🎙️ Candidate Opening & Architectural Context
"I would never run 'terraform force-unlock' immediately. State locking prevents concurrent executions from corrupting the state file. I must first verify that no pipeline job or background process is actively modifying infrastructure."
Advertisement
⚡ Recommended Practice Lab

Want to master this scenario in a live sandbox? KodeKloud's HashiCorp Certified Terraform Associate (003) Interactive Labs covers this exact problem with hands-on terminal drills.

🛠️ Production Runbook & Step-by-Step Resolution

Direct Answer: I would never run 'terraform force-unlock' immediately. State locking prevents concurrent executions from corrupting the state file. I must first verify that no pipeline job or background process is actively modifying infrastructure.

1

Inspect Lock Metadata

Read the lock error message. It contains the Lock ID, Who acquired it (hostname and user), and When it was acquired.

Error acquiring the state lock: state blob is already locked
Lock Info:
  ID:        e4b7b2a1-3e4b-4a5f-9e12-88f912c9b4e1
  Path:      tfstate/prod.terraform.tfstate
  Operation: OperationTypeApply
  Who:       azdo-agent-04@internal
  Created:   2026-10-04 02:15:30 UTC
2

Confirm Agent / Process Termination

Log into Azure DevOps or the agent host. Verify that the agent pipeline process was canceled, killed, or timed out, and that no terraform apply process is currently executing in memory.

ps aux | grep terraform
Advertisement
3

Execute Verified Force-Unlock

Once confirmed that the process is 100% dead, execute terraform force-unlock with the specific Lock ID, followed immediately by terraform refresh to verify state consistency.

terraform force-unlock e4b7b2a1-3e4b-4a5f-9e12-88f912c9b4e1
terraform refresh
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Never unlock without verifying that the process listed in Who and Created is completely terminated to avoid irreversible concurrent state corruption."
⚡ 60-Second Elevator Pitch Talking Points
  • Inspect Lock ID, Created timestamp, and Who hostname in the error output.
  • Verify on the agent host that no terraform process is actively executing (ps aux | grep terraform).
  • Confirm that the Azure DevOps pipeline build was genuinely canceled or aborted.
  • Execute terraform force-unlock <ID> and immediately run terraform refresh to validate consistency.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. Executing Terraform applies without inspecting '# forces replacement' warnings in plan output Attributes that force resource replacement destroy and recreate critical infrastructure (databases, load balancers), inducing unannounced production outages.
  2. Omitting remote state locking mechanisms (DynamoDB or backend native locks) for shared state files Without state locking, concurrent CI/CD pipeline executions or team applies create race conditions that corrupt the state file.
  3. Structuring entire enterprise cloud footprints into a single monolithic Terraform root module Monolithic state files slow plan execution dramatically, increase blast radius during failed applies, and create severe pipeline locking contention.
  4. Failing to pin required_providers versions, causing breaking provider upgrades in CI pipelines in azure-q007-terraform-force-unlock-failed-remote-state Unpinned provider versions automatically download breaking major upgrades during CI runs, introducing unexpected schema validation failures.
Advertisement
Want more Terraform & IaC scenarios?
Explore our complete collection of scenario-based Terraform & IaC interview runbooks.
Browse All Terraform & IaC Questions →