TERRAFORM-ASSOCIATE-003 practice question 28 of 30

[Understand Terraform Basics and CLI] What does state looking accomplish?

Full set: 226 questions
Answer options
A: Prevent accidental Prevent accident deletion of the state file
B: Blocks Terraform commands from modifying, the state file Correct answer
C: Copies the state file from memory to disk
D: Encrypts any credentials stored within the state file
Correct Answer:
B. Blocks Terraform commands from modifying, the state file
Explanation

State locking is a fundamental concurrency control mechanism in Terraform. When a Terraform command that can modify the state (e.g., terraform apply, terraform destroy) is initiated, it first attempts to acquire a lock on the state file. If successful, this lock prevents any other Terraform process from running commands that could also modify the state simultaneously. This mechanism is crucial for collaborative environments, as it prevents race conditions, state corruption, and data loss that could occur if multiple users or automated systems tried to update the same state file at the same time.

Why Incorrect

A. State locking does not prevent file system operations like deletion; it only blocks other Terraform processes from writing to the state.
C. This describes the general process of state persistence after an operation, not the function of locking, which precedes state modification.
D. Locking is for concurrency control, not encryption. State file encryption is a separate feature, often handled by the backend configuration (encryption at rest).

References

1. HashiCorp Terraform Documentation, "State: Locking": "To prevent this, Terraform has a feature called state locking. When one user is running a state-changing command like terraform apply, Terraform will lock the state. This lock prevents any other user from running a state-changing command at the same time." This directly supports the function of blocking other commands.

2. HashiCorp Terraform Documentation, "Backends: Configuration": In the "State Storage and Locking" section, it is explained that "Most backends support remote state storage and locking." This distinguishes locking as a specific feature for preventing concurrent operations.

3. HashiCorp Terraform Documentation, "Command: apply": The documentation for the apply command states, "Before it can apply any changes, Terraform automatically runs a terraform plan to create an execution plan, and will prompt for your approval before executing it. It will also acquire a state lock to prevent others from making concurrent changes." This confirms that locking is an integral part of commands that modify state.

Scroll to Top