Terraform lock loss

Suppose your Terraform plan or apply processes are stuck in the state of:

Acquiring a state lock. This may take a few moments….

Or you were faced with an error:

Error: Error locking state: Error acquiring the state lock: writing "gs://cloud-storage-bucket/default.tflock" failed: googleapi: Error 412: At least one of the pre-conditions you specified did not hold., conditionNotMet
Lock Info:
ID: 1123123412341234
Path: gs://cloud-storage-bucket/default.tflock
Operation: OperationTypePlan
Who: root@runner-rdegdgh4-project-43243222-concurrent-6lwper
Version: 0.15.25
Created: 2022-05-28 10:04:42.321501207 +0000 UTC

The reason most likely is that you had a Terraform plan or apply process stop after acquiring the lock but before its release.
Thus, it is left in a locked state. Every new try is going to give you this error.

Actually, you have two options there:

  • Use force-unlock command: terragrunt force-unlock -force 1123123412341234
  • Break the lease on your default.tflock if you are using Azure, or just delete/rename .tflock file if you’re using GCP. Keep in mind, that you need to be sure that there are no terraform processes changing cluster at the moment.

Notice:

  1. If you don’t know your lock ID because you don’t receive the error shown above, add this parameter to the terraform call: -lock-timeout=1m. Then wait after “Acquiring a state lock. This may take a few moments….” line appears, and you are going to get an error containing lock ID.
  2. force-unlock command sometimes doesn’t work, so the only option available for you is to delete the .tflock file.
  3. If you want to test if a lost lock is actually the cause of your problem, you may run the same plan or apply Terraform processes but with the -lock=false flag. However, you need to be 100% sure that there are no other Terraform processes running at the same time. After you find out that the lost lock is the actual problem, you should apply the steps from the main part of the article and remove the -lock=false flag.

That’s all.

Telegram channel

If you still have any questions, feel free to ask me in the comments under this article or write me at promark33@gmail.com.

If I saved your day, you can support me 🤝

Leave a Reply

Your email address will not be published. Required fields are marked *