Terraform Associate Domain 6: Terraform State Management
25 free practice questions for Terraform State Management in the HashiCorp Certified: Terraform Associate (004) exam. Try each question first, then open "Show answer" for the correct option and explanation.
Terraform State Management practice questions
-
Question 1. Which backend does Terraform use when no backend is configured?
Show answer
Answer: A. The local backend
The local backend writes terraform.tfstate on disk and locks it with the operating system's file locking.
-
Question 2. What is terraform.tfstate.backup?
Show answer
Answer: D. The previous state, written before each update
With the local backend, Terraform keeps the last state as a backup when it writes a new one.
-
Question 3. What does state locking prevent?
Show answer
Answer: B. Two runs writing to the same state at once
Without locking, concurrent applies can corrupt state or create duplicate resources.
-
Question 4. A run crashed and left the state locked. Which command releases the lock?
Show answer
Answer: C. terraform force-unlock LOCK_ID
Only use it when you're sure no other run holds the lock. The lock ID appears in the error message.
-
Question 5. What does running terraform apply -lock=false do?
Show answer
Answer: A. Skips state locking, which is risky with others running
It's meant for rare situations where locking itself is broken, not for routine use.
-
Question 6. How does the S3 backend lock state in current Terraform versions?
Show answer
Answer: B. With use_lockfile = true, using a lock file in the bucket
S3-native locking writes a .tflock object with conditional writes. DynamoDB-based locking is deprecated.
-
Question 7. Can a backend block use input variables, such as bucket = var.bucket?
Show answer
Answer: D. No, backend settings can't reference variables or locals
The backend is configured during init, before variables are evaluated. Use partial configuration for values that vary.
-
Question 8. How do you supply backend settings, such as the bucket name, at init time instead of in code?
Show answer
Answer: B. terraform init -backend-config=prod.hcl
This is partial configuration. It keeps environment-specific or secret settings out of the configuration files.
-
Question 9. Which is a main benefit of a remote backend for a team?
Show answer
Answer: C. Shared state with locking, kept off individual laptops
Everyone works from the same state, locking prevents collisions, and many backends add encryption and versioning.
-
Question 10. You change the backend block in an existing configuration. What happens when you run terraform init?
Show answer
Answer: A. Terraform detects the change and offers to migrate the state
Use -migrate-state to copy state, or -reconfigure to start fresh with the new backend.
-
Question 11. How can one configuration read output values from another configuration's state?
Show answer
Answer: D. With the terraform_remote_state data source
It needs read access to the other state. Many teams prefer provider data sources or HCP Terraform outputs to avoid broad state access.
-
Question 12. What does the terraform_remote_state data source expose?
Show answer
Answer: B. Only the root module outputs of the other state
Values from child modules must be re-exported as root outputs to be visible.
-
Question 13. What is resource drift?
Show answer
Answer: C. Real infrastructure changed outside Terraform
For example, someone edits a security group in the console. Terraform's state and configuration no longer match reality.
-
Question 14. How does Terraform detect drift?
Show answer
Answer: A. terraform plan refreshes state and shows differences
Plan reads current values from the providers and proposes changes to bring resources back to the configuration.
-
Question 15. A change made outside Terraform is correct, and you want state to record it without changing any resources. Which command does this?
Show answer
Answer: D. terraform apply -refresh-only
A refresh-only plan updates state to match reality. Then update the configuration so the next plan is clean.
-
Question 16. A change was made outside Terraform by mistake. How do you put the resource back as configured?
Show answer
Answer: B. Run terraform plan and apply to restore the configured values
The normal plan proposes changes that revert the drift.
-
Question 17. Which command lists every resource address in the current state?
Show answer
Answer: C. terraform state list
You can filter by address prefix, for example terraform state list module.network.
-
Question 18. Which command shows all attributes of one resource in state?
Show answer
Answer: A. terraform state show aws_instance.web
It prints the recorded attributes of that single resource.
-
Question 19. You rename resource aws_instance.app to aws_instance.web in code. How do you avoid Terraform destroying and recreating it?
Show answer
Answer: D. Add a moved block from the old address to the new one
moved { from = aws_instance.app to = aws_instance.web } updates state during plan and apply, and it's reviewed like any change.
-
Question 20. Which CLI command changes a resource's address in state without changing infrastructure?
Show answer
Answer: B. terraform state mv
moved blocks are usually better because they're in code, reviewed, and work for everyone who uses the configuration.
-
Question 21. You want Terraform to stop managing a resource without destroying it. Which configuration block does this?
Show answer
Answer: A. A removed block with lifecycle { destroy = false }
After apply, the resource is forgotten from state and keeps running. terraform state rm does the same from the CLI.
-
Question 22. What does terraform state rm aws_instance.web do?
Show answer
Answer: D. Removes it from state; the real instance keeps running
If the resource block is still in configuration, the next plan proposes to create it again.
-
Question 23. What are Terraform CLI workspaces?
Show answer
Answer: B. Separate state files for one configuration
terraform workspace new, select, and list manage them. They share the same code and backend.
-
Question 24. What does the expression terraform.workspace return?
Show answer
Answer: C. The name of the current workspace, such as default
It's often used to vary names or sizes per workspace, though separate directories are clearer for very different environments.
-
Question 25. With the local backend, where is state kept for a CLI workspace named staging?
Show answer
Answer: A. terraform.tfstate.d/staging/terraform.tfstate
The default workspace uses terraform.tfstate. Other workspaces get their own directory under terraform.tfstate.d.
HashiCorp, Terraform, and HCP Terraform are trademarks of HashiCorp, Inc. These questions are original study material, not questions from the real exam.