'Error locking state: Error acquiring the state lock: state blob is already locked

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?



Solution 1:[1]

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

enter image description here

Solution 2:[2]

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>

Solution 3:[3]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 opHASnoNAME
Solution 3 Ajay Meda