What will happen if two instances of Terraform are run simultaneously?
sobes.tech AI
Answer from AI
Regarding race conditions and the potential corruption of the state file. This is critical because Terraform uses the state file to synchronize the actual infrastructure with the configuration. Corruption can lead to incorrect application of changes, resource duplication, or deletion regardless of the planned state.
To prevent this, a state file locking mechanism is used. Most Terraform backends (such as S3, GCS, Artifactory) support locking. When attempting to run a second Terraform instance while the first is executing, the second waits for the lock to be released or terminates with an error.
Example of a backend configuration with locking (for S3):
terraform {
backend "s3" {
bucket = "my-terraform-state-bucket"
key = "path/to/my/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "my-terraform-lock-table" // Used for locking
}
}
In this case, dynamodb_table is used to implement distributed locking. Before executing Terraform, it attempts to create an item in this table, which serves as an indicator of the state file being in use. Another instance trying to access will see the existing item and will not be able to acquire the lock.
If locking is not configured or a local backend without locking support is used, simultaneous execution can lead to unpredictable behavior.
The consequences may include:
- Corruption of the state file: Writing data from two processes simultaneously can lead to inconsistent state.
- Incorrect infrastructure state: Some changes may be applied, others ignored or altered by the second process.
- Resource duplication: Terraform may create resources it considers non-existent because the information in the state file has not yet been updated.
- Resource deletion: In rare cases, misinterpreting the state, Terraform may delete resources required by the configuration.
To prevent such situations, always use a backend with locking support and ensure it is properly configured. In CI/CD pipelines, this is achieved through task synchronization and ensuring that only one Terraform process works with a specific state file at any given time.