Terraform configuration Study Guide — 004

12% of the exam · domain 4 of 8 · HashiCorp Terraform Associate · Free Terraform configuration practice questions

0 of 41 topics covered
What the exam asks here — tap to tick off0 / 8 covered
Domain 4 of 8

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

TypeDescriptionExample Value
stringUTF-8 encoded text"hello"
numberInteger or floating-point42, 3.14
boolBoolean true/falsetrue, false

Collection Types

TypeDescriptionElement ConstraintDuplicate Keys/Values
list(T)Ordered sequenceAll elements same typeDuplicates allowed
set(T)Unordered collectionAll elements same typeNo duplicates
map(T)Key-value pairs, string keysAll values same typeKeys unique

Structural Types

TypeDescriptionMixed Types?
object({...})Named attributes with individual typesYes
tuple([T, T, ...])Ordered sequence with per-position typesYes
anyNo type constraint; Terraform infersN/A

Key distinctions for the exam:

  • list preserves order; set does not — converting list → set removes duplicates and ordering.
  • object attributes have named keys with distinct types; tuple elements have positional types.
  • any disables type checking — Terraform infers the type from the assigned value at plan time.

Common Traps

  • Confusing map(string) (all values must be strings) with object({...}) (values can be mixed types).
  • Assuming set preserves insertion order — it does not.
  • tuple requires a fixed number of elements matching the declared position types.
  • any does 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, ...])
  • list without a type argument is shorthand for list(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 = list instead of type = list(string) — the former is valid (list(any)) but imprecise.
  • object requires all declared attributes to be provided unless they have defaults (via optional()).
  • optional() modifier inside object (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.