What if a simple mistake in your server list could break your entire cloud setup?
Why Tuple type definition in Terraform? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you need to define a list of server configurations manually, each with a fixed set of properties like IP address, port, and role. You write each entry by hand, hoping to keep the order and types consistent.
This manual approach is slow and risky. If you mix up the order or types, your infrastructure might break or behave unexpectedly. It's hard to check if each item matches the expected format, leading to errors that are tough to find.
Using a tuple type definition in Terraform lets you specify exactly what types and order each element in a list should have. This ensures your configurations are consistent and validated automatically, saving time and avoiding mistakes.
variable "servers" { type = list(any) }
variable "servers" { type = list(tuple([string, number, string])) }
It enables precise, reliable infrastructure definitions that Terraform can check before applying changes.
Defining a tuple for a list of database nodes where each node must have a hostname (string), port (number), and environment (string) ensures all nodes are configured correctly.
Manual lists can cause errors if types or order are mixed up.
Tuple type definitions enforce exact types and order in lists.
This leads to safer, clearer, and more maintainable infrastructure code.
Practice
Solution
Step 1: Understand tuple definition
A tuple in Terraform is a collection of values with a fixed number and specific types for each position.Step 2: Compare with other types
Unlike lists, tuples have fixed length and types per position, not all the same type.Final Answer:
A fixed list of values where each value has a specific type -> Option AQuick Check:
Tuple = fixed types and order [OK]
- Confusing tuple with list (all same type)
- Thinking tuple can have variable length
- Mixing tuple with map types
Solution
Step 1: Recall tuple syntax
Terraform tuple types are defined as tuple([type1, type2, ...]), using square brackets inside parentheses.Step 2: Check options
The correct syntax is tuple([string, number]). Options B and D are invalid syntax. list([string, number]) is a list, not a tuple.Final Answer:
tuple([string, number]) -> Option AQuick Check:
Tuple syntax = tuple([type1, type2]) [OK]
- Using parentheses without square brackets
- Using square brackets without parentheses
- Confusing tuple syntax with list syntax
variable "example" {
type = tuple([string, number, bool])
default = ["hello", 42, true]
}
What will be the value of var.example[1]?Solution
Step 1: Understand tuple indexing
Tuple elements are indexed starting at 0. The second element is at index 1.Step 2: Identify value at index 1
The tuple is ["hello", 42, true], so index 1 is 42.Final Answer:
42 -> Option DQuick Check:
Index 1 in tuple = 42 [OK]
- Confusing index 1 with index 0
- Expecting string instead of number at index 1
- Thinking tuple elements are unordered
variable "bad_tuple" {
type = tuple([string, number])
default = ["text", "not a number"]
}Solution
Step 1: Check tuple type syntax
The syntax tuple([string, number]) is correct for a tuple with two elements.Step 2: Validate default values
The default is ["text", "not a number"]. The second element should be a number but is a string, causing a type mismatch.Final Answer:
The default value does not match the tuple types -> Option BQuick Check:
Tuple types must match default values [OK]
- Assuming tuple syntax is wrong
- Ignoring type mismatch in default values
- Thinking tuples can't mix types
Solution
Step 1: Understand tuple element types
The tuple has three elements: a string, a list of numbers, and a boolean.Step 2: Use correct syntax for list of numbers
In Terraform, list of numbers is written as list(number). So the tuple type is tuple([string, list(number), bool]).Step 3: Check options
tuple([string, list(number), bool]) matches the correct syntax. Others use invalid syntax like list[number] or brackets.Final Answer:
tuple([string, list(number), bool]) -> Option CQuick Check:
List type inside tuple uses list(type) [OK]
- Using square brackets instead of parentheses for list
- Omitting type inside list
- Using tuple with square brackets incorrectly
