Terraform 004 configuration explained
Objective 4 of the Terraform Associate (004) is configuration: how you write HCL so that it is flexible, safe and readable. It has eight sub-objectives (4a to 4h), the most of any objective, and three of the four items new in 004 are here: dependencies (4f), custom conditions (4g) and sensitive data with Vault (4h). HashiCorp publishes no weights, so do not skip any of them.
4a and 4b: Resources, data and references
A resource block creates and manages an object. A data block only reads information that already exists, such as an existing network or an image ID. Both are referenced by type and name, and an attribute reference such as one resource’s ID used inside another creates a dependency that Terraform uses to order operations.
4c and 4d: Variables, outputs and types
Input variables parameterize a configuration; you can give them a type, default, description and validation. Values come from -var, .tfvars files, environment variables prefixed with TF_VAR_ or defaults. Outputs expose values to the user or to a parent module. Locals name an expression you reuse.
Complex types are collections (list, set, map) and structural types (object, tuple). Know which is ordered, which is unique and which has named keys.
4e: Expressions and functions
Expect conditionals (condition ? a : b), for expressions, splat expressions and count versus for_each. Use for_each when instances have stable names, because count ties instances to index numbers and removing an item in the middle shifts the rest. Know a handful of functions by purpose (lookup, merge, length, join, file, cidrsubnet) rather than by memorised argument order. Dynamic blocks generate repeated nested blocks.
4f: Dependencies
Terraform infers order from references. Use depends_on only when a dependency exists that no argument expresses. Know the difference between implicit and explicit.
4g: Custom conditions
| Where | Block | Purpose |
|---|---|---|
| Variable | validation | Reject bad input values |
| Resource or data source | precondition | Check assumptions before the object is used |
| Resource or data source | postcondition | Check the result after it exists |
| Output | precondition | Guard what you expose |
Each has a condition expression and an error message.
4h: Sensitive data and Vault
Mark variables and outputs sensitive to redact them in CLI output. This does not keep them out of state. Best practice is to keep secrets out of version control, protect the state backend and read secrets at run time from a secrets manager such as HashiCorp Vault through its provider or data sources.
Typical traps
- Treating
datablocks as creating something. - Using
countfor items that may be removed from the middle of a list. - Assuming
sensitiveencrypts state. - Putting validation in the wrong place: input checks belong in the variable block.
Sample questions
Question 1. A configuration creates three subnets from a list of names. Later, the first name is removed from the list. Which approach avoids unintended replacement of the other subnets?
- A. Use count with the list length
- B. Use for_each keyed by the subnet names
- C. Use depends_on on every subnet
- D. Use a local value to rename the list
Show answer
Answer: B
With for_each over a set or map, each instance is keyed by a stable name, so removing one key only affects that instance. With count, instances are tied to index numbers, so removing the first element shifts the rest and can cause replacements.
Want more questions like this? Full Terraform 004 practice tests →
Question 2. Where should you place a rule that rejects an input value for a variable if it is not in an allowed list?
- A. A validation block in the variable declaration
- B. A postcondition on the resource
- C. A precondition on an output
- D. A depends_on argument
Show answer
Answer: A
A validation block inside the variable declaration checks the input value and returns your error message before any planning proceeds. A postcondition checks results after creation, an output precondition guards exported values, and depends_on is only about ordering.
Want more questions like this? Full Terraform 004 practice tests →
Question 3. A resource needs a role to exist first, but none of its arguments reference the role. What is the correct way to enforce the order?
- A. Mark the role as sensitive
- B. Add a data block for the role
- C. Run terraform fmt
- D. Add depends_on pointing to the role
Show answer
Answer: D
When no attribute reference expresses the relationship, depends_on declares an explicit dependency. Data blocks do not create ordering by themselves, sensitive only hides values and terraform fmt just formats files.
Want more questions like this? Full Terraform 004 practice tests →
Next
Read modules to see how configurations are packaged, or return to the workflow page.