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
Recall & Review
beginner
What is an object type in Terraform?
An object type in Terraform is a way to group multiple named values with specific types into a single complex value, like a small container with labeled slots.
Click to reveal answer
beginner
How do you define an object type with two attributes: name as string and age as number?
You define it like this: object({ name = string, age = number })
Click to reveal answer
intermediate
Why use object types instead of maps in Terraform?
Object types have fixed attribute names and types, which helps catch errors early and makes your code clearer, unlike maps which allow any keys and values.
Click to reveal answer
intermediate
Can object types in Terraform have nested objects?
Yes, object types can contain other object types as attributes, allowing you to build complex structured data.
Click to reveal answer
beginner
What happens if you try to assign a wrong type to an object attribute in Terraform?
Terraform will show an error during plan or apply, telling you the type does not match, helping you fix mistakes before deployment.
Click to reveal answer
Which Terraform type defines a fixed set of named attributes with specific types?
Aobject
Blist
Cmap
Dstring
✗ Incorrect
The object type defines a fixed set of named attributes with specific types.
How do you define an object type with attributes 'id' as number and 'enabled' as bool?
Amap(string)
Blist(number, bool)
Ctuple(number, bool)
Dobject({ id = number, enabled = bool })
✗ Incorrect
The correct syntax for an object type with named attributes is object({ id = number, enabled = bool }).
What is a benefit of using object types over maps in Terraform?
Can an object type attribute itself be another object type?
AOnly if declared as a map
BNo
CYes
DOnly in Terraform 0.11 or earlier
✗ Incorrect
Object types can be nested by having attributes that are also object types.
What happens if you assign a string to an object attribute defined as number?
ATerraform throws a type error
BTerraform converts it automatically
CTerraform ignores the attribute
DTerraform treats it as null
✗ Incorrect
Terraform throws a type error to prevent invalid assignments.
Explain what an object type is in Terraform and why it is useful.
Think of an object type as a labeled box with specific slots for values.
You got /4 concepts.
Describe how you would define a nested object type in Terraform with an example.
Use object({ ... }) inside another object({ ... })
You got /3 concepts.
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
Step 1: Understand object type concept
An object type groups multiple related values, each with a name and a type.
Step 2: Compare with other types
Unlike lists or single values, objects organize structured data clearly.
Final Answer:
To group related values with specific names and types -> Option A
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
Step 1: Recall object type syntax
Terraform object types use curly braces with unquoted identifier keys and equal signs: object({ key = type, ... })
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.
Final Answer:
object({ name = string, age = number }) -> Option D
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
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).
Step 2: Determine var.person.age value
Accessing var.person.age returns the number 30 as defined.
Final Answer:
30 -> Option B
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:
Quoted keys are allowed, default values match types, booleans are valid.
Final Answer:
The type int is invalid; should be number -> Option A
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 })