Map and object types

5 minutes 5 Questions

Map and object types are essential collection types in Terraform that allow you to define structured data within your configurations. **Map Type:** A map is a collection of key-value pairs where all values must be of the same type. Maps are useful when you need to look up values based on string keys. You declare a map using the syntax `map(type)` where type specifies the value type. Example: hcl variable "instance_tags" { type = map(string) default = { Name = "web-server" Environment = "production" } } You can access map values using bracket notation: `var.instance_tags["Name"]` **Object Type:** An object is a structural type that allows you to define a collection with named attributes, where each attribute can have a different type. Objects provide more precise type constraints compared to maps. Example: hcl variable "server_config" { type = object({ name = string cpu_count = number is_enabled = bool tags = map(string) }) } **Key Differences:** 1. Maps require all values to share the same type, while objects allow different types for each attribute 2. Objects have a fixed schema with predefined attribute names, whereas maps accept any string key 3. Objects provide stricter validation at plan time **When to Use Each:** - Use maps when you have dynamic keys with uniform value types, such as resource tags or environment-specific settings - Use objects when you need structured data with known attributes of varying types, like configuration blocks Both types support optional attributes using the `optional()` modifier in Terraform 1.3+, allowing for flexible default values within your variable definitions.

Map and Object Types in Terraform

Why Map and Object Types Are Important

Map and object types are fundamental data structures in Terraform that allow you to organize and manage complex configuration data efficiently. Understanding these types is essential for writing maintainable, scalable infrastructure code and is a key topic tested on the Terraform Associate exam.

What Are Map and Object Types?

Map Type:
A map is a collection of key-value pairs where all values must be of the same type. Maps are useful for looking up values based on a string key.

Example:
variable "instance_sizes" {
  type = map(string)
  default = {
    small = "t2.micro"    medium = "t2.small"    large = "t2.large"  }}


Object Type:
An object is a structural type that groups multiple attributes together, where each attribute can have a different type. Objects are useful when you need to represent complex data with named attributes of varying types.

Example:
variable "server_config" {
  type = object({
    name = string
    instance_type = string
    port = number
    enabled = bool
  })
}


How They Work

Accessing Map Values:
Use square bracket notation or the lookup function:
var.instance_sizes["small"]
lookup(var.instance_sizes, "small", "t2.micro")

Accessing Object Attributes:
Use dot notation:
var.server_config.name
var.server_config.port

Key Differences Between Maps and Objects

• Maps: All values must share the same type; keys are arbitrary strings
• Objects: Each attribute can have a different type; attribute names are predefined in the schema

Common Functions for Maps

• keys(map) - Returns a list of keys
• values(map) - Returns a list of values
• lookup(map, key, default) - Retrieves a value with a fallback default
• merge(map1, map2) - Combines multiple maps

Exam Tips: Answering Questions on Map and Object Types

1. Know the syntax differences: Maps use map(type) while objects use object({attr = type})

2. Understand type constraints: Map values must be homogeneous (same type), but object attributes can be heterogeneous (different types)

3. Remember access patterns: Maps use bracket notation ["key"], objects use dot notation .attribute

4. Pay attention to default values: Both types can have default values defined in variable blocks

5. Know the lookup function: Expect questions about retrieving map values safely with defaults

6. Watch for optional attributes: In Terraform 1.3+, objects can have optional attributes using optional()

7. Recognize when to use each: Maps are ideal for dynamic key-value lookups; objects are better for structured data with known attributes

8. Review for-each with maps: Maps are commonly used with for_each meta-arguments to create multiple resources

Practice identifying whether a given scenario calls for a map or object type, as this distinction frequently appears in exam questions.
Test mode:
More Map and object types questions
30 questions (total)