Error acquiring the state lock

by Daniel Pham
Published: Updated:

Error acquiring the state lock, how to solve this error?

When I use Terraform to provisioning the infrastructure, I faced this error.

Error acquiring the state lock

The full error look like below:

Error: Error acquiring the state lock

Error message: ConditionalCheckFailedException: The conditional request failed
│ Lock Info:
│   ID:        716c826c-d839-ce4c-a0c3-6530bdf83290
│   Path:      terraform.dev/state
│   Operation: OperationTypePlan
│   Who:       dungpham@local
│   Version:   1.3.0
│   Created:   2023-04-18 10:16:00.062567367 +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.

I faced this error when I run the command terraform plan. This is a normal command but why this error occured?

Terraform state locking with DynamoDB

My Terraform using S3 bucket as the remote backend for the state and I also using the DynamoDB to lock the state.

error-acquiring-the-state-lock
Error: Error acquiring the state lock.

We can say this thing in simple way that:

  • Many team members can work with Terraform code as the same time. For example, a mate A push a new Terraform code to create a new S3 bucket. As the same time, a mate B push a new Terraform code to delete all S3 buckets. They can conflict to each other and this thing is really dangerous for the infrastructure.
  • We don’t want thing above happen right? So, Terraform support a thing “state locking” with DynamoDB. Simply, at one time, just only one member can push and apply the Terraform code. If someone else push a new code as the same time, it’s will be locked in the DynamoDB table.

You can refer to this docs from Terraform.

And back to my error, the reason for it is the Terraform state is locking with DynamoDB. That meaning there is someone else in my team applied a new code as the same time with me.

So, how can I deal with this error?

Solve the error acquiring the state lock

There are few options to solve this error:

  • As the information of the full error above, you can see that you can use the flag -lock=false but this thing is not recommended.
  • The second option that quite easy, you can wait for few mins like 5 mins, 10 mins to other mate finish the apply. You can choose this option if no rush.
  • The last option (I was used it), please note that if you’re rushing and already checked with your team that your code isn’t conflict with any other, then you can use the command below:
terraform force-unlock 716c826c-d839-ce4c-a0c3-6530bdf83290

Please look back to the first part, the full error, you can see a line with ID: 716c826c-d839-ce4c-a0c3-6530bdf83290. That the thing you will need to apply to command above, replace with the ID of current lock.

Conclusion

That’s all, very simple, right? This is just a simple trick for someone new with Terraform.

You may scare when the first time see this error because thinking something wrong with your code or it’s breaking something?

But no worries, with this post, I hoping will help you guys.

0 0 votes
Article Rating

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.