What if you have users are working on the same terraform project (they don’t need to work directly on the state file)? How does the state file know which changes were the latest – and how does it merge everyone’s changes? (i.e. prevent overwrites)

Multiple Users and Terraform State file

terraform state pull  Always use terraform state pull prior to starting work on the tf project.

Pulling the terraform state file

To ensure that you always pick up the latest terraform state, you need to perform a special operation.

Apart from doing the normal git pull it is also good to do the terraform state pull  . All latest changes within the terraform state file will be pulled with this command.

terraform state pull 

Push terraform state file?

This is a command that can potentially lead to an inconsistent state. Push might override other team member changes on to terraform state file. It is often discouraged to use terraform state push command.

terraform state push
Do not use terraform state push, as the push happens automatically when you tf apply.

Can tf prevent accidental state pushes?

1.  Lineage ID – Terraform has a safety mechanism based on a lineage ID  assigned to each state file. If you are attempting to push the terraform state file with a different lineage ID then terraform will not allow it.

2. Serial Number – Terraform also assigns a unique and higher serial number to each terraform state file.