Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform: Error acquiring the state lock: ConditionalCheckFailedException

I got the following error during a terraform plan which occured in my pipeline:

Error: Error locking state: Error acquiring the state lock: ConditionalCheckFailedException: The conditional request failed Lock Info: ID:        9db590f1-b6fe-c5f2-2678-8804f089deba Path:      ... Operation: OperationTypePlan Who:       ... Version:   0.12.25 Created:   2020-05-29 12:52:25.690864752 +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. 

It is weird because I'm sure there is no other concurrent plan. Is there a way to deal with this? How should I remove this lock?

like image 202
veben Avatar asked Jun 04 '20 08:06

veben


People also ask

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.

What is terraform state file locking?

If supported by your backend, Terraform will lock your state for all operations that could write state. This prevents others from acquiring the lock and potentially corrupting your state. State locking happens automatically on all operations that could write state. You won't see any message that it is happening.


Video Answer


2 Answers

Cause of Error

This error usually appears when one process fails running terraform plan or terraform apply. For example if your network connection interrupts or the process is terminated before finishing. Then Terraform "thinks" that this process is still working on the infrastructure and blocks other processes from working with the same infrastructure and state at the same time in order to avoid conflicts.

As stated in the error message, you should make sure that there is really no other process still running (e.g. from another developer or from some build-automation). If you force-unlock in such a situation you might screw up your terraform state, making it hard to recover.

Resolution

If there is no other process still running: run this command

terraform force-unlock 9db590f1-b6fe-c5f2-2678-8804f089deba

(where the numerical id should be replace by the one mentioned in the error message)

if you are not sure if there is another process running and you are worried that you might make things worse, I would recommend waiting for some time (like 1h), try again, then try again after maybe 30 min. If the error still persists it is likely that there really is no other process and it's safe to unlock as described above

like image 157
Falk Tandetzky Avatar answered Oct 18 '22 23:10

Falk Tandetzky


It looks like the lock persist after the previous pipeline. I had to remove it using the following command in order to remove it:

terraform force-unlock -force 9db590f1-b6fe-c5f2-2678-8804f089deba 

Or to relaunch the plan with the following option -lock=false

terraform plan -lock=false ... 
like image 23
veben Avatar answered Oct 18 '22 22:10

veben