All HashiCorp exams
Free Terraform Associate practice test: 30 questions with answers
30 original questions for Terraform Associate, drawn from every topic area. Answer each one to see whether you got it right and why. No sign-up.
- Free questions
- 30
- Full bank
- 112 questions
- Real exam
- 60 min
All 30 Terraform Associate questions with answers
The same questions as the test above, grouped by exam topic. Open any question to see the answer and explanation.
Read, generate, and modify configuration (4 questions)
Question 1
Which meta-argument creates one resource instance for each key in a map or set of strings?
- Acount, which creates instances indexed by number and works with integer values only
- Bdepends_on, which sets an explicit dependency between resources so that the graph orders them
- Cfor_each, which creates one instance per key in a map or set of strings
- Dlifecycle, which controls create-before-destroy and prevent_destroy behavior for a resource
Show answer and explanationHide answer
Correct answer: Cfor_each creates instances keyed by map keys or set members, which gives stable addresses when items are added or removed. count indexes by number, depends_on orders resources, and lifecycle changes replacement behavior.
Question 2
A resource must be replaced whenever a value in another resource changes, but not when its own arguments change. Which lifecycle setting fits?
- Areplace_triggered_by, which forces replacement of the resource when a referenced value changes
- Bignore_changes, which prevents Terraform from ever updating the resource after its creation
- Cprevent_destroy, which blocks any change that would cause the resource to be replaced at all
- Dcreate_before_destroy, which builds the new object before removing the old one on every change
Show answer and explanationHide answer
Correct answer: Areplace_triggered_by forces replacement when a listed resource or attribute changes, which covers cross-resource dependencies. ignore_changes suppresses updates, prevent_destroy blocks replacement, and create_before_destroy only changes the order of a replacement.
Question 3
Which built-in function concatenates the elements of a list of strings into one string using a separator?
- Aconcat, which appends one list to another list in the order that the arguments are given
- Bformat, which builds a string from a template with placeholder values filled in from its arguments
- Cjoin, which concatenates the elements of a list into one string using the given separator
- Dsplit, which breaks a string into a list of substrings using the given separator value
Show answer and explanationHide answer
Correct answer: Cjoin takes a separator and a list and returns one string, which is the operation described. concat combines lists rather than strings, format fills templates, and split does the reverse of join.
Question 4
Which expression returns one of two values based on a condition in a resource argument?
- AA conditional expression of the form condition ? a : b, which returns one of two values
- BA splat expression, which collects one attribute from every element of a list or set value
- CA dynamic block, which generates repeated nested blocks from a collection of values in the configuration
- DA for expression, which transforms each element of a collection into a new collection value
Show answer and explanationHide answer
Correct answer: AThe conditional expression evaluates the condition and returns one of two values, which suits a choice inside an argument. For expressions transform collections, splat expressions extract attributes, and dynamic blocks generate nested blocks.
Implement and maintain state (4 questions)
Question 5
Two engineers start terraform apply at nearly the same time against the same remote backend. What prevents conflicting writes to the state?
- AState locking supplied by the backend, so the second run waits until the first run releases the lock
- BBucket versioning merges the two state files automatically at the end of each run
- CEach engineer's run writes to a separate state, so the two runs never share any state data at all
- DTerraform reads the state during apply but never writes to it, so concurrent applies cannot conflict
Show answer and explanationHide answer
Correct answer: ARemote backends that support locking hold a lock during a run, so a second run waits or fails rather than overwriting the state. Versioning does not merge states, and Terraform writes state during apply.
Question 6
Why should a Terraform state file be treated as sensitive data?
- AIt contains only resource names, which are public information, so it needs no protection at all
- BIt can contain secrets and resource attributes in plaintext, so access to it must be restricted
- CTerraform encrypts it with a key stored in the configuration folder beside the state file itself
- DIt is used only during planning and is never written to disk, so it cannot leak any data
Show answer and explanationHide answer
Correct answer: BState records resource attributes, and some of them can be secrets stored in plaintext, so access controls and encryption at rest are required. State is written to disk or to the backend, and it is not encrypted with a key kept beside it.
Question 7
Which command removes a resource from Terraform state without destroying the real object in the cloud?
- Aterraform destroy, which removes all managed resources from the cloud account in one operation
- Bterraform taint, which marks the resource for replacement during the next apply run in the workspace
- Cterraform apply -replace, which recreates the resource and removes the old object immediately from the cloud
- Dterraform state rm, which removes the resource from the state while leaving the real object running
Show answer and explanationHide answer
Correct answer: Dterraform state rm forgets the object in state while leaving it in place, which is useful when handing an object to another configuration. Destroy deletes real infrastructure, replace recreates it, and taint marks a resource for replacement.
Question 8
Which two practices reduce the risk of state corruption in a team? (Choose two.)
- AStoring state in a local file on each engineer's laptop and sharing it by email between the team
- BEditing the state JSON file by hand whenever a plan looks unexpected to the engineer on duty
- CUsing a remote backend with state locking, and restricting who is allowed to write to it
- DEnabling versioning on the state bucket so that earlier versions can be restored after a problem
- ERunning applies from several branches at once to speed up the environment rollout
Show answer and explanationHide answer
Correct answer: C, DRemote backends with locking prevent concurrent writes, and versioning allows recovery of earlier state. Local copies shared by email, hand edits and parallel applies from different branches each make corruption more likely.
Use the Terraform CLI (4 questions)
Question 9
Which command rewrites configuration files in the working directory into the canonical formatting style?
- Aterraform validate, which checks the configuration for syntax errors and internal consistency
- Bterraform fmt, which rewrites configuration files into the canonical formatting style
- Cterraform show, which prints the current state or a saved plan in a human-readable form
- Dterraform plan with the -out flag, which saves the execution plan to a file for later use
Show answer and explanationHide answer
Correct answer: Bterraform fmt applies the standard HCL formatting to files, which keeps code consistent across a team. Validate checks correctness, plan writes a plan file and show displays state or plans without changing formatting.
Question 10
A configuration now requires a new provider. Which command must run before a plan can succeed?
- Aterraform workspace new, which creates a new workspace so that the configuration has its own state
- Bterraform init, which downloads the provider plugins and configures the backend for the working directory
- Cterraform refresh, which updates the state file from the real infrastructure without any planning
- Dterraform state push, which uploads a local state file into the remote backend configured for the team
Show answer and explanationHide answer
Correct answer: Bterraform init installs providers and modules and initializes the backend, which must happen before planning with a new provider. Refresh, workspace creation and state push do not install provider plugins.
Question 11
After a configuration change, terraform plan shows that a resource will be replaced. Which change is the most likely cause?
- AA change to a tag value, which the provider applies in place without recreating the resource
- BA change to the description of an output, which only affects how the next plan output is displayed
- CA change to the default workspace name, which forces every resource in the configuration to be recreated
- DA change to an argument that the provider marks as force-new, such as an immutable resource name
Show answer and explanationHide answer
Correct answer: DSome arguments cannot be modified in place, so the provider marks them as force-new and Terraform replaces the object. Tag changes are usually in place, and output descriptions and workspace names do not affect resources.
Question 12
Which command checks a configuration for syntax errors and internal consistency without accessing any remote infrastructure?
- Aterraform plan, which contacts provider APIs to read the current state of every resource first
- Bterraform show, which lists the resources in the current state for the configured backend in readable form
- Cterraform apply -refresh-only, which reads real infrastructure to update the state before any checks run at all
- Dterraform validate, which checks syntax and internal consistency without accessing any remote infrastructure
Show answer and explanationHide answer
Correct answer: Dterraform validate checks the configuration without reaching providers or state, which makes it quick to run in CI. Plan, refresh-only apply and show all read remote data or state.
Interact with Terraform modules (4 questions)
Question 13
Which block calls a child module from a root configuration and passes input values to it?
- AA resource block, which declares a managed infrastructure object together with its arguments
- BA data block, which reads an existing object without managing its lifecycle in the configuration
- CA provider block, which sets the credentials and the region that are used for API calls
- DA module block, which names a source and passes input variables to the child module
Show answer and explanationHide answer
Correct answer: DA module block references the child module's source and supplies its input variables. Resource, data and provider blocks do not call child modules.
Question 14
Which practice keeps consumers of a shared module from breaking when the module changes?
- APin the module source to a version constraint, and publish breaking changes as a new major version
- BReference the module from the main branch so that every consumer always receives the latest code
- CCopy the module source into each repository once and never update the copies after that point
- DUse the unversioned commit hash of whichever branch was most recently pushed by the team
Show answer and explanationHide answer
Correct answer: AVersion constraints let consumers adopt changes deliberately, and a new major version signals breaking changes. Tracking the main branch or copying code forces every change on consumers, and unversioned hashes give no stable contract.
Question 15
A root module references a child module from a registry source. What does terraform init do for that module?
- ACreates a separate state file for each module that the root configuration references
- BApplies the module's resources to the cloud account before any plan output is displayed
- CDownloads the module source into the .terraform directory so that plan can read and use it
- DDeletes any local copy of the module and replaces it with the remote version on every run
Show answer and explanationHide answer
Correct answer: Cterraform init retrieves referenced modules into the .terraform directory so that later commands can evaluate them. It does not apply resources, delete local copies or create separate state files.
Question 16
Which two elements belong in a well-designed module interface? (Choose two.)
- AHard-coded account IDs inside the resources, so that the module always targets one account
- BInput variables with descriptions and validation rules for each setting that consumers configure
- COutputs that expose the identifiers that other configurations need in order to reference the resources
- DProvider blocks inside the module that embed credentials for every consumer of the module
- EUndocumented defaults that differ between environments without any explanation given to consumers
Show answer and explanationHide answer
Correct answer: B, CDocumented, validated inputs and outputs form the contract between a module and its callers. Hard-coded accounts, embedded credentials and undocumented defaults make modules unsafe or hard to reuse.
Understand Terraform Cloud (3 questions)
Question 17
Which Terraform Cloud feature evaluates a run's plan against organization-wide rules before the apply step?
- APrivate registry, which stores and versions reusable modules for the organization's teams to share
- BVCS connection, which starts runs automatically when commits are pushed to the linked repository
- CPolicy as code, such as Sentinel or OPA policies that evaluate the plan before it is applied
- DVariable sets, which share environment variables and Terraform variables across many workspaces
Show answer and explanationHide answer
Correct answer: CPolicy as code evaluates the plan against rules, such as allowed regions or tag requirements, and can block the apply. The registry, variable sets and VCS integration support sharing and triggering runs rather than enforcing plan rules.
Question 18
Which workspace setting keeps applies behind a manual approval step?
- ASpeculative plans enabled, so that plans run on pull requests and no apply step is ever created
- BApply method set to manual, so that a run waits for an authorized user to confirm the apply
- CAuto-apply turned on, so that every successful plan is applied immediately without any review
- DRemote execution disabled, so that the plan runs on the engineer's laptop instead of in the cloud
Show answer and explanationHide answer
Correct answer: BWith manual apply, the run pauses after the plan until someone confirms it, which is the approval gate. Auto-apply removes the gate, speculative plans never apply, and local execution does not add approval.
Question 19
Which two features support team collaboration in HCP Terraform? (Choose two.)
- ARemote runs, with workspace-level state management and a history of every run
- BA local-only mode that prevents any shared state or run history from being recorded anywhere
- CUnlimited free access to every private provider from the public registry, with no restrictions
- DA requirement that every configuration be written in JSON rather than in the HCL syntax
- ERole-based access control for workspaces, variables, and runs, assigned to teams
Show answer and explanationHide answer
Correct answer: A, ERemote runs and state with run history, plus role-based access on workspaces, support shared, auditable work. Local-only modes, unlimited private provider access and JSON-only rules are not features of HCP Terraform collaboration.
Understand infrastructure as code (IaC) concepts (3 questions)
Question 20
Which benefit is the main reason to manage infrastructure as code rather than through a console?
- ADrift becomes impossible, because the tool blocks every manual change that is made in the console
- BCloud providers reduce their prices for teams that keep infrastructure definitions in a git repository
- CEngineers no longer need to understand the resources, because the tool manages them entirely on their behalf
- DChanges can be reviewed, versioned, and reproduced consistently from the same definitions
Show answer and explanationHide answer
Correct answer: DStoring definitions in version control makes changes reviewable and repeatable, which is the central value of IaC. Price discounts for repositories are not a benefit of the approach, and drift can still happen when people change resources outside the code.
Question 21
Which statement best describes declarative infrastructure as code compared with imperative?
- ADeclarative tools run shell scripts one step at a time, while imperative tools describe the desired end state
- BImperative tools keep state automatically, while declarative tools never store any record of the resources they manage
- CDeclarative tools require manual approval for every change, while imperative tools apply each change instantly
- DDeclarative tools describe the desired end state and work out the steps, while imperative tools specify each step
Show answer and explanationHide answer
Correct answer: DA declarative configuration states the end result and the tool plans the steps needed to reach it, while an imperative approach spells out each action in order. The other statements reverse the two models or describe approval workflows that are unrelated to the distinction.
Question 22
Which two benefits come from idempotent infrastructure as code runs? (Choose two.)
- AEvery run creates duplicate resources so that the team has spare capacity available for growth
- BRepeated runs with the same configuration converge on the same infrastructure state
- CEngineers are no longer needed to review changes before they are applied in production
- DDrift can be detected by comparing real infrastructure with the declared configuration
- EResources keep running after the configuration that declared them has been removed from the repository
Show answer and explanationHide answer
Correct answer: B, DIdempotent runs converge on the declared state, and comparing reality with the declaration reveals drift. Duplicate creation, removing review and orphaned resources are the opposite of what idempotent, declarative management provides.
Understand Terraform basics (3 questions)
Question 23
Which block names a value that can be referenced elsewhere in a module but is not an input supplied by the caller?
- AA variable block, which accepts a value supplied by the caller at plan or apply time
- BAn output block, which exposes a value to the caller after apply completes successfully
- CA locals block, which names an expression so that it can be reused within the same module
- DA provider block, which configures authentication and the region used for API calls
Show answer and explanationHide answer
Correct answer: CLocals name computed expressions for reuse inside a module and are not inputs from outside. Variables come from callers, outputs go back to callers, and provider blocks configure API access.
Question 24
A variable is declared with sensitive set to true. What is the effect?
- ATerraform encrypts the variable in the state file, so that no value is ever stored in plaintext
- BTerraform redacts the value in plan and apply output, but the value is still recorded in the state
- CThe variable cannot be referenced by any resource or data source anywhere in the configuration
- DTerraform removes the value from state after each apply, so that it cannot be recovered later
Show answer and explanationHide answer
Correct answer: BThe sensitive flag hides values in CLI output, but the state still records them, so state must be protected separately. The other options claim encryption, a reference ban or state removal that Terraform does not perform.
Question 25
Which block reads attributes of an existing infrastructure object without making Terraform manage its lifecycle?
- AA module block, which packages several resources behind a single reusable interface for callers
- BA data block, which reads an existing object's attributes without managing its lifecycle
- CAn output block, which exposes values from the state to the caller of the configuration
- DA resource block, which creates and manages the object on every apply run in the workspace
Show answer and explanationHide answer
Correct answer: BA data source reads an object that exists outside the configuration, so its attributes can be used without taking ownership. Resources manage objects, modules package resources and outputs expose values.
Navigate the Terraform workflow (3 questions)
Question 26
Which command shows the changes Terraform would make, without changing any infrastructure?
- Aterraform apply -auto-approve, which applies the changes immediately without asking for confirmation
- Bterraform destroy -target, which removes selected resources after the user confirms the destruction
- Cterraform plan, which computes and displays the changes needed to reach the declared configuration
- Dterraform import, which adds an existing resource to the state file in the current working directory
Show answer and explanationHide answer
Correct answer: Cterraform plan computes the proposed changes and displays them without applying anything. Apply, destroy and import all modify infrastructure or state, so they do not meet the requirement for a read-only preview.
Question 27
Which file preserves a plan so that exactly the same changes can be applied later?
- AA saved plan file created with terraform plan -out, which apply can use as its input
- Bbackend.hcl, which configures where the remote state is stored for the working directory
- Cterraform.tfvars, which stores variable values for each environment in the working directory
- Dterraform.lock.hcl, which records provider versions and checksums for the configuration
Show answer and explanationHide answer
Correct answer: AA plan file saved with -out captures the exact proposed changes, and applying it runs those changes without re-planning. The variable file, backend file and lock file configure inputs, storage and provider versions rather than capturing a plan.
Question 28
Which step in the core Terraform workflow comes immediately before apply?
- ADestroy, where Terraform removes resources that are no longer declared anywhere in the configuration files
- BPlan, which computes the changes needed to reach the declared configuration
- CInit, where Terraform downloads providers and modules before any other workflow step is able to run at all
- DWrite, where the engineer edits the configuration files in the working directory before running anything at all
Show answer and explanationHide answer
Correct answer: BThe core workflow is write, plan, apply, so the plan is the step that precedes apply. Init comes first, writing comes before planning, and destroy is a separate operation.
Understand Terraform's purpose (2 questions)
Question 29
Which characteristic allows Terraform to manage resources across many cloud platforms with one workflow?
- AProviders that map resource types to each platform's API, so one configuration language covers many clouds
- BA built-in compiler that translates configuration into native binaries for each cloud provider separately
- CA single cloud vendor's SDK that is embedded in every Terraform release for all platforms at once
- DAutomatic conversion of templates from one provider's format to another's when each plan is generated
Show answer and explanationHide answer
Correct answer: ATerraform's providers translate the same HCL resource model into calls to each platform's API, which is what makes it multi-cloud. Compilers, a single vendor SDK and automatic template conversion do not describe how providers work.
Question 30
Which statement describes Terraform's role compared with a configuration management tool such as Ansible?
- ATerraform manages the software configuration inside existing servers, while configuration tools provision cloud resources and networks
- BTerraform is limited to Kubernetes clusters and cannot manage resources that exist outside containers or cluster workloads
- CTerraform provisions infrastructure through provider APIs, while configuration tools manage software inside hosts
- DTerraform replaces the operating system package manager, so it installs application packages on each server directly over SSH
Show answer and explanationHide answer
Correct answer: CTerraform provisions infrastructure through provider APIs, and configuration management tools manage the software and settings inside hosts. The other statements swap these roles or wrongly limit Terraform to Kubernetes.