How to UNLOCK your Terraform State

How to UNLOCK your Terraform State

If you are working with Terraform, you must know this tip because he can UNLOCK your business.

Yesterday, I ran the terraform plan but It didn’t work. The reason was a tfstate LOCK. The question was only one: what can I do now? The answer is very simple!

terraform plan –var-file=dev.tfvars

Are you ready? ⬇️

Terraform Cookbook - Second Edition: Provision, run, and scale cloud architecture with real-world examples using Terraform

Error message

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
│ Error: Error acquiring the state lock
│
│ Error message: state blob is already locked
│ Lock Info:
│   ID:        14d1cd10-6dcb-1e7f-a769-f0c8ad082546
│   Path:      tfstate/terraform.tfstate
│   Operation: OperationTypePlan
│   Who:       ABC\devan@ABC
│   Version:   1.3.7
│   Created:   2023-09-18 12:16:46.2312809 +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.

Solution

terraform force-unlock <LOCK_ID>

In this specific case, the <LOCK_ID> must be replaced with 14d1cd10-6dcb-1e7f-a769-f0c8ad082546

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 still be in use. Only ‘yes’ will be accepted to confirm.

Enter a value:

If you want to UNLOCK the tfstate, you must write yes and press enter. If it works without problem, the next message will be:

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.

Have a good cloud day!