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 are complex types in Terraform?
Complex types are data structures like lists, maps, and objects that group multiple values together. They help organize and manage related data in Terraform configurations.
Click to reveal answer
beginner
Why do complex types matter in Terraform?
They allow you to model real-world infrastructure more accurately by grouping related settings. This makes configurations easier to read, reuse, and maintain.
Click to reveal answer
intermediate
How do complex types improve Terraform module design?
Using complex types lets you pass structured data between modules, reducing errors and making modules more flexible and reusable.
Click to reveal answer
beginner
Give an example of a complex type in Terraform.
A map of objects, like a map where each key is a server name and the value is an object with IP address and role, helps organize server details clearly.
Click to reveal answer
beginner
What is a benefit of using complex types over simple types?
Complex types reduce repetition and mistakes by grouping related data, making your Terraform code cleaner and easier to update.
Click to reveal answer
Which of the following is NOT a complex type in Terraform?
AString
BMap
CList
DObject
✗ Incorrect
String is a simple type, while List, Map, and Object are complex types that group multiple values.
Why use complex types in Terraform modules?
ATo make modules less reusable
BTo organize related data clearly
CTo increase code repetition
DTo avoid passing data between modules
✗ Incorrect
Complex types help organize related data clearly, improving module design and reusability.
What does a map type in Terraform represent?
AA boolean flag
BAn ordered list of values
CA single string value
DA key-value collection
✗ Incorrect
A map is a collection of key-value pairs, useful for grouping related data.
How do complex types help reduce errors?
ABy grouping related data and avoiding repetition
BBy making code longer
CBy hiding data from users
DBy removing all variables
✗ Incorrect
Grouping related data reduces mistakes and makes code easier to maintain.
Which complex type would you use to store multiple server configurations with different properties?
AString
BNumber
CList of objects
DBoolean
✗ Incorrect
A list of objects lets you store multiple servers, each with its own properties.
Explain why complex types are important in Terraform and how they improve infrastructure code.
Think about how grouping data helps manage infrastructure settings.
You got /5 concepts.
Describe a scenario where using a complex type like a map of objects would be beneficial in Terraform.
Consider managing multiple servers or network settings.
You got /4 concepts.
Practice
(1/5)
1. Why are complex types like object or map useful in Terraform?
easy
A. They automatically fix syntax errors in your code.
B. They group related data together for better organization and clarity.
C. They make Terraform run faster by using less memory.
D. They replace the need for variables entirely.
Solution
Step 1: Understand the purpose of complex types
Complex types like objects and maps group related pieces of data into one unit, making code clearer.
Step 2: Compare with other options
They do not speed up Terraform or fix syntax errors automatically, nor do they replace variables.
Final Answer:
They group related data together for better organization and clarity. -> Option B
Quick Check:
Complex types = group related data [OK]
Hint: Think: complex types bundle related info neatly [OK]
Common Mistakes:
Confusing complex types with performance improvements
Believing complex types fix syntax errors
Thinking complex types remove the need for variables
2. Which of the following is the correct syntax to define an object variable with attributes name (string) and age (number) in Terraform?
easy
A. variable "person" { type = object[name string, age number] }
B. variable "person" { type = map(string, number) }
C. variable "person" { type = list(object(name, age)) }
D. variable "person" { type = object({ name = string, age = number }) }
Solution
Step 1: Recall object type syntax
Terraform defines object types with curly braces and attribute names with their types inside parentheses.
Step 2: Check each option
variable "person" { type = object({ name = string, age = number }) } matches correct syntax: object({ name = string, age = number }). Others have wrong syntax or wrong type structures.
Final Answer:
variable "person" { type = object({ name = string, age = number }) } -> Option D
Quick Check:
Object type syntax = variable "person" { type = object({ name = string, age = number }) } [OK]
variable "server" {
type = object({
ip = string
ports = list(number)
})
default = {
ip = "10.0.0.1"
ports = [80, 443]
}
}
What is the value of var.server.ports[1]?
medium
A. "443"
B. 80
C. 443
D. Error: index out of range
Solution
Step 1: Understand the variable structure
The variable "server" is an object with an IP string and a list of numbers called ports: [80, 443].
Step 2: Access the second port value
Index 1 in the list is the second element, which is 443 (a number, not a string).
Final Answer:
443 -> Option C
Quick Check:
var.server.ports[1] = 443 [OK]
Hint: List index starts at 0, so 1 is second item [OK]
Common Mistakes:
Confusing index 1 with index 0
Thinking numbers are strings
Assuming index out of range error
4. This Terraform code snippet causes an error:
variable "config" {
type = object({
region = string
tags = map(string)
})
}
output "region" {
value = var.config.region
}
output "tag_value" {
value = var.config.tags["env"]
}
What is the most likely cause of the error?
medium
A. The variable config is missing a default value or input.
B. The tags attribute should be a list, not a map.
C. Output blocks cannot access variable attributes directly.
D. The syntax for accessing map values is incorrect.
Solution
Step 1: Check variable declaration and usage
The variable "config" is declared with no default, so it must be provided during apply.
Step 2: Identify error cause
If no input is given, accessing var.config.region or var.config.tags["env"] causes an error because the variable is undefined.
Final Answer:
The variable config is missing a default value or input. -> Option A
Quick Check:
Missing input for variable = error [OK]
Hint: Variables without defaults need input to avoid errors [OK]
Common Mistakes:
Assuming map access syntax is wrong
Thinking outputs can't access variables
Confusing map and list types
5. You want to pass a list of objects representing servers to a module. Each server has name (string), ip (string), and ports (list of numbers). Which variable type declaration correctly enforces this structure?
hard
A. variable "servers" { type = list(object({ name = string, ip = string, ports = list(number) })) }
B. variable "servers" { type = map(object({ name = string, ip = string, ports = list(string) })) }
C. variable "servers" { type = object({ name = list(string), ip = list(string), ports = list(number) }) }
D. variable "servers" { type = list(map(string)) }
Solution
Step 1: Understand the required structure
The variable is a list of objects, each with name (string), ip (string), and ports (list of numbers).
Step 2: Match the correct type declaration
variable "servers" { type = list(object({ name = string, ip = string, ports = list(number) })) } correctly declares a list of objects with the specified attributes and types.
Step 3: Eliminate incorrect options
variable "servers" { type = map(object({ name = string, ip = string, ports = list(string) })) } uses map instead of list and ports as list of strings (wrong type). variable "servers" { type = object({ name = list(string), ip = list(string), ports = list(number) }) } is a single object with lists, not a list of objects. variable "servers" { type = list(map(string)) } is a list of maps with string values only, missing nested list of numbers.
Final Answer:
variable "servers" { type = list(object({ name = string, ip = string, ports = list(number) })) } -> Option A
Quick Check:
List of objects with correct attributes = variable "servers" { type = list(object({ name = string, ip = string, ports = list(number) })) } [OK]
Hint: List of objects means list(object({...})) [OK]
Common Mistakes:
Confusing map and list types
Using wrong attribute types inside objects
Declaring a single object instead of list of objects