HashiCorp Terraform Associate (004) · 12% practice allocation

HashiCorp 004: Terraform state management Practice Questions

5 free sample questions from the Terraform state management domain, with answers and explanations. The full 004 bank has 282 questions across 8 domains. Questions are scenario-based and written from the official HashiCorp exam guide.

1.

A security-conscious team is reviewing their Terraform setup. They are concerned about sensitive values in state. Which of the following statements about sensitive data in Terraform state are TRUE? (Select TWO)

  • A. Terraform automatically excludes sensitive output values from the state file to prevent exposure.
  • B. Using a remote backend like S3 with server-side encryption (SSE) is the recommended way to protect sensitive values stored in state at rest.✓ Correct
  • C. Sensitive values marked with sensitive = true are stored in plaintext in the state file, despite being redacted in CLI output.✓ Correct
  • D. Marking a variable as sensitive = true encrypts its value in the state file using AES-256.
  • E. The sensitive attribute in a variable or output block prevents the value from being written to the state file entirely.
Explanation

Options B and C are correct. The sensitive = true flag in Terraform only redacts values from CLI output and plan displays — it does NOT encrypt or exclude the value from the state file. The state file always stores all resource attributes in plaintext JSON. Therefore, encrypting the state file at rest (e.g., using S3 SSE, GCS encryption, or Azure Storage encryption) is the correct mechanism to protect sensitive data. Option D is wrong — sensitive = true does not perform any encryption; it is purely a display control. Option A is wrong — sensitive outputs are written to state; they are just redacted in terminal output. Option E is wrong — sensitive does not prevent writing to state; it only controls display behavior.

2.

What is the PRIMARY purpose of the Terraform state file?

  • A. To store encrypted credentials used by providers to authenticate with cloud APIs
  • B. To cache the results of the last terraform plan so that terraform apply does not need to re-evaluate the configuration
  • C. To map real-world infrastructure resources to the configuration defined in .tf files✓ Correct
  • D. To store the Terraform provider plugin binaries for offline use
Explanation

The Terraform state file (terraform.tfstate) maps real-world infrastructure to the configuration, allowing Terraform to know which cloud resources correspond to which resource blocks. This is how Terraform tracks resource identity, detects drift, and manages updates. Option D is wrong: provider binaries are stored in the .terraform directory, not in state. Option B is wrong: terraform plan always re-evaluates the configuration against current state and the real infrastructure; state is not a plan cache. Option A is wrong: credentials are configured via environment variables, provider blocks, or credential files — never stored in the state file (though sensitive resource attributes may appear in state).

3.

A developer runs terraform plan and notices that a resource which exists in the real cloud environment is not reflected in the Terraform state file. Which of the following BEST describes this situation?

  • A. Configuration drift, where the state file is out of sync with the real infrastructure✓ Correct
  • B. A corrupted terraform.tfstate file that must be deleted and recreated
  • C. An expected condition that Terraform resolves automatically on the next terraform apply
  • D. A backend misconfiguration that is preventing state from being written
Explanation

Option A is correct. When real infrastructure exists but is not tracked in the Terraform state file, this is called configuration drift (or state drift). The state file no longer accurately reflects the actual infrastructure. Option B is incorrect — a missing resource in state does not mean the file is corrupted; it may simply never have been imported or may have been manually created. Option C is incorrect — Terraform does NOT automatically reconcile resources it doesn't know about; untracked resources are ignored unless imported. Option D is incorrect — a backend misconfiguration would typically cause errors reading or writing state, not silently omit resources.

4.

A team stores Terraform state in an S3 bucket and uses a DynamoDB table for state locking. Which DynamoDB attribute name MUST be used as the table's partition key for Terraform's lock mechanism to function correctly?

  • A. lock_id
  • B. LockID✓ Correct
  • C. StateFile
  • D. TerraformLock
Explanation

Terraform's S3 backend requires the DynamoDB table to have a partition key named exactly LockID (case-sensitive) of type String. Terraform writes a lock entry using this key when a state-modifying operation begins. Option A (lock_id) is wrong: the casing does not match; Terraform will fail to acquire or release locks if the key name differs. Option C (StateFile) is a plausible-sounding but incorrect name. Option D (TerraformLock) is also incorrect; Terraform does not recognize this key name.

5.

A team is refactoring their Terraform code and needs to rename a resource block from aws_instance.web_server to aws_instance.application_server without destroying and recreating the EC2 instance. Which command should they use?

  • A. terraform state mv aws_instance.web_server aws_instance.application_server✓ Correct
  • B. terraform apply -replace=aws_instance.web_server
  • C. terraform import aws_instance.application_server <instance_id>
  • D. terraform state show aws_instance.web_server aws_instance.application_server
Explanation

Option A is correct. terraform state mv moves an existing state entry from one address to another. After updating the configuration code and running this command, Terraform will associate the existing infrastructure with the new resource address, avoiding destruction and recreation. Option D is incorrect — terraform state show is a read-only command used to display attributes of a resource in state; it cannot rename or move resources. Option C is incorrect — terraform import is used to bring an existing resource under Terraform management that has no state entry; using it here would require removing the old entry first and risks a plan that wants to destroy the old resource. Option B is incorrect — terraform apply -replace forces the resource to be destroyed and recreated, which is the opposite of the desired outcome.

Test yourself across every 004 domain

The free 004 practice test mixes about 20 questions weighted like the exam. The full 282-question bank adds instant grading, flashcards and a timed mock exam.

Study this domain

Terraform state management study guide (004) →

Other 004 domains

← All HashiCorp Terraform Associate study resources