HashiCorp Certified: Terraform Associate (004) Terraform Associate 004 Practice Test 3
Question 20
The same Terraform module should deploy three environment sizes without copying its HCL files. Instance size is the only required difference. What interface should the module expose?
- An input variable for instance size, with validated values or a suitable default.
- A separate Terraform binary for each instance size.
- An output value that forces users to edit provider state.
- A manual comment next to each resource that a reviewer must interpret.
Explanation
Correct Answer
A:Module variables parameterize configuration so the same source can be reused.
Incorrect Answers
B:The Terraform executable is not the per-environment configuration interface.
C:Outputs publish values; they do not replace module inputs.
D:Comments cannot supply typed module arguments.
Question 21
A module expects each service specification to contain a port number, protocol string, and an enabled flag. Callers sometimes swap positional list items accidentally. Which Terraform type helps enforce named fields and their types?
- list(any) with positional indexes only.
- A single string with comma-separated values and no parser.
- set(string) with no support for per-field types.
- object({ port = number, protocol = string, enabled = bool }).
Explanation
Correct Answer
D:An object constraint validates the presence and type of named attributes.
Incorrect Answers
A:A loosely typed list does not give each field a stable name/type contract.
B:Encoding heterogeneous fields in a string loses useful type checking.
C:A string set cannot represent distinct typed attributes as requested.
Question 22
A Terraform configuration creates one service per customer name. When one customer is removed, the team wants other customer resources to keep stable addresses instead of shifting indexes. Which iteration pattern helps?
- Use count based on list position and reorder the list freely.
- Use for_each with a map keyed by stable customer identifiers.
- Create one giant resource for all customers and manually edit it.
- Use terraform fmt to rearrange state addresses automatically.
Explanation
Correct Answer
B:for_each uses stable keys in resource addresses, reducing index churn.
Incorrect Answers
A:List indexes can shift when entries are inserted or removed.
C:One resource does not represent independent per-customer objects when the provider requires separate objects.
D:Formatting does not manage resource identity.
Question 23
A deployment resource does not reference an IAM-policy resource, but its API call must wait until that policy exists. The provider has no way to infer the relationship from attributes. What should be added?
- A depends_on reference to the policy resource on the deployment resource.
- A new output block that is never referenced.
- A random delay inserted into the user’s terminal workflow.
- A terraform fmt comment saying apply last.
Explanation
Correct Answer
A:Explicit depends_on expresses ordering when the dependency is real but not visible through value references.
Incorrect Answers
B:Unused outputs do not introduce graph edges for the resource.
C:A timed pause is unreliable and does not model dependencies.
D:Comments are not dependency declarations.
Question 24
A network module accepts a requested port number. The operations policy allows only ports 1024 through 65535 and wants invalid input rejected during configuration evaluation. Which mechanism is most direct?
- An output description stating the desired range.
- A provider alias named valid_port.
- A validation block on the input variable with an appropriate numeric condition.
- A terraform state rm command after apply fails.
Explanation
Correct Answer
C:Variable validation checks a condition and produces an error for invalid values.
Incorrect Answers
A:Descriptions document intent but cannot enforce it.
B:Provider aliases configure provider instances, not input invariants.
D:Forgetting state cannot validate configuration input.
Question 25
A Terraform variable is marked sensitive, and the pipeline no longer prints the secret in ordinary plan output. A teammate concludes the secret can never appear in state. What is the correct security conclusion?
- The sensitive annotation guarantees encryption of all local and remote copies.
- A masked plan means credentials are removed automatically from every provider request.
- Sensitive values can be passed openly through public outputs without risk.
- Sensitive marking redacts routine output but does not by itself prevent values from being stored in state; protect state or use supported ephemeral patterns.
Explanation
Correct Answer
D:State must be access-controlled and encrypted, or design should avoid persistence where supported.
Incorrect Answers
A:Sensitivity controls UI disclosure, not guaranteed backend encryption.
B:Providers may still need secret values to configure real resources.
C:Publicly exposing sensitive data can leak credentials.
Question 26
An engineer replaces a resource block for a managed storage bucket with a data block for that same bucket. What happens to Terraform’s relationship with the bucket after an appropriate state refactor?
- Terraform continues managing bucket creation and deletion because data blocks own remote objects.
- The data block can query the existing bucket, but it does not manage creation or deletion of that bucket.
- The data block always deletes the bucket when its lookup fails.
- The data block forces the provider to adopt the bucket into managed resource state.
Explanation
Correct Answer
B:Read-only data lookups can supply attributes without ownership.
Incorrect Answers
A:Data blocks are queries and do not manage full resource lifecycle.
C:Failed reads generally produce errors, not managed delete commands.
D:Importing a resource is separate from reading it as data.
Question 27
A root module needs the subnet ID created inside a child module. Directly referencing the child’s internal resource address fails from the root. What should the child provide?
- An output value for the ID, read by the parent through module.<name>.<output>.
- An arbitrary environment variable in the operator’s shell only.
- A second provider installation inside the parent module.
- A duplicate subnet resource in the root.
Explanation
Correct Answer
A:Child module outputs expose selected values across module boundaries.
Incorrect Answers
B:An unreferenced shell variable is not a module interface.
C:Providers do not expose child resource values to the parent automatically.
D:Duplicating managed infrastructure is not a safe way to read its ID.
Question 28
A module receives a map of services with enabled flags. It needs a new map containing only enabled entries, without changing the original input. Which HCL feature most directly expresses the transformation?
- terraform destroy -target for disabled services.
- An empty dynamic block with no iterator.
- A for expression over the map with an if clause.
- A state file manually edited to remove disabled keys.
Explanation
Correct Answer
C:A for expression can filter and transform collections as an expression.
Incorrect Answers
A:Destroy targets infrastructure, not a pure input-data transformation.
B:A dynamic block generates nested block configuration, not an arbitrary filtered map by itself.
D:Editing state is unrelated to building a derived configuration value.
Question 29
A service resource must be replaced to change an immutable setting. Capacity permits both old and new instances briefly, and the team wants to minimize interruption. Which lifecycle setting can help when the provider permits it?
- ignore_changes for every attribute so replacement never occurs.
- prevent_destroy permanently so no future update can proceed.
- count = 0 during the rollout.
- create_before_destroy = true for the applicable resource.
Explanation
Correct Answer
D:create_before_destroy requests creation of the replacement before deletion of the old object, subject to provider and dependency constraints.
Incorrect Answers
A:Ignoring all changes hides required updates rather than sequencing replacement.
B:Prevent-destroy blocks removal, which may prevent needed replacement.
C:Count zero explicitly requests removal rather than safe replacement.
Question 30
A managed database depends on an external subnet ID that must begin with a particular prefix. The team wants to stop an invalid configuration before attempting the database operation. Which Terraform construct fits?
- An output value named subnet_ok with no condition.
- A lifecycle precondition that asserts the required subnet-ID property.
- A log statement in the README instead of HCL validation.
- An HCP workspace description that says validate manually.
Explanation
Correct Answer
B:Preconditions can verify assumptions relevant to a resource or data source during evaluation.
Incorrect Answers
A:An unasserted output does not block invalid resource configuration.
C:Documentation cannot enforce runtime configuration conditions.
D:A workspace description is not an executable check.