Terraform Associate 004 ยท Configuration building blocks
Structural types and type constraints
object and tuple types, optional attributes, any, type conversion, picking the right type for a variable (4d).
Key points
- object({ name = string, port = number }) has named attributes, each with its own type; tuple([string, number]) is an ordered sequence of fixed length with a type per position. optional(type, default) lets callers omit an object attribute (null without a default). any lets Terraform infer one type and is rarely the right choice.
- Terraform converts compatible values automatically, for example the string "5" to the number 5.
Source: https://developer.hashicorp.com/terraform/language/expressions/type-constraints
Practice questions
Which type has named attributes where each attribute can have a different type?
- map(string)
- list(any)
- object
- set(number)
Show answer
Answer: C. object
An object type defines named attributes with individual types. map requires one value type, and list and set are collections of a single type.
Which statements about structural types are true?
- A tuple type fixes the number of elements and a type for each position
- An optional attribute without a default is null when omitted
- All elements of a tuple must share one type
- Extra keys in a value are discarded when converting to an object type
- An object type cannot have attributes of different types
Show answer
Answer: A. A tuple type fixes the number of elements and a type for each position, B. An optional attribute without a default is null when omitted, D. Extra keys in a value are discarded when converting to an object type
Tuples need the exact length and per-position types; optional without a default yields null; extra keys on an object value are discarded in conversion. A tuple is not required to hold one type.
True or false: any is the recommended type for most variables because it accepts every value safely.
Show answer
Answer: False
False. any is only a placeholder Terraform resolves to a single type; it is rarely correct and hides mistakes. Prefer explicit types.
Practice 8 questions on this topic in Bitzu
Short lessons, instant explanations and spaced review of what you get wrong. Free on iOS and Android.
