Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 780
Allan Xu Avatar asked Nov 05 '20 02:11

Allan Xu


People also ask

How do you unlock the State blob?

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.

How do I unlock my Terraform state lock?

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.

Can you disable state locking?

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.


3 Answers

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

like image 102
BlueJapan Avatar answered Oct 16 '22 18:10

BlueJapan


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>
like image 23
opHASnoNAME Avatar answered Oct 16 '22 17:10

opHASnoNAME


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.

like image 27
Ajay Meda Avatar answered Oct 16 '22 19:10

Ajay Meda