23

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Allan Xu
  • 7,998
  • 11
  • 51
  • 122

5 Answers5

45

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

BlueJapan
  • 1,286
  • 2
  • 15
  • 16
11

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>
opHASnoNAME
  • 20,224
  • 26
  • 98
  • 143
1

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.

Ajay Meda
  • 313
  • 1
  • 11
1

if use old terraform version: terraform cannot auto unlock when you force to break. so you need to unlock mannually. portal->storage account->your account->containers->your container->your xxx.tfstate file-> click the break lease button

if button is not available, it means your state is unlocked

Fenrir
  • 136
  • 1
  • 3
0

Its simple: got to the storage account -> container and then select the state file -> break lease option.

Kumar
  • 19
  • 1