Terraform Configuration
This domain covers the Terraform configuration language (HCL), its data types, built-in functions, and dynamic constructs used to write expressive, maintainable infrastructure code. HashiCorp does not publish objective weightings for Terraform Associate 004, so this is one of the estimated-even-weight topic areas under the current blueprint rather than a confirmed percentage — do not rely on a specific question count. Expect questions that test your ability to read and reason about HCL syntax, choose the correct function for a transformation, understand how dynamic blocks generate repeated nested structures, and distinguish when to use provisioners versus other mechanisms.
HCL Data Types
Terraform's type system is used for variable declarations, function arguments, and output values. Knowing the exact names and behaviors is essential.
Primitive Types
| Type | Description | Example Value |
|---|---|---|
string | UTF-8 encoded text | "hello" |
number | Integer or floating-point | 42, 3.14 |
bool | Boolean true/false | true, false |
Collection Types
| Type | Description | Element Constraint | Duplicate Keys/Values |
|---|---|---|---|
list(T) | Ordered sequence | All elements same type | Duplicates allowed |
set(T) | Unordered collection | All elements same type | No duplicates |
map(T) | Key-value pairs, string keys | All values same type | Keys unique |
Structural Types
| Type | Description | Mixed Types? |
|---|---|---|
object({...}) | Named attributes with individual types | Yes |
tuple([T, T, ...]) | Ordered sequence with per-position types | Yes |
any | No type constraint; Terraform infers | N/A |
Key distinctions for the exam:
listpreserves order;setdoes not — convertinglist→setremoves duplicates and ordering.objectattributes have named keys with distinct types;tupleelements have positional types.anydisables type checking — Terraform infers the type from the assigned value at plan time.
Common Traps
- Confusing
map(string)(all values must be strings) withobject({...})(values can be mixed types). - Assuming
setpreserves insertion order — it does not. tuplerequires a fixed number of elements matching the declared position types.anydoes not mean "accepts anything at runtime" — it means Terraform infers the type from context.
Variable Types and Type Constraints
Variables are declared with a type argument. Type constraints control what callers can pass.
variable "region" {
type = string
default = "us-east-1"
}
variable "allowed_ports" {
type = list(number)
}
variable "tags" {
type = map(string)
}
variable "server_config" {
type = object({
instance_type = string
count = number
enable_ha = bool
})
}
Type constraint syntax rules:
- Primitive:
string,number,bool— no parentheses. - Collection:
list(T),set(T),map(T)— one type argument. - Structural:
object({ key = type, ... }),tuple([T, T, ...]) listwithout a type argument is shorthand forlist(any).
Type coercion: Terraform automatically coerces compatible types. A number can be passed where a string is expected (it is converted). A bool can be coerced to string ("true"/"false"). These conversions are automatic but unidirectional in practice.
Common Traps
- Writing
type = listinstead oftype = list(string)— the former is valid (list(any)) but imprecise. objectrequires all declared attributes to be provided unless they have defaults (viaoptional()).optional()modifier insideobject(added in Terraform 1.3) allows omitting attributes.
Unlock with a paid plan
13 more sections (about 2,173 words) continue below. Every paid plan unlocks all 004 study guides, the full question bank and mock exams, from $4.97/month.