0
0
Terraformcloud~5 mins

Tuple type definition in Terraform - Commands & Configuration

Choose your learning style9 modes available
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'.