Bird
Raised Fist0
Terraformcloud~10 mins

Object type definition in Terraform - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Object type definition
Start: Define object type
Specify attribute names and types
Use object type in variable or output
Assign values matching the object structure
Terraform validates types
Apply configuration or error
End
Define an object type by listing attribute names and their types, then use it to enforce structured data in variables or outputs.
Execution Sample
Terraform
variable "server" {
  type = object({
    name = string
    ip   = string
    port = number
  })
  default = {
    name = "web01"
    ip   = "10.0.0.1"
    port = 80
  }
}
Defines a variable 'server' with an object type having name, ip, and port attributes, then assigns default values.
Process Table
StepActionAttribute CheckedExpected TypeValue ProvidedValidation Result
1Start variable definition---Waiting for attributes
2Define attribute 'name'namestring-Defined
3Define attribute 'ip'ipstring-Defined
4Define attribute 'port'portnumber-Defined
5Assign default value for 'name'namestring"web01"Valid
6Assign default value for 'ip'ipstring"10.0.0.1"Valid
7Assign default value for 'port'portnumber80Valid
8Terraform validates entire objectallobject{name="web01", ip="10.0.0.1", port=80}Valid
9Variable ready for use---Success
💡 All attributes match their types; object variable definition is valid and ready.
Status Tracker
VariableStartAfter Step 5After Step 6After Step 7Final
server.nameundefined"web01""web01""web01""web01"
server.ipundefinedundefined"10.0.0.1""10.0.0.1""10.0.0.1"
server.portundefinedundefinedundefined8080
Key Moments - 3 Insights
Why must each attribute in the object have a specified type?
Terraform checks each attribute's value against its declared type during validation (see execution_table steps 5-8). This prevents errors by ensuring data matches expected formats.
What happens if a value does not match the declared type?
Terraform will throw an error during validation (step 8), stopping the apply process until the mismatch is fixed.
Can the object have extra attributes not defined in the type?
No, Terraform only accepts attributes defined in the object type. Extra attributes cause validation errors.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the validation result when the 'port' attribute is assigned the value 80?
AMissing attribute error
BValid
CInvalid type error
DIgnored
💡 Hint
Check step 7 in the execution_table where 'port' is assigned 80.
At which step does Terraform validate the entire object variable?
AStep 5
BStep 7
CStep 8
DStep 9
💡 Hint
Look for the step mentioning 'Terraform validates entire object' in the execution_table.
If the 'ip' attribute value was changed to a number, how would the validation result change at step 6?
AInvalid type error
BStill Valid
CMissing attribute error
DIgnored
💡 Hint
Refer to step 6 where 'ip' expects a string type.
Concept Snapshot
Object type definition in Terraform:
- Use object({ attr1=type1, attr2=type2, ... })
- Enforces structured data with named attributes
- Each attribute must have a declared type
- Values must match types exactly
- Terraform validates on apply
- Prevents configuration errors by type checking
Full Transcript
This visual execution shows how Terraform defines an object type variable. First, the object type is declared with attribute names and their types. Then, default values are assigned to each attribute. Terraform validates each value against its declared type step-by-step. If all values match, the object variable is ready for use. If any value mismatches, Terraform throws an error and stops. This ensures structured and type-safe configuration.

Practice

(1/5)
1. What is the main purpose of defining an object type in Terraform?
easy
A. To group related values with specific names and types
B. To create a list of strings
C. To define a single integer value
D. To write shell scripts inside Terraform

Solution

  1. Step 1: Understand object type concept

    An object type groups multiple related values, each with a name and a type.
  2. Step 2: Compare with other types

    Unlike lists or single values, objects organize structured data clearly.
  3. Final Answer:

    To group related values with specific names and types -> Option A
  4. Quick Check:

    Object type = group related named values [OK]
Hint: Objects group named values, not single or list values [OK]
Common Mistakes:
  • Confusing object with list or map types
  • Thinking object holds only one value
  • Assuming object is for scripting
2. Which of the following is the correct syntax to define an object type with a string field name and a number field age in Terraform?
easy
A. object({ name string, age number })
B. object({ name: string, age: number })
C. object({ "name" = string, "age" = number })
D. object({ name = string, age = number })

Solution

  1. Step 1: Recall object type syntax

    Terraform object types use curly braces with unquoted identifier keys and equal signs: object({ key = type, ... })
  2. Step 2: Check each option

    object({ name = string, age = number }) correctly uses unquoted keys and equal signs. Others use invalid syntax like colons, quoted keys for simple identifiers, or missing equals.
  3. Final Answer:

    object({ name = string, age = number }) -> Option D
  4. Quick Check:

    Object keys unquoted with = sign [OK]
Hint: Object keys use unquoted identifiers with = type [OK]
Common Mistakes:
  • Using colons instead of equal signs
  • Quoting keys in object type
  • Omitting commas or using wrong separators
3. Given this variable definition in Terraform:
variable "person" {
  type = object({
    name = string
    age  = number
  })
  default = {
    name = "Alice"
    age  = 30
  }
}
What will be the value of var.person.age?
medium
A. "30"
B. 30
C. null
D. Error: type mismatch

Solution

  1. Step 1: Analyze variable type and default

    The variable is an object with fields name (string) and age (number). Default sets age to 30 (number).
  2. Step 2: Determine var.person.age value

    Accessing var.person.age returns the number 30 as defined.
  3. Final Answer:

    30 -> Option B
  4. Quick Check:

    Object field age = 30 number [OK]
Hint: Object fields keep their defined types and values [OK]
Common Mistakes:
  • Thinking number becomes string automatically
  • Expecting null if not accessed
  • Confusing default with no value
4. Identify the error in this Terraform object type definition:
variable "config" {
  type = object({
    "enabled" = bool
    "count" = int
  })
  default = {
    enabled = true
    count = 3
  }
}
medium
A. The type int is invalid; should be number
B. Keys in object type should not be quoted
C. Default values must be strings
D. Boolean values cannot be used in objects

Solution

  1. Step 1: Check type names in object

    Terraform uses number for numeric types, not int.
  2. Step 2: Validate other syntax

    Quoted keys are allowed, default values match types, booleans are valid.
  3. Final Answer:

    The type int is invalid; should be number -> Option A
  4. Quick Check:

    Use number, not int, for numeric types [OK]
Hint: Use 'number' type, not 'int' in Terraform objects [OK]
Common Mistakes:
  • Using 'int' instead of 'number'
  • Thinking keys cannot be quoted
  • Believing booleans are invalid in objects
5. You want to define a Terraform variable that accepts an object with a name (string), tags (map of strings), and an optional count (number). Which is the correct way to define this object type?
hard
A. object({ name = string, tags = map(string), count = number })
B. object({ "name" = string, "tags" = map(string), "count" = optional(number) })
C. object({ name = string, tags = map(string), count = optional(number) })
D. object({ name = string, tags = map(string), count = optional number })

Solution

  1. Step 1: Recall optional attribute syntax

    Terraform uses optional(type) without quotes for optional fields inside object types.
  2. Step 2: Check key quoting rules

    Keys in object type definitions use unquoted identifiers for standard names; quoting simple keys is incorrect.
  3. Step 3: Evaluate options

    object({ name = string, tags = map(string), count = optional(number) }) correctly uses unquoted keys and optional(number) syntax. object({ "name" = string, "tags" = map(string), "count" = optional(number) }) quotes keys incorrectly. object({ name = string, tags = map(string), count = number }) misses optional. object({ name = string, tags = map(string), count = optional number }) has invalid syntax.
  4. Final Answer:

    object({ name = string, tags = map(string), count = optional(number) }) -> Option C
  5. Quick Check:

    Optional fields use optional(type) with unquoted keys [OK]
Hint: Use optional(type) without quotes and unquoted keys [OK]
Common Mistakes:
  • Quoting keys in object type
  • Missing optional() for optional fields
  • Using invalid syntax like 'optional number'