Variable Types: Complex Types
You'll learn how to use Terraform's structured variable types—list, set, map, and object—to handle groups of related values.
Beyond Primitives
While strings, numbers, and booleans (the primitives) handle single values, complex types let you organize multiple values together. Think of primitives as individual items on a shopping list, and complex types as organizing that list by category or creating a recipe with ingredients and quantities.
The Four Complex Types
List
An ordered collection of values of the same type. Use square brackets:
variable "availability_zones" {
type = list(string)
default = ["us-east-1a", "us-east-1b", "us-east-1c"]
}
Set
Like a list, but unordered and with no duplicates. Useful when order doesn't matter:
variable "allowed_ports" {
type = set(number)
default = [80, 443, 8080]
}
Map
Key-value pairs where all values share the same type. Think of it as a dictionary:
variable "instance_tags" {
type = map(string)
default = {
Environment = "production"
Team = "platform"
}
}
Object
The most flexible—a collection of named attributes with different types:
variable "server_config" {
type = object({
name = string
size = string
replicas = number
})
}
When to Use Each
Use lists for ordered sequences, sets when uniqueness matters, maps for simple lookups, and objects when you need structured configuration with mixed types.
Key Takeaway: Complex variable types let you model real-world infrastructure configurations by grouping related values into lists, sets, maps, and objects—making your Terraform code cleaner and more expressive.