All HashiCorp exams
Free Terraform Authoring & Operations Advanced practice test: 30 questions with answers
30 original questions for Terraform Authoring & Operations Advanced, 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
- 240 min
Terraform Authoring & Operations Advanced exam guide: topics, cost and format
All 30 Terraform Authoring & Operations Advanced questions with answers
The same questions as the test above, grouped by exam topic. Open any question to see the answer and explanation.
Manage resource lifecycle (5 questions)
Question 1
A CI pipeline runs `terraform plan -out=tfplan` on a merge request. After a reviewer approves the output, the deploy job runs `terraform apply tfplan`. What does the deploy job do?
- AIt evaluates the configuration again and applies whatever differs at that moment, ignoring any earlier review.
- BIt applies only the changes recorded in the saved plan, without planning again.
- CIt imports every resource found in the cloud account into the state file first.
- DIt writes a JSON copy of the plan and makes no changes to the infrastructure.
Show answer and explanationHide answer
Correct answer: BA saved plan file records the exact changes that were reviewed, so applying it runs those changes without a new plan. Option A describes an apply with no saved plan, option C describes an import, and option D describes plan output rather than an apply.
Question 2
Which command is equivalent to running terraform destroy?
- Aterraform apply -destroy
- Bterraform plan -destroy
- Cterraform apply -refresh-only
- Dterraform state rm
Show answer and explanationHide answer
Correct answer: Aterraform destroy is a shortcut for terraform apply -destroy, which removes all managed resources after confirmation. The plan variant only previews the removal, refresh-only changes state without destroying anything, and state rm forgets resources without deleting them.
Question 3
An engineer runs `terraform state rm aws_s3_bucket.logs` because the bucket is now owned by another team's configuration. What is the result?
- AThe bucket keeps existing, but Terraform stops tracking it in its state.
- BThe bucket is renamed so that it matches the address of the new configuration.
- CThe bucket is deleted from the cloud account during the next apply.
- DThe bucket is exported to a JSON file and then removed from the cloud account.
Show answer and explanationHide answer
Correct answer: Aterraform state rm only edits the state file, so the real bucket keeps running while Terraform no longer tracks it. Deleting, renaming, or exporting the bucket would require an apply or a separate action, and none of those happen during a state removal.
Question 4
Which two statements about terraform init are correct? (Choose two.)
- AIt downloads the provider plugins and modules that the configuration requires.
- BIt creates the infrastructure described in the configuration files.
- CIt initializes the backend that stores the state, using the configured settings.
- DIt refreshes the state of every managed resource in the workspace before planning.
Show answer and explanationHide answer
Correct answer: A, Cterraform init installs providers and modules and sets up the configured backend, and it is safe to run again. It does not create or refresh infrastructure, which happens during plan and apply.
Question 5
An administrator changed a security group rule directly in the cloud console. The team wants Terraform's state to match the real rule without changing any infrastructure. Which command fits?
- Aterraform apply -replace=aws_security_group.app to rebuild the group
- Bterraform apply -refresh-only to update the state from real infrastructure
- Cterraform state rm aws_security_group.app to forget the group entirely
- Dterraform import aws_security_group.app sg-0abc123 to adopt the group again
Show answer and explanationHide answer
Correct answer: BThe -refresh-only mode reads real infrastructure and writes the result to state, with no configuration-driven changes to resources. The replace option would recreate the group, state rm forgets it, and importing a resource that Terraform already manages fails.
Develop and troubleshoot dynamic configuration (5 questions)
Question 6
Which description of the try function is correct?
- AIt retries a failing provider API call until a successful response is returned.
- BIt returns true when an expression is valid and false when it produces an error.
- CIt returns the first expression that evaluates without an error.
- DIt returns a fallback only when a variable is null, and errors on other values.
Show answer and explanationHide answer
Correct answer: Ctry evaluates its arguments in order and returns the first one that succeeds, which makes it useful for fallback values. The can function is the one that returns a boolean for validity, while the other options describe behaviour that try does not have.
Question 7
A variable named region is set in terraform.tfvars, exported as TF_VAR_region, and passed as -var region=eu-west-1 on the command line. Which value does Terraform use?
- AThe value in terraform.tfvars, because tfvars files are read after the others.
- BThe TF_VAR_region environment variable, because environment values override files.
- CTerraform stops with an error because the variable is defined in three places.
- DThe -var value passed on the command line, which has the highest precedence.
Show answer and explanationHide answer
Correct answer: DValues passed with -var or -var-file on the command line have the highest precedence, above tfvars files, and environment variables have the lowest. The file, environment, and error options each misstate that order or wrongly treat the duplicate definitions as a failure.
Question 8
A module output that carries a database password is marked sensitive = true. Which statement about this output is correct?
- ATerraform refuses to pass the output to any other module or configuration block.
- BThe value is encrypted in the state file, so the state can be committed to Git safely.
- CThe value is removed from the plan and is never sent to the provider at all.
- DThe value is hidden in CLI output but still stored in plain text in the state file.
Show answer and explanationHide answer
Correct answer: DThe sensitive flag only redacts the value in plan and apply output and does not encrypt state, so the state must still be protected. Sensitive outputs can still be passed to other modules, and the value still reaches the provider when it is needed.
Question 9
A configuration must use the ID of a VPC created by another team's configuration, and it must not manage that VPC. Which block should it use to look the VPC up?
- AA resource block with the same type, so Terraform adopts the VPC automatically.
- BA data block that reads the existing VPC using its attributes or tags.
- CA module block that copies the VPC definition into this configuration code.
- DAn output block that exports the VPC ID from the state of this configuration.
Show answer and explanationHide answer
Correct answer: BA data block reads existing objects during planning without creating or managing them. A resource block with the same type would try to manage a VPC, and module or output blocks cannot look up objects that belong to another state.
Question 10
A variable named instance_type must accept only t3.micro or t3.small. Which approach enforces this rule?
- ASet type = string so that only lowercase values are accepted.
- BAdd a validation block whose condition checks the value against the allowed list.
- CAdd a sensitive = true argument so that invalid values are masked in the output.
- DAdd a precondition block to the provider configuration to check the instance type.
Show answer and explanationHide answer
Correct answer: BA validation block in a variable evaluates a condition on the input and fails with the custom error message when the condition is false. The type argument checks only the data type, sensitive hides values, and provider blocks do not support precondition blocks.
Develop collaborative workflows (5 questions)
Question 11
A team's repository contains a .terraform.lock.hcl file created by terraform init. What is the recommended handling?
- AAdd it to .gitignore because it records the local cache path of each provider on that machine.
- BDelete it before each run so the newest provider versions are always selected.
- CCommit it so every team member and CI run installs the same provider versions and hashes.
- DStore it only in the remote backend, because it records secret values from state.
Show answer and explanationHide answer
Correct answer: CThe dependency lock file records the provider versions and checksums chosen during init, so committing it keeps installs reproducible. Ignoring it, deleting it before each run, or keeping it only in a backend would remove that consistency, and the file holds no secrets from state.
Question 12
A required_providers entry sets the constraint ~> 5.2.0. Which provider versions does Terraform accept?
- AVersions from 5.2.0 or higher, but only up to, not including, 5.3.0.
- BOnly version 5.2.0 exactly, so upgrades require editing the constraint each time.
- CAny version 5.2.0 or higher, including 6.x releases and later.
- DAny 5.x version from 5.0.0 upward, with no upper limit inside the major version.
Show answer and explanationHide answer
Correct answer: AThe pessimistic operator lets only the last listed number increase, so ~> 5.2.0 allows 5.2.x and nothing from 5.3.0 upward. The other options allow major-version jumps, a single exact version, or unlimited minor upgrades, which the operator prevents.
Question 13
Team A's network configuration exports vpc_id as an output. Team B's application configuration needs that value without copying the network code. What should Team B use?
- AA terraform_remote_state data source that points at the network state's backend.
- BA resource block that re-creates the VPC inside the application configuration files.
- CA module block that includes the network configuration source code directly.
- DA moved block that transfers the network resources into the application state.
Show answer and explanationHide answer
Correct answer: Aterraform_remote_state reads the outputs of another configuration's state from its backend, so Team B can use exported values without duplicating code or resources. A resource block would create a second VPC, a module block would copy code, and a moved block only relocates addresses within one state.
Question 14
A CI job runs terraform apply and stalls at an interactive prompt. Which change makes the job non-interactive while keeping the reviewed changes?
- ARun terraform init -upgrade before every apply to skip the confirmation step.
- BDelete the entire backend block so Terraform does not ask for any state storage details.
- CAdd -input=false and apply the saved plan file produced by the reviewed plan step.
- DReplace the apply step with terraform console to evaluate the plan's expressions.
Show answer and explanationHide answer
Correct answer: CThe -input=false flag stops Terraform from prompting for missing values, and applying a saved plan runs the exact reviewed changes without an interactive confirmation. Upgrading providers, deleting the backend, or using console does not address the prompt, and deleting the backend would break state storage.
Question 15
Which two statements about state locking are correct? (Choose two.)
- AA backend with locking stops two runs from writing the same state at the same time.
- BLocking is turned off automatically whenever the -auto-approve flag is used.
- Cterraform force-unlock is safe only when no other operation still holds the lock.
- DLock details are saved in the .terraform.lock.hcl file alongside provider checksums.
Show answer and explanationHide answer
Correct answer: A, CState locking prevents concurrent operations from corrupting state, and force-unlock is a recovery command that is risky while another run is active. Auto-approve does not disable locking, and the lock file records provider checksums rather than lock records.
Create, maintain, and use modules (5 questions)
Question 16
A team wants to reference a module stored in a Git repository at the release tag v2.1.0, so the version stays reproducible. Which source is correct?
- Agit::https://git.example.com/platform/modules.git//network with version = 2.1.0 set in the module block
- B./vendor/modules/network?ref=v2.1.0
- Cgit::https://git.example.com/platform/modules.git//network?tag=v2.1.0
- Dgit::https://git.example.com/platform/modules.git//network?ref=v2.1.0
Show answer and explanationHide answer
Correct answer: DGit module sources use the ref query parameter to select a tag or commit, and the double slash selects a subdirectory of the repository. The version argument works only for registry modules, a local path cannot carry a Git ref, and tag is not a recognised source parameter.
Question 17
Which two statements about module inputs and outputs are correct? (Choose two.)
- AA caller reads a child module's output with module.<name>.<output>.
- BInput variables are supplied as arguments inside the module block.
- CA child module automatically receives every variable of its parent without any arguments.
- DA child module can reference the parent's resources directly by their resource address.
Show answer and explanationHide answer
Correct answer: A, BModule outputs are accessed through the module address, and module inputs are supplied as arguments in the module block. Child modules do not inherit parent variables or resources, so any value they need must be passed in explicitly.
Question 18
An aws_instance.web resource in the root configuration moves into a module named app, and the team must avoid destroying the instance. Which change achieves this?
- ADelete the resource from the root and recreate it inside module.app with the same settings.
- BAdd a moved block from aws_instance.web to module.app.aws_instance.web.
- CRun terraform destroy -target on the old address before adding the module block.
- DAdd a removed block for aws_instance.web and a new resource inside the module.
Show answer and explanationHide answer
Correct answer: BA moved block tells Terraform that an existing object now has a new address, so the plan shows a move instead of a destroy and create. The other options either destroy the instance or leave Terraform creating a second one.
Question 19
A reusable module must deploy into a provider configuration aws.west that the root configuration defines. Which approach is recommended?
- AHardcode the region and credentials in each resource block inside the module.
- BDeclare a provider block with alias west inside the module source code itself.
- CUse the providers argument in the module block to pass aws.west from the root.
- DCopy the module into each region directory so it can use the default provider.
Show answer and explanationHide answer
Correct answer: CThe providers argument in a module block maps the module's provider names to configurations defined by the caller, which keeps provider blocks in the root. Declaring providers inside reusable modules or hardcoding credentials makes the module harder to reuse and is discouraged.
Question 20
A team uses the registry module terraform-aws-modules/vpc/aws and wants to accept updates only within major version 5. Which version argument fits?
- Aversion = "~> 5.0", which allows any 5.x release from 5.0 onward
- Bversion = ">= 5.0", which allows any release from 5.0 upward, including major version 6
- Cversion = "5.0.0" exactly, which pins the module to a single release
- Dversion = "~> 5.0.0" set in the required_providers block, which constrains the module
Show answer and explanationHide answer
Correct answer: AThe pessimistic operator in a module's version argument allows 5.x releases at or above 5.0 and blocks 6.0. An exact pin, an open-ended constraint, or a constraint placed in required_providers does not give that behaviour, since required_providers applies to providers.
Configure and use providers (5 questions)
Question 21
Two resources in one configuration must use different regions. One resource must use the aliased provider aws.west. Which meta-argument selects that provider for the resource?
- AThe provider meta-argument, set to aws.west
- BThe alias argument inside the resource block
- CA separate terraform block that overrides the default provider for the resource
- DThe depends_on argument pointing at the aws.west provider block
Show answer and explanationHide answer
Correct answer: AThe provider meta-argument picks which provider configuration a resource uses, and aliases are declared in provider blocks rather than resource blocks. A terraform block does not select providers per resource, and depends_on controls ordering rather than provider choice.
Question 22
Where should provider version constraints be declared so that terraform init enforces them?
- AIn a version argument on every resource block that uses that provider
- BIn an environment variable named TF_PROVIDER_VERSION read at init time
- CIn the .terraform.lock.hcl file, which the team edits by hand for each version
- DIn the required_providers block nested inside the top-level terraform block
Show answer and explanationHide answer
Correct answer: Drequired_providers inside the terraform block declares each provider's source and version constraint, and init enforces it. Resource blocks have no version argument, the lock file records selected versions rather than constraints, and no such environment variable exists.
Question 23
How does Terraform core work with a provider such as the AWS provider?
- ATerraform core calls cloud APIs directly, and the provider only supplies schema data.
- BThe provider runs as a separate plugin process and communicates over RPC.
- CThe provider is compiled into the Terraform binary and loaded when it starts.
- DThe provider keeps its own state file that Terraform merges in during apply runs.
Show answer and explanationHide answer
Correct answer: BProviders are separate plugin programs that Terraform starts and talks to over RPC, which is why init has to download them. The other options wrongly place API calls in core, bundle the provider into the binary, or merge separate state files.
Question 24
A provider call fails with an unclear error, and the engineer needs detailed debug logs without changing the configuration. Which approach fits?
- AAdd a debug = true argument to every provider block in the configuration files.
- BRun terraform validate with the -json flag, which prints provider API responses.
- CDelete the .terraform directory and rerun init with verbose output enabled.
- DSet TF_LOG to a level such as TRACE, optionally with TF_LOG_PATH for a file.
Show answer and explanationHide answer
Correct answer: DTF_LOG controls log verbosity for Terraform core and provider plugins, and TF_LOG_PATH writes that output to a file. Provider blocks have no debug argument, and validate does not call provider APIs, so it cannot print their responses.
Question 25
Which two practices are recommended for provider authentication? (Choose two.)
- AUse environment variables, credential profiles, or workload identity instead of literal keys.
- BCommit access keys to the provider block so that every workspace shares the same credentials.
- CInject credentials from a secrets manager at runtime and keep them out of version control.
- DStore long-lived keys in a terraform.tfvars file that is committed with the configuration.
Show answer and explanationHide answer
Correct answer: A, CDynamic or externally managed credentials keep secrets out of code and out of version control, which is the recommended approach. Committing keys to provider blocks or to a committed tfvars file exposes them to everyone with repository access.
Collaborate on infrastructure as code using HCP Terraform (5 questions)
Question 26
A pull request is opened against a repository connected to an HCP Terraform workspace. What does HCP Terraform do by default?
- AIt runs a speculative plan that shows proposed changes but cannot be applied.
- BIt applies the changes automatically if the workspace uses auto-apply.
- CIt creates a separate workspace for each branch and copies every variable value over.
- DIt locks the workspace state until the pull request is closed or merged.
Show answer and explanationHide answer
Correct answer: ASpeculative plans run from pull requests in VCS-connected workspaces and show proposed changes, but they are never applied, even if auto-apply is enabled. The other options describe applying, locking, or creating workspaces, which speculative plans do not do.
Question 27
A developer must review plan results and queue plan runs in a workspace, but must not apply changes. Which workspace permission fits?
- ARead permission, which allows viewing runs without queuing anything
- BPlan permission, which allows queuing plans without the ability to apply
- CWrite permission, which allows applying runs and editing workspace variables
- DAdmin permission, which allows managing workspace settings and deleting the workspace
Show answer and explanationHide answer
Correct answer: BThe Plan workspace permission lets a user queue plan runs and see their results while blocking apply. Write adds applying runs, Read is limited to viewing, and Admin covers workspace settings and deletion.
Question 28
A Sentinel policy set is enforced at hard-mandatory level and one policy fails during a run. What happens?
- AThe run continues, but the failure is logged in the run history only.
- BUsers with permission can override the failure and proceed to apply.
- CThe run stops before apply, and no one can override the failure.
- DThe policy is skipped, because policies run only after apply completes.
Show answer and explanationHide answer
Correct answer: CHard-mandatory policies stop the run before apply when they fail, and no one can override them, while soft-mandatory policies can be overridden by authorized users. Policy checks run between plan and apply, so the option that places them after apply is wrong.
Question 29
Five workspaces in one HCP Terraform organization need the same cloud provider credentials, managed centrally. Which feature is designed for this?
- AA separate organization for each workspace, each holding its own copy of credentials
- BA copy of the credentials in each workspace's terraform.tfvars file
- CA variable set that is assigned to all five workspaces and managed centrally
- DOne shared state file that holds the credentials for all five workspaces
Show answer and explanationHide answer
Correct answer: CVariable sets let an organization define values such as provider credentials once and apply them to several workspaces. Copying values per workspace, creating extra organizations, or sharing a state file do not provide that central management.
Question 30
A team wants Terraform runs to execute on its own machines, while the state is still stored and locked in HCP Terraform. Which execution mode fits?
- ARemote execution mode, where HCP Terraform performs plans and applies in its own runners
- BA speculative plan, which runs only on pull requests and never stores state
- CLocal execution mode, where runs happen on the team's machines and state stays in HCP Terraform
- DSelf-hosted state mode, which copies the state to the team's servers after each apply
Show answer and explanationHide answer
Correct answer: CLocal execution runs plans and applies on the team's own machines while the workspace still stores and locks state in HCP Terraform. Remote execution moves the runs into HCP Terraform, speculative plans are a plan-only trigger, and the last option describes state copying that HCP Terraform does not do after each apply.