Free HashiCorp Certified: Terraform Associate (004) practice — 6 questions on Write and maintain Terraform configuration (HCL), with explanations. No sign-up.
Full 12-question mixed test →
Question 1 of 6 · Write and maintain Terraform configuration (HCL)
A module declares:
variable "instances" {
type = list(object({
name = string
size = string
}))
}
The team wants exactly one aws_instance resource created per list entry, addressable in state by the instance's `name` value (so reordering the list doesn't force replacements). Which for_each expression on the resource block satisfies this?
for_each requires a map or a set of strings. Converting the list of objects into a map keyed by `name` gives Terraform a stable key so reordering the source list doesn't cause resources to be destroyed/recreated, and each.value still exposes the full object for other attributes.
Question 2 of 6 · Write and maintain Terraform configuration (HCL)
An aws_security_group resource must generate a variable number of ingress rules from a list variable `var.ingress_rules`, each containing `port` and `cidr` fields. Which construct correctly generates these nested blocks?
A dynamic block is required to generate a variable number of nested configuration blocks (like ingress) from a collection. Inside the content block, the iterator variable defaults to the block's label name — here 'ingress' — so `each.value` is wrong; the correct reference should be `ingress.value`. Wait — actually the correct answer generates the block using `dynamic` with the proper iterator; among the given choices, only the dynamic block options are structurally valid, and this one correctly matches Terraform's default iterator naming convention semantics tested on the exam (the block label is the default iterator name).
Question 3 of 6 · Write and maintain Terraform configuration (HCL)
Which Terraform block type does NOT support a `postcondition` (while supporting `precondition`)?
Output value blocks support only `precondition` blocks, which validate the output's value before it is emitted. Output blocks cannot contain `postcondition` blocks; that capability is reserved for resource and data source lifecycle blocks and check blocks.
Question 4 of 6 · Write and maintain Terraform configuration (HCL)
A variable must only accept strings matching the pattern of a valid AWS instance type family, such as 't2.micro' or 'm5.large', without Terraform crashing on a malformed value during validation. Which validation condition is correct?
regex() raises an error if the pattern doesn't match, which would crash validation entirely rather than producing a clean false result. Wrapping it in can() catches that error and converts it into a boolean, making it safe to use as a validation condition that returns false on non-matching input instead of erroring out.
Question 5 of 6 · Write and maintain Terraform configuration (HCL)
A module output must return a value derived from a resource attribute marked sensitive by the provider, but the consuming root module needs to use that value as a map key elsewhere, which Terraform disallows for sensitive values. What should be done in the module to make this usable, while acknowledging the security tradeoff?
nonsensitive() explicitly strips the sensitive marking from a value, which is the only built-in mechanism for deliberately downgrading a value so it can be used in contexts (like map keys) that reject sensitive values. This should be done with care since it removes Terraform's redaction protections for that value going forward.
Question 6 of 6 · Write and maintain Terraform configuration (HCL)
A resource block references an output of a module only inside a `provisioner "local-exec"` command string that Terraform cannot statically parse for dependency purposes (e.g., embedded in a script argument built via templatefile()). The resource must still be created only after that module's resources are fully provisioned. What should be added?
When a reference is buried inside constructs Terraform can't reliably parse for its dependency graph (such as inside a rendered script string via templatefile), implicit dependency detection may not be sufficient. Adding depends_on = [module.network] explicitly forces the ordering, ensuring the module's resources are fully provisioned first regardless of how the reference is used internally.
Ready for the real thing?
The full course: two full-length practice tests, video lessons for every exam domain, hands-on labs and detailed explanations.
undefined$34.99 with code FREETEST33 — valid through Sep 23.