Skip to Content

HashiCorp Certified: Prevent Concurrent State File Changes in Terraform

Learn how to prevent multiple Terraform runs from concurrently modifying the same state file. Discover the best practice for ensuring state consistency in the HashiCorp Certified: Terraform Associate Exam.

Table of Contents

Question

Which of these actions will prevent two Terraform runs from changing the same state file at the same time?

A. Refresh the state after running Terraform
B. Delete the state before running Terraform
C. Configure state locking for your state backend
D. Run Terraform with parallelism set to 1

Answer

C. Configure state locking for your state backend

Explanation

State locking is the recommended approach to prevent multiple Terraform runs from concurrently modifying the same state file. When state locking is enabled, Terraform will acquire a lock on the state file before making any changes, ensuring that no other Terraform processes can access or modify the state simultaneously.

State locking is supported by various backends, such as AWS S3, HashiCorp Consul, and Terraform Cloud. By configuring state locking for your chosen backend, Terraform will automatically handle the locking mechanism, preventing conflicts and ensuring state consistency.

The other options are not effective solutions:

  • Refreshing the state (A) does not prevent concurrent modifications.
  • Deleting the state (B) would result in losing the entire state history.
  • Setting parallelism to 1 (D) only limits the number of concurrent resource operations within a single Terraform run but does not prevent multiple runs from accessing the state file simultaneously.

Therefore, configuring state locking for your state backend is the correct and recommended approach to prevent concurrent state file changes in Terraform.

HashiCorp Certified: Terraform Associate certification exam practice question and answer (Q&A) dump with detail explanation and reference available free, helpful to pass the HashiCorp Certified: Terraform Associate exam and earn HashiCorp Certified: Terraform Associate certification.