Terraform Associate · Domain 6 of 8 · 13% of the exam

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

  1. Question 1. Which backend does Terraform use when no backend is configured?

    1. The local backend
    2. The HCP Terraform backend
    3. An S3 backend
    4. A temporary in-memory backend
    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.

  2. Question 2. What is terraform.tfstate.backup?

    1. A copy made by HCP Terraform each night
    2. A lock file for concurrent runs
    3. A file listing all providers
    4. The previous state, written before each update
    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.

  3. Question 3. What does state locking prevent?

    1. Reading the state during a plan
    2. Two runs writing to the same state at once
    3. Changes made in the cloud console
    4. Providers from being upgraded
    Show answer

    Answer: B. Two runs writing to the same state at once

    Without locking, concurrent applies can corrupt state or create duplicate resources.

  4. Question 4. A run crashed and left the state locked. Which command releases the lock?

    1. terraform state unlock
    2. terraform init -unlock
    3. terraform force-unlock LOCK_ID
    4. terraform apply -lock=false
    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.

  5. Question 5. What does running terraform apply -lock=false do?

    1. Skips state locking, which is risky with others running
    2. Unlocks every workspace in the backend at once
    3. Makes the state file read-only for that one run
    4. Forces the backend to create a brand-new lock ID
    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.

  6. Question 6. How does the S3 backend lock state in current Terraform versions?

    1. It doesn't support locking
    2. With use_lockfile = true, using a lock file in the bucket
    3. Only through a separate DynamoDB table
    4. By copying the state to a second bucket
    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.

  7. Question 7. Can a backend block use input variables, such as bucket = var.bucket?

    1. Yes, any expression is allowed in a backend block
    2. Only when the values are passed with -var during init
    3. Only when the configuration runs in HCP Terraform
    4. No, backend settings can't reference variables or locals
    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.

  8. Question 8. How do you supply backend settings, such as the bucket name, at init time instead of in code?

    1. terraform init -var=bucket=...
    2. terraform init -backend-config=prod.hcl
    3. terraform plan -backend=prod
    4. TF_VAR_bucket
    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.

  9. Question 9. Which is a main benefit of a remote backend for a team?

    1. Plans run without providers
    2. State no longer contains secrets
    3. Shared state with locking, kept off individual laptops
    4. Resources apply in parallel across clouds
    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.

  10. Question 10. You change the backend block in an existing configuration. What happens when you run terraform init?

    1. Terraform detects the change and offers to migrate the state
    2. The old state is deleted from the previous backend
    3. Nothing happens, because backend changes are ignored
    4. Terraform immediately runs an apply against the new backend
    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.

  11. Question 11. How can one configuration read output values from another configuration's state?

    1. With a module block pointing at the state
    2. By importing the state file
    3. By copying the outputs into tfvars
    4. With the terraform_remote_state data source
    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.

  12. Question 12. What does the terraform_remote_state data source expose?

    1. Every resource attribute in the other state
    2. Only the root module outputs of the other state
    3. Only resource IDs
    4. The other configuration's variables
    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.

  13. Question 13. What is resource drift?

    1. A provider upgrade that changes resource names
    2. Moving resources between modules
    3. Real infrastructure changed outside Terraform
    4. State stored in two backends
    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.

  14. Question 14. How does Terraform detect drift?

    1. terraform plan refreshes state and shows differences
    2. terraform fmt flags drifted files
    3. terraform validate compares state to the cloud
    4. Only HCP Terraform can detect it
    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.

  15. Question 15. A change made outside Terraform is correct, and you want state to record it without changing any resources. Which command does this?

    1. terraform apply -replace
    2. terraform state push
    3. terraform init -reconfigure
    4. terraform apply -refresh-only
    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.

  16. Question 16. A change was made outside Terraform by mistake. How do you put the resource back as configured?

    1. Run terraform apply -refresh-only to record it
    2. Run terraform plan and apply to restore the configured values
    3. Delete the state file so Terraform rebuilds it
    4. Run terraform fmt to rewrite the resource block
    Show answer

    Answer: B. Run terraform plan and apply to restore the configured values

    The normal plan proposes changes that revert the drift.

  17. Question 17. Which command lists every resource address in the current state?

    1. terraform show -list
    2. terraform output
    3. terraform state list
    4. terraform providers
    Show answer

    Answer: C. terraform state list

    You can filter by address prefix, for example terraform state list module.network.

  18. Question 18. Which command shows all attributes of one resource in state?

    1. terraform state show aws_instance.web
    2. terraform show aws_instance.web
    3. terraform output aws_instance.web
    4. terraform describe aws_instance.web
    Show answer

    Answer: A. terraform state show aws_instance.web

    It prints the recorded attributes of that single resource.

  19. Question 19. You rename resource aws_instance.app to aws_instance.web in code. How do you avoid Terraform destroying and recreating it?

    1. Run terraform apply -replace
    2. Add a lifecycle ignore_changes block
    3. Set create_before_destroy
    4. Add a moved block from the old address to the new one
    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.

  20. Question 20. Which CLI command changes a resource's address in state without changing infrastructure?

    1. terraform state rename
    2. terraform state mv
    3. terraform import -move
    4. terraform apply -rename
    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.

  21. Question 21. You want Terraform to stop managing a resource without destroying it. Which configuration block does this?

    1. A removed block with lifecycle { destroy = false }
    2. A moved block pointing to null
    3. An import block with an empty id
    4. prevent_destroy = true
    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.

  22. Question 22. What does terraform state rm aws_instance.web do?

    1. Destroys the instance and removes it from state
    2. Moves the instance into another workspace's state
    3. Marks the instance for replacement on the next apply
    4. Removes it from state; the real instance keeps running
    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.

  23. Question 23. What are Terraform CLI workspaces?

    1. Separate HCP Terraform organizations
    2. Separate state files for one configuration
    3. Folders holding modules
    4. Separate provider caches
    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.

  24. Question 24. What does the expression terraform.workspace return?

    1. The name of the backend
    2. The organization name
    3. The name of the current workspace, such as default
    4. The path of the working directory
    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.

  25. Question 25. With the local backend, where is state kept for a CLI workspace named staging?

    1. terraform.tfstate.d/staging/terraform.tfstate
    2. staging.tfstate in the working directory
    3. .terraform/staging.tfstate
    4. In the default workspace's state file
    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.