Bird
Raised Fist0
Terraformcloud~20 mins

Why complex types matter in Terraform - Challenge Your Understanding

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
Challenge - 5 Problems
🎖️
Complex Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding complex types in Terraform variables

Which of the following Terraform variable declarations correctly defines a variable that expects a list of maps, where each map contains a string key and a number value?

Avariable "example" { type = list(map(number)) }
Bvariable "example" { type = map(list(number)) }
Cvariable "example" { type = list(object({ key = string, value = number })) }
Dvariable "example" { type = map(object({ key = string, value = number })) }
Attempts:
2 left
💡 Hint

Think about the structure: a list of maps means the outer container is a list, and each item is a map with string keys and number values.

Configuration
intermediate
2:00remaining
Outputting a complex type in Terraform

Given the following Terraform variable declaration, which output block correctly outputs the first map's value for the key "port"?

variable "servers" {
  type = list(map(number))
  default = [
    { port = 8080, id = 1 },
    { port = 9090, id = 2 }
  ]
}
Aoutput "first_port" { value = var.servers[0].port }
Boutput "first_port" { value = var.servers["port"][0] }
Coutput "first_port" { value = var.servers.port[0] }
Doutput "first_port" { value = var.servers[0]["port"] }
Attempts:
2 left
💡 Hint

Remember that var.servers is a list of maps, so you access the first element by index, then the map key.

Architecture
advanced
2:30remaining
Choosing complex types for modular Terraform design

You are designing a Terraform module that manages multiple cloud resources. You want to pass a variable that holds a list of resources, each with a name (string), count (number), and tags (map of strings). Which variable type best fits this requirement?

Aobject({ name = string, count = number, tags = map(string) })
Blist(object({ name = string, count = number, tags = map(string) }))
Clist(map(string))
Dmap(object({ name = string, count = number, tags = list(string) }))
Attempts:
2 left
💡 Hint

Consider the structure: multiple resources means a list, each resource has multiple attributes including a map for tags.

security
advanced
2:30remaining
Security implications of complex types in Terraform variables

Which of the following statements about using complex types in Terraform variables is true regarding security best practices?

AComplex types automatically encrypt sensitive data stored in variables without additional configuration.
BUsing simple string variables is always safer than complex types because they are easier to audit.
CTerraform variables with complex types cannot be marked as sensitive, so they should not be used for secrets.
DUsing complex types like objects allows you to enforce strict schemas, reducing the risk of misconfiguration that could expose sensitive data.
Attempts:
2 left
💡 Hint

Think about how strict typing helps prevent mistakes that could lead to security issues.

service_behavior
expert
3:00remaining
Behavior of Terraform when complex type variable input is invalid

Given a Terraform variable declared as variable "config" { type = object({ host = string, ports = list(number) }) }, what happens if you provide the following input?

{ host = "example.com", ports = ["80", 443] }
ATerraform plan fails with a type mismatch error because "80" is a string, not a number.
BTerraform plan succeeds and converts "80" string to number automatically.
CTerraform plan ignores the invalid port and uses only 443.
DTerraform plan succeeds but warns about the string in the ports list.
Attempts:
2 left
💡 Hint

Terraform enforces strict typing and does not perform automatic type conversion.

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

  1. 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.
  2. Step 2: Compare with other options

    They do not speed up Terraform or fix syntax errors automatically, nor do they replace variables.
  3. Final Answer:

    They group related data together for better organization and clarity. -> Option B
  4. 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

  1. Step 1: Recall object type syntax

    Terraform defines object types with curly braces and attribute names with their types inside parentheses.
  2. 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.
  3. Final Answer:

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

    Object type syntax = variable "person" { type = object({ name = string, age = number }) } [OK]
Hint: Remember object uses curly braces with attribute types inside [OK]
Common Mistakes:
  • Using map instead of object for fixed attributes
  • Incorrect brackets or parentheses
  • Mixing list and object syntax
3. Given this Terraform variable declaration:
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

  1. 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].
  2. Step 2: Access the second port value

    Index 1 in the list is the second element, which is 443 (a number, not a string).
  3. Final Answer:

    443 -> Option C
  4. 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

  1. Step 1: Check variable declaration and usage

    The variable "config" is declared with no default, so it must be provided during apply.
  2. 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.
  3. Final Answer:

    The variable config is missing a default value or input. -> Option A
  4. 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

  1. Step 1: Understand the required structure

    The variable is a list of objects, each with name (string), ip (string), and ports (list of numbers).
  2. 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.
  3. 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.
  4. Final Answer:

    variable "servers" { type = list(object({ name = string, ip = string, ports = list(number) })) } -> Option A
  5. 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