Skip to main content

Working with Complex Data Types

1. Using Lists and Tuples

Feature Description Example
list Ordered collection of elements ["apple", "banana", "cherry"]
tuple Fixed-length ordered collection ["apple", 42, true]

Example: List Operations

variable "fruits" {
  type = list(string)
  default = ["apple", "banana", "cherry"]
}

output "first_fruit" {
  value = var.fruits[0]
}

2. Using Maps and Objects

Feature Description Example
map Collection of key-value pairs {name = "John", age = 30}
object Structured type with defined attributes {name = string, age = number}

Example: Map Usage

variable "server_config" {
  type = map(object({
    instance_type = string
    ami_id = string
  }))
  default = {
    web = {
      instance_type = "t2.micro"
      ami_id = "ami-12345678"
    }
  }
}

3. Using Sets

Feature Description Example
set Unordered collection with unique elements set(["apple", "banana"])

Example: Set Operations

variable "allowed_ports" {
  type = set(number)
  default = set([80, 443, 22])
}

output "port_list" {
  value = tolist(var.allowed_ports)
}

4. Using Nested Objects

Feature Description Example
nested object Object containing other objects or collections {database = {host = "localhost", port = 5432}}

Example: Nested Object Access

variable "environment" {
  type = object({
    web = object({
      instance_type = string
      security_group = string
    })
  })
  default = {
    web = {
      instance_type = "t2.micro"
      security_group = "sg-12345678"
    }
  }
}

output "web_instance_type" {
  value = var.environment.web.instance_type
}

5. Using Optional Object Attributes

Feature Description Example
optional attribute Attribute that may or may not be present {name = string, age = number, optional = string}

Example: Optional Attribute Handling

variable "user_config" {
  type = object({
    name = string
    age = number
    email = optional(string)
  })
  default = {
    name = "John Doe"
    age = 30
  }
}

6. Using Type Constraints

Feature Description Example
type constraint Enforce specific data types for variables type = list(string)

Example: Type Constraint Enforcement

variable "server_list" {
  type = list(object({
    name = string
    port = number
  }))
  default = [
    {
      name = "server1"
      port = 80
    }
  ]
}

7. Converting Between Types

Function Description Example
tolist Converts a set or tuple to list tolist(var.set_var)
tomap Converts a list of maps to a map tomap(var.list_var)
toset Converts a list to a set toset(var.list_var)

Example: Type Conversion

variable "ports_list" {
  type = list(number)
  default = [80, 443, 22]
}

output "ports_set" {
  value = toset(var.ports_list)
}

8. Using Structural Types

Type Description Example
object Structured type with defined attributes {name = string, age = number}
list Ordered collection of elements list(string)
map Collection of key-value pairs map(string)

Example: Structural Type Usage

variable "config" {
  type = object({
    database = object({
      host = string
      port = number
    })
  })
  default = {
    database = {
      host = "localhost"
      port = 5432
    }
  }
}

9. Merging Complex Objects

Function Description Example
merge Merges multiple maps or objects merge(var.map1, var.map2)
merge with list Merges lists or tuples merge(var.list1, var.list2)

Example: Merging Maps

variable "base_config" {
  type = map(string)
  default = {
    name = "default"
    version = "1.0"
  }
}

variable "override_config" {
  type = map(string)
  default = {
    version = "2.0"
    env = "prod"
  }
}

output "merged_config" {
  value = merge(var.base_config, var.override_config)
}

10. Flattening Nested Structures

Method Description Example
flatten Flattens nested lists into a single list flatten(var.nested_list)
transpose Transposes lists to form maps transpose(var.list_map)

Example: Flattening Nested Lists

variable "nested_list" {
  type = list(list(string))
  default = [
    ["a", "b"],
    ["c", "d"]
  ]
}

output "flattened_list" {
  value = flatten(var.nested_list)
}

Was this article helpful?