Software Engineering

[Solved] Error acquiring the state lock in Terraform


If you have been handed the Acquiring state lock error message about ConditionalCheckFailedException: The conditional request then you have 2 options for fixing it!

The error looks something like this:

Acquiring state lock. This may take a few moments...
╷
│ Error: Error acquiring the state lock
│ 
│ Error message: ConditionalCheckFailedException: The conditional request
│ failed
│ Lock Info:
│   ID:        3324a6cb-7401-3194-d650-fxxxxxx3864
│   Path:      xxxx-terraformstate-nonprod/env:/xxxx/terraform.tfstate
│   Operation: OperationTypeApply
│   Who:       root@ip-100-65-2-165
│   Version:   1.0.4
│   Created:   2021-11-29 21:00:37.652114035 +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.

There are 3 possible ways to fix this

In short, you want to delete the lock associated. This is achieved by forcing an unlock.

You can force unlock the state using the following:

terraform force-unlock -force 3324a6cb-7401-3194-d650-fxxxxxx3864

Option 2: Relaunch the Plan

You can run the plan again with a -lock=false as following:

terraform plan -lock=false ...

Option 3: Make sure you use the right profile!

Sometimes everything seems correct, but it turns out you are using a different profile!

AWS_PROFILE=some_other_profile

Force Unlock still not working?

Sometimes you can get another error when trying to force unlock this:
Failed to unlock state: failed to retrieve lock info: unexpected end of JSON input

This usually means that you are in the wrong workspace.

View all workspaces available: terraform workspace list

Switch to a workspace: terraform workspace select <>

Now you can try and force unlock again.

If you were successful, then you will get the following message:

Terraform state has been successfully unlocked!

The state has been unlocked, and Terraform commands should now be able to
obtain a new lock on the remote state.