Module 4: Terraform Configuration
Lesson 4.4: Complex Types
| Type |
Shape |
Ordered? |
Unique? |
Example |
string, number, bool |
Primitive |
— |
— |
"dev", 3, true |
list(T) |
Sequence of one type |
✓ |
✗ |
["a", "b", "a"] |
set(T) |
Collection of one type |
✗ |
✓ |
toset(["a", "b"]) |
map(T) |
String keys → values of one type |
by key |
keys unique |
{ owner = "platform" } |
object({...}) |
Named attributes, each with its own type |
— |
— |
{ size = "small", port = 8080 } |
tuple([...]) |
Sequence with a type per position |
✓ |
✗ |
["web", 8080, true] |
any |
Placeholder: Terraform infers a concrete type |
|
|
|
HCLvariable "servers" {
type = map(object({
size = string
port = optional(number, 8080) # optional attribute with a default
}))
default = {
web = { size = "small" } # port becomes 8080
api = { size = "medium", port = 9090 }
}
}
Type conversion
- Primitives convert automatically where it makes sense.
"5" becomes 5 for a number argument, and true becomes "true" for a string.
- Structural types convert to compatible collections: a tuple → list and an object → map, when all elements can convert to one type.
- list → set removes duplicates and drops ordering.
- Explicit conversion functions:
tostring, tonumber, tobool, tolist, toset, tomap.