HashiCorp Certified: Terraform Associate (004) practice questions
8 sample questions from our 300-question Terraform Associate bank — one from each official domain, with the answer and a full explanation under each. Every question is original, written against the published exam objectives, and quality-checked before it reaches you.
Question 1 · Infrastructure as Code (IaC) with Terraform
Brightmoor Logistics currently builds its staging network by hand in the cloud console, and each rebuild produces slightly different security group rules. The platform lead wants rebuilds to produce byte-identical results and wants the definition reviewed in pull requests. Which property of an Infrastructure as Code approach with Terraform most directly delivers this?
- A.The infrastructure definition is codified in version-controlled files, so the same committed configuration produces the same resource set on every run
- B.Terraform encrypts the credentials used during provisioning so different operators cannot produce different results
- C.Terraform connects to the cloud console session and records the operator's clicks into a replayable transcript
- D.Terraform installs an agent on each provisioned machine that continuously repairs configuration drift in real time
Show answer & explanation
Answer: A
Codifying infrastructure in version-controlled configuration files is what makes provisioning repeatable and reviewable, since the same committed files describe the same desired state on every run. Terraform does not record console clicks or install a repair agent on managed machines; it converges resources to the declared configuration only when a run is executed.
Question 2 · Terraform fundamentals
A new repository at Larkfield Systems contains the snippet below. A colleague asks what the `~> 5.40` constraint permits. ```hcl terraform { required_providers { dandelion = { source = "dandelion-cloud/dandelion" version = "~> 5.40" } } } ``` Which provider versions satisfy this constraint?
- A.Any 5.x version at or above 5.40, but not 6.0.0 or later
- B.Any 5.40.x patch release, but not 5.41.0
- C.Only version 5.40.0 exactly
- D.Any version at or above 5.40, including 6.x and 7.x
Show answer & explanation
Answer: A
The pessimistic constraint operator allows the rightmost specified component to increment, so `~> 5.40` permits 5.40, 5.41, and onward within the 5 major version while excluding 6.0.0. Restricting to patch releases only would require writing the constraint with three components, such as `~> 5.40.0`.
Question 3 · Core Terraform workflow
Pipeline policy at Selwyn Freight requires that the change a human approves is exactly the change that gets executed, with no re-evaluation between approval and execution. Which command sequence satisfies this?
- A.`terraform plan` in one stage, then `terraform apply -auto-approve` in the approved stage
- B.`terraform show` in one stage, then `terraform apply -target` for each listed resource
- C.`terraform apply -refresh=false` in a single stage after a manual gate
- D.`terraform plan -out=change.tfplan` in one stage, then `terraform apply change.tfplan` in the approved stage
Show answer & explanation
Answer: D
Saving the plan to a file and applying that file guarantees Terraform executes the reviewed set of actions instead of computing a fresh plan at apply time. Running a bare plan and then a separate auto-approved apply lets the second run recompute actions, which can differ if anything changed in between.
Question 4 · Terraform configuration
Nadia must guarantee that the environment name callers pass into her configuration is one of three approved values, and she wants a clear error at plan time rather than a confusing downstream failure. Which construct should she add?
- A.```hcl locals { environment = contains(["dev", "stage", "prod"], var.environment) } ```
- B.```hcl output "environment" { value = var.environment precondition = contains(["dev", "stage", "prod"], var.environment) } ```
- C.```hcl variable "environment" { type = string allowed = ["dev", "stage", "prod"] } ```
- D.```hcl variable "environment" { type = string validation { condition = contains(["dev", "stage", "prod"], var.environment) error_message = "environment must be dev, stage, or prod." } } ```
Show answer & explanation
Answer: D
A `validation` block inside the variable declaration evaluates a condition against the supplied value and raises the custom error message before any resource work begins. There is no `allowed` argument on variables, and a local value would simply compute a boolean without stopping the run.
Question 5 · Terraform modules
Wrenlow Retail wants to consume a community-maintained networking module from the public Terraform Registry and pin it so that unreviewed major releases cannot enter a build. Which module block does this?
- A.```hcl module "network" { source = "https://registry.terraform.io/acme-labs/network/nimbus" version = "4.2" } ```
- B.```hcl module "network" { source = "acme-labs/network/nimbus" required_version = "~> 4.2" } ```
- C.```hcl module "network" { source = "acme-labs/network/nimbus" version = "~> 4.2" } ```
- D.```hcl module "network" { source = "acme-labs/network/nimbus@4.2" } ```
Show answer & explanation
Answer: C
Registry modules use the `<NAMESPACE>/<NAME>/<PROVIDER>` source address together with a separate `version` argument, and `~> 4.2` blocks 5.x. Version cannot be appended to the source string, and `required_version` constrains the Terraform CLI rather than the module.
Question 6 · Terraform state management
Two engineers at Ashgrove Utilities both keep a local `terraform.tfstate` copy and have already overwritten each other's changes once. Which change most directly prevents recurrence for a shared environment?
- A.Run every apply with `-refresh=false` so remote drift cannot overwrite local state
- B.Configure a remote backend that supports state locking so concurrent operations are serialized
- C.Have each engineer use a separate local state file for the same environment
- D.Add the state file to version control so merge conflicts surface the overlap
Show answer & explanation
Answer: B
A remote backend gives the team a single authoritative state and, where supported, locks it so a second operation cannot run concurrently against the same state. Committing state to version control does not prevent simultaneous applies and additionally exposes any secrets stored in state.
Question 7 · Maintain infrastructure with Terraform
Compliance at Merribeck Freight wants a nightly job that reports whether any managed infrastructure has been changed outside Terraform, without modifying anything. Which command best serves as the detection step?
- A.`terraform plan -refresh-only`, checking whether the plan is empty
- B.`terraform validate`, checking the exit code
- C.`terraform state pull`, comparing the file against the previous night's copy
- D.`terraform apply -auto-approve`, checking the change count in the output
Show answer & explanation
Answer: A
A refresh-only plan queries the real infrastructure and reports differences between it and recorded state without proposing configuration changes, making it the standard drift-detection step. An apply would remediate rather than report, and `validate` never contacts the provider APIs.
Question 8 · HCP Terraform
Sable Grove Media connects a repository to HCP Terraform and sets the workspace execution mode to remote. Where do plan and apply operations run, and where is the state stored?
- A.Operations run on HCP Terraform, but state must still be configured with a separate object storage backend
- B.Operations run on the developer's workstation, and only the resulting state is uploaded to HCP Terraform
- C.Operations run on a self-hosted agent that the organization must always provide before remote mode works
- D.Operations run on HCP Terraform's managed infrastructure, and state is stored and versioned by HCP Terraform
Show answer & explanation
Answer: D
In remote execution mode, HCP Terraform performs the runs on its own infrastructure and stores versioned state for the workspace, so no separate backend is required. Local execution mode is the option that runs operations on the workstation while still using HCP Terraform for state, and self-hosted agents are a distinct optional execution mode.
292+ more questions, free
Full-length, timed or practice mode, weighted to the official blueprint — with per-domain scoring so you know exactly where you stand.
Start the full practice exam →