I am getting this error when I try to do any operation:
Error locking state: Error acquiring the state lock: state blob is already locked
How can I list the people currently have a lock and how long the lock has been acquired for?
The easiest fix for this issue is to navigate to the storage account, then to the container in the Azure portal that holds the state file. The blob will show as 'Leased' under the leased state column. Select the state file, and hit the 'break lease' button.
Manually unlock the state for the defined configuration. This will not modify your infrastructure. This command removes the lock on the state for the current configuration.
You can disable state locking for most commands with the -lock flag but it is not recommended. If acquiring the lock is taking longer than expected, Terraform will output a status message. If Terraform doesn't output a message, state locking is still occurring if your backend supports it.
The easiest fix for this issue is to:
(1) navigate to the storage account,
(2) then to the container in the Azure portal that holds the state file.
(3) The blob will show as ‘Leased’ under the leased state column.
(4) Select the state file, and hit the ‘break lease’ button.
*FYI: You need PIM (Privileged Identity Management (PIM)) to do this.
Quote from Fixing Terraform ‘Error acquiring state lock’ in Azure
Depends on the mechanism used for state locking.
You can see this in the config (for example in terraform.tf, when used as filename..)
terraform {
required_version = "0.13.4"
backend "s3" {
bucket = "my.state.bucket.for.terraform"
key = "infra/my.tfstate"
region = "eu-central-1"
encrypt = true
dynamodb_table = "my.locking.table.in.dynamo"
}
}
If dynamobdb (on aws) is used, simply check the table. Same for PostgreSQL or whatever.
To fix it, you can release the state from your client:
terraform force-unlock <ID_OF_LOCK_SHOWN_IN_ERROR>
Maybe doesn't exactly answer your question description but here is a way this unintentional locking situation could be avoided.
I faced this issue when trying to run a Terraform Init
and Terraform Plan
from two different instances of AzureCLI task. Move all the terraform commands to a single instance of the task and that should fix the issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With