When Multiple People Try to Update a Locked State File

When you store your state file in a remote location like S3 or an Azure Storage account, you can enable state locking. What does that mean? Locking makes sure only one person can make changes to the state file at a time.
If two people are making changes around the same time, whoever runs terraform apply first will make the changes, and the other person will have to incorporate those changes into their code first before they can make their own changes and update the state file accordingly.

Practical Example: Joe is working on the network components of the infrastructure, and Jane is updating the storage account. Both started working and updated the code accordingly. If Jane and Joe both run terraform apply at the same time, only one of them will be able to do so, and the other will get an error message due to the lock.

Let’s say Jane makes the changes first, which will update the state file. Now, if Joe runs terraform apply, he will see drifts related to the changes Jane has made, along with the changes he is making. Joe would need to integrate Jane’s changes into his config files (by doing a git pull, assuming both of them got the code from a repo in the first place) and then update his changes.

Benefits Of State File Locking:

Locking prevents potential conflicts, file corruption, or writing errors if multiple people attempt to update the file at the same time. This ensures your infrastructure remains stable and predictable, with only one set of changes being made at a time

Leave a Comment

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

Scroll to Top