2023-07-17

Error acquiring the state lock

Error: Error acquiring the state lock

The following error occurred while executing commands such as terraform plan and terraform apply.

bash
$ terraform plan
╷
│ Error: Error acquiring the state lock
│
│ Error message: ConditionalCheckFailedException: The conditional request failed
│ Lock Info:
│   ID:        03d3b219-5ea1-47b9-71a5-3e93e7102fdf
│   Path:      terraform.tfstate
│   Operation: OperationTypeApply
│   Who:       ryuseikakujo@whos-who.local
│   Version:   1.5.2
│   Created:   2023-07-16 19:17:52.561678 +0000 UTC
│   Info:
│
│ Terraform acquires a state lock to protect the state from being written
│ by multiple users at the same time. Please resolve the issue above and try
│ again. For most commands, you can disable locking with the "-lock=false"
│ flag, but this is not recommended.

If you have configured State lock management with DynamoDB, the State is locked during the apply operation to prevent multiple users from processing simultaneously. Normally, the lock is released when the apply is completed. However, if the operation is interrupted midway, the State file will remain locked.

Solution

Use the terraform force-unlock command. This allows you to manually unlock the lock.

bash
$ terraform force-unlock --help
Usage: terraform [global options] force-unlock LOCK_ID

  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 workspace. The behavior of this lock is dependent
  on the backend being used. Local state files cannot be unlocked by another
  process.

Options:

  -force                 Don't ask for input for unlock confirmation.

The ID value to input for LOCK_ID can be found in the error message. (In my case, it's 03d3b219-5ea1-47b9-71a5-3e93e7102fdf)

bash
$ terraform force-unlock 03d3b219-5ea1-47b9-71a5-3e93e7102fdf

Alternatively, you can remove the lock by executing each command with the -lock=false option.

bash
$ terraform apply --lock=false

References

https://stackoverflow.com/questions/62189825/terraform-error-acquiring-the-state-lock-conditionalcheckfailedexception
https://github.com/hashicorp/terraform/issues/14513#issuecomment-465375001

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!