0
0
TerraformHow-ToBeginner · 3 min read

How to Use Object Variable in Terraform: Syntax and Example

In Terraform, you use an object variable to group multiple named attributes with specific types into one variable. Define it with variable block specifying type = object({ ... }), then access its attributes using dot notation like var.my_object.attribute.
📐

Syntax

An object variable in Terraform groups related values with named keys and defined types. You declare it inside a variable block using type = object({ key = type, ... }). Each key must have a type like string, number, or bool. You assign values as a map with matching keys and types.

Access object attributes using dot notation: var.variable_name.key.

terraform
variable "example_object" {
  type = object({
    name    = string
    age     = number
    enabled = bool
  })
  description = "An example object variable"
}
💻

Example

This example shows how to define an object variable, assign values in terraform.tfvars, and use the values in a resource.

terraform
variable "user_info" {
  type = object({
    username = string
    uid      = number
    active   = bool
  })
  description = "User information object"
}

# terraform.tfvars
user_info = {
  username = "alice"
  uid      = 1001
  active   = true
}

resource "null_resource" "example" {
  triggers = {
    user_name = var.user_info.username
    user_id   = tostring(var.user_info.uid)
    status    = var.user_info.active ? "active" : "inactive"
  }
}
Output
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
⚠️

Common Pitfalls

Common mistakes when using object variables include:

  • Not matching the exact keys and types when assigning values causes errors.
  • Trying to assign a plain map without specifying object type in the variable block.
  • Accessing attributes with wrong key names or missing keys.

Always ensure the assigned value keys and types exactly match the declared object type.

terraform
variable "bad_object" {
  type = object({
    name = string
    age  = number
  })
}

# Wrong assignment (missing 'age' key)
bad_object = {
  name = "bob"
}

# Correct assignment
bad_object = {
  name = "bob"
  age  = 30
}
📊

Quick Reference

ConceptDescriptionExample
Declare object variableUse variable block with type = object({ ... })variable "obj" { type = object({ key = string }) }
Assign valuesProvide a map with matching keys and typesobj = { key = "value" }
Access attributesUse dot notation to get valuesvar.obj.key
Type mismatch errorOccurs if assigned keys or types differEnsure keys and types match exactly
Use in resourceReference attributes inside resource blockstriggers = { name = var.obj.key }

Key Takeaways

Define object variables with explicit keys and types using the object type.
Assign values as maps matching the declared keys and types exactly.
Access object attributes using dot notation like var.object_name.key.
Mismatched keys or types cause errors during Terraform plan or apply.
Use object variables to group related data cleanly and improve code clarity.