Bird
Raised Fist0
Terraformcloud~5 mins

Tuple type definition in Terraform - Commands & Configuration

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
Introduction
Sometimes you want to store a fixed list of values where each value has a specific type. A tuple lets you do this by defining a list with a set number of elements, each with its own type. This helps keep your infrastructure code clear and safe.
When you need to pass a fixed set of different types of values to a module.
When you want to define a variable that holds a list with a known number of elements and types.
When you want to ensure that a list always has the same structure, like a pair of strings or a string and a number.
When you want to avoid errors by making sure the order and type of values are correct.
When you want to document clearly what kind of data your Terraform code expects.
Config File - main.tf
main.tf
variable "example_tuple" {
  type = tuple([string, number, bool])
  description = "A tuple with a string, a number, and a boolean"
  default = ["hello", 42, true]
}

output "tuple_output" {
  value = var.example_tuple
}

This file defines a variable named example_tuple with a tuple type. The tuple expects exactly three values: a string, a number, and a boolean, in that order. The default provides example values. The output block shows how to access and display the tuple's value after applying the configuration.

Commands
This command initializes the Terraform working directory. It downloads necessary plugins and prepares Terraform to run.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/random... - Installing hashicorp/random v3.4.3... - Installed hashicorp/random v3.4.3 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command applies the Terraform configuration to create or update infrastructure. The -auto-approve flag skips the confirmation prompt.
Terminal
terraform apply -auto-approve
Expected OutputExpected
var.example_tuple A tuple with a string, a number, and a boolean Apply complete! Resources: 0 added, 0 changed, 0 destroyed. Outputs: tuple_output = ["hello", 42, true]
-auto-approve - Automatically approve the apply without asking for confirmation
This command shows the value of the output named tuple_output, which displays the tuple variable's content.
Terminal
terraform output tuple_output
Expected OutputExpected
["hello", 42, true]
Key Concept

If you remember nothing else from this pattern, remember: a tuple defines a fixed list of values where each value has a specific type and order.

Common Mistakes
Defining a tuple variable but providing a default value with the wrong number of elements.
Terraform will give an error because the tuple expects a fixed number of elements and types.
Make sure the default list has exactly the same number of elements and types as the tuple definition.
Using a tuple when a list of the same type would be simpler.
Tuples require fixed types and order, which can be unnecessarily strict for simple lists.
Use list(type) when you want a flexible number of elements of the same type.
Summary
Define a tuple variable with the exact types and order of elements you want.
Initialize Terraform with 'terraform init' before applying changes.
Apply the configuration with 'terraform apply' and check outputs with 'terraform output'.

Practice

(1/5)
1. What is a tuple type in Terraform?
easy
A. A fixed list of values where each value has a specific type
B. A list of values all having the same type
C. A map with keys and values of any type
D. A variable that can hold any type of data

Solution

  1. Step 1: Understand tuple definition

    A tuple in Terraform is a collection of values with a fixed number and specific types for each position.
  2. Step 2: Compare with other types

    Unlike lists, tuples have fixed length and types per position, not all the same type.
  3. Final Answer:

    A fixed list of values where each value has a specific type -> Option A
  4. Quick Check:

    Tuple = fixed types and order [OK]
Hint: Remember: tuple = fixed order and types [OK]
Common Mistakes:
  • Confusing tuple with list (all same type)
  • Thinking tuple can have variable length
  • Mixing tuple with map types
2. Which of the following is the correct syntax to define a tuple type with a string and a number in Terraform?
easy
A. tuple([string, number])
B. tuple[string, number]
C. list([string, number])
D. tuple(string, number)

Solution

  1. Step 1: Recall tuple syntax

    Terraform tuple types are defined as tuple([type1, type2, ...]), using square brackets inside parentheses.
  2. 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.
  3. Final Answer:

    tuple([string, number]) -> Option A
  4. Quick Check:

    Tuple syntax = tuple([type1, type2]) [OK]
Hint: Use tuple([type1, type2]) syntax with square brackets inside parentheses [OK]
Common Mistakes:
  • Using parentheses without square brackets
  • Using square brackets without parentheses
  • Confusing tuple syntax with list syntax
3. Given this variable definition in Terraform:
variable "example" {
  type = tuple([string, number, bool])
  default = ["hello", 42, true]
}
What will be the value of var.example[1]?
medium
A. "hello"
B. true
C. Error: invalid index
D. 42

Solution

  1. Step 1: Understand tuple indexing

    Tuple elements are indexed starting at 0. The second element is at index 1.
  2. Step 2: Identify value at index 1

    The tuple is ["hello", 42, true], so index 1 is 42.
  3. Final Answer:

    42 -> Option D
  4. Quick Check:

    Index 1 in tuple = 42 [OK]
Hint: Tuple index starts at 0, so second item is index 1 [OK]
Common Mistakes:
  • Confusing index 1 with index 0
  • Expecting string instead of number at index 1
  • Thinking tuple elements are unordered
4. What is wrong with this Terraform tuple type definition?
variable "bad_tuple" {
  type = tuple([string, number])
  default = ["text", "not a number"]
}
medium
A. The tuple type syntax is incorrect
B. The default value does not match the tuple types
C. Tuple cannot have string and number types together
D. Default value must be a map, not a list

Solution

  1. Step 1: Check tuple type syntax

    The syntax tuple([string, number]) is correct for a tuple with two elements.
  2. 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.
  3. Final Answer:

    The default value does not match the tuple types -> Option B
  4. Quick Check:

    Tuple types must match default values [OK]
Hint: Check default values match tuple types exactly [OK]
Common Mistakes:
  • Assuming tuple syntax is wrong
  • Ignoring type mismatch in default values
  • Thinking tuples can't mix types
5. You want to define a Terraform variable that holds a tuple with three elements: a string, a list of numbers, and a boolean. Which is the correct type definition?
hard
A. tuple(string, list[number], bool)
B. tuple(string, list, bool)
C. tuple([string, list(number), bool])
D. tuple[string, list(number), bool]

Solution

  1. Step 1: Understand tuple element types

    The tuple has three elements: a string, a list of numbers, and a boolean.
  2. 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]).
  3. Step 3: Check options

    tuple([string, list(number), bool]) matches the correct syntax. Others use invalid syntax like list[number] or brackets.
  4. Final Answer:

    tuple([string, list(number), bool]) -> Option C
  5. Quick Check:

    List type inside tuple uses list(type) [OK]
Hint: Use list(type) inside tuple for lists [OK]
Common Mistakes:
  • Using square brackets instead of parentheses for list
  • Omitting type inside list
  • Using tuple with square brackets incorrectly