Unlocking remote terraform state

2 min read | by Jordi Prats

When you are working with the terraform state on a S3 bucket if you cancel an operation you might end up with the lock not being released so you won't be able to perform any more changes (not even plan a new change!)

$ terraform plan
Acquiring state lock. This may take a few moments...

Error: Error locking state: Error acquiring the state lock: ConditionalCheckFailedException: The conditional request failed
Lock Info:
  ID:        **6dec3c3f-dea3-4232-60f5-5fe0647a41f6**
  Path:      demo/terraform.tfstate
  Operation: OperationTypePlan
  Who:       jordi.prats@pet2cattle.com
  Version:   0.12.29
  Created:   2020-12-21 21:26:08.295375151 +0000 UTC

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.

To be able to unlock it you will have to use the force-unlock, you can get the ID to unlock on the error message that tells you that the S3 bucket is locked. Before forcefully unlocking the bucket, make sure there's no operation in progress using this lock.

$ terraform force-unlock 6dec3c3f-dea3-4232-60f5-5fe0647a41f6
Do you really want to force-unlock?
  Terraform will remove the lock on the remote state.
  This will allow local Terraform commands to modify this state, even though it
  may be still be in use. Only 'yes' will be accepted to confirm.

  Enter a value: yes

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.

After unlocking the terraform state you will be able to get a new lock to be able to plan or apply changes to it

Posted on 21/12/2020