We use Terraform and Azure DevOps pipelines to deploy our Azure infrastructure.
Every now and then, apparently out of nowhere – could be due to several pipelines running in parallel or an unexpected job stop – the terraform plan remains stuck in the phase of “Acquiring state lock. This may take a few moments…” :
Eventually the task and pipeline fail after the default time of 60 minutes or less, if we specify the timeoutInMinutes job property.
When you run terraform plan from a local computer, Terraform tries to recreate all resources because it cannot read the tfstate:
Terraform already thought about this scenario and integrated a mechanism to unlock the state file :
terraform force-unlock LOCK_ID
Unfortunately, even if the command runs successfully, the issue can persist.
Fortunately, if the tfstate is stored in an Azure Storage Account, there is another way to fix this. Just navigate to the file location, right click the file and choose Break Lease to unlock the file:
This will unlock the file. Do not worry about the “Broken” lease state, it’s all good 😊