Cyberithub

Solved "Terraform: Error acquiring the state lock"

Advertisements

In this article, we will see how to solve terraform error - "error acquiring the state lock". You might notice this error always comes with an error message "ConditionalCheckFailedException: The conditional request failed". Before going into the error, let's first understand what is state locking in Terraform? Whenever you perform any terraform operations then it will first lock your state so that all other operations that could write state won't able to access the same state at the same time. This will prevent simultaneous writes on a state file and hence saves it from getting corrupted by other terraform operations. This is basically the idea behind state locking mechanism in Terraform.

 

Solved "Terraform: Error acquiring the state lock"

Solved "Terraform: Error acquiring the state lock"

Also Read: How to Install Performance Co-Pilot on CentOS 7: [4 Easy Steps]

Now that you understand what is state locking and why it is being done then let's dive into the error and try to understand when this "error of acquiring the state lock" could appear. Well, if you are trying to perform terraform deployment or other terraform operations and in between if some interruptions happened due to some internal or externals causes like your system got restarted, network got disconnected, some other terraform operations already running etc. then you will notice the newly performed operation will always fail with "error acquiring the state lock".

Something like this happened to me as well when I was trying to run terraform plan to verify my deployment before running terraform apply. While I was running terraform plan, in between my system got restarted due to some issue and then next time when I ran terraform plan, it showed me below output.

cyberithub@macos1066 % terraform plan
Initializing modules...
Downloading registry.terraform.io/cyberithub/hello/example 6.0.0 for hello...
- hello in .terraform/modules/hello
- nginx-pet in nginx
.................................................
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:        e547e0f7-7f16-b1d1-7845-a8fb722521c0
    Path:      cyberithub-states/u01/app/terraform.tfstate
    Operation: OperationTypePlan
    Who:       CYBERITHUB@MACOS1066
    Version:   1.2.3
    Created:   2023-09-14 08:12:23.48427 +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.

Error: Terraform exited with code 1.
Error: Process completed with exit code 1.

This could happen with anyone due to number of reasons. So if you are also facing this error then you can follow some of the below given solutions which suits your problem best. For me, the first solution worked perfectly. So I would also recommend you to use solution 1 to check if resolves the error. I have tested the solution by applying it in production environment and it seems to be working fine. In case, solution 1 does not work for you then you can try using rest of the below given solutions as per your requirement.

 

 

Solution 1: Unlock State Forcefully

The first solution that you can try to solve "error acquiring the state lock" is by unlocking the state forcefully by using terraform force-unlock -force <ID> command. For example, in my case I have unlocked my state by running terraform force-unlock -force e547e0f7-7f16-b1d1-7845-a8fb722521c0 command as shown below. But remember you can only use below solution when you are completely sure there is no other process running.

If you are not sure about other running processes then just to be on safe side, I would advise you to wait for an hour or so and then try again to see if the error still persists. If the error indeed persist then you can safely assume there is no other processes running and you can unlock the state as described below.

cyberithub@macos1066 % terraform force-unlock -force e547e0f7-7f16-b1d1-7845-a8fb722521c0
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.

 

Solution 2: Kill the Process

If in case terraform force-unlock is giving "Local state cannot be unlocked by another process" error then you need to find the running process and kill them. Depending on the operating system you are using, you can follow below step:-

  • For Windows: Open task manager and search for terraform console process, right click on that process and then click on End task.
  • For Linux: You can open terminal, grep for terraform process and kill the terraform console process by using kill -9 <PID> command.

 

Solution 3: Use -lock=false option

Second solution which is generally not recommended to use in production or critical systems as it carries the risk of corrupting the state. If you are willing to take that risk then you can run new terraform operation with -lock=false option, if everything goes well then you should not see the "error acquiring the state lock" anymore. In our case, we are running terraform plan with -lock=false option as shown below.

cyberithub@macos1066 % terraform plan -lock=false

 

Solution 4: Remove State File

There is another more reliable solution that you can think of using is that just go to the path where state file is created and remove it from there. If you want to play safe then just rename the file to some other filename such as terraform.tfstate_bkp and then start the terraform operation again. You will see a new state file getting created and the operation will run as expected.

Hopefully above solution would be enough to solve your "error acquiring the state lock" problem. But in case if the error still persists then I would request you to please give your feedback in the comment box so that we can work together to solve your error as well.

Leave a Comment