Cheatsheets:
programming:
How-to:
Troubleshooting:
Rants:
Other:
Cheatsheets:
programming:
How-to:
Troubleshooting:
Rants:
Other:
In Terraform, a tuple is a sequence of values, identified by their order. Tuples are similar to lists, but they are intended to represent a fixed collection of elements where each element can have a different type.
Tuples can be defined using square brackets [] with elements separated by commas. Each element in the tuple can be of a different type.
variable "example_tuple" {
description = "An example of a tuple"
type = tuple([string, number, bool])
default = ["hello", 42, true]
}
Tuples can be used to pass multiple values of different types within a single variable. You can access elements of a tuple using zero-based indexing.
variable "example_tuple" {
description = "An example of a tuple"
type = tuple([string, number, bool])
default = ["hello", 42, true]
}
output "first_element" {
value = var.example_tuple[0] # "hello"
}
output "second_element" {
value = var.example_tuple[1] # 42
}
output "third_element" {
value = var.example_tuple[2] # true
}
When using tuples in modules, you can pass a tuple variable from a root module to a child module.
module "example" {
source = "./child_module"
example_tuple = ["hello", 42, true]
}
variable "example_tuple" {
description = "An example of a tuple"
type = tuple([string, number, bool])
}
output "first_element" {
value = var.example_tuple[0]
}
output "second_element" {
value = var.example_tuple[1]
}
output "third_element" {
value = var.example_tuple[2]
}
Tuples can also be nested within other tuples or complex types like objects. This allows for more complex data structures.
variable "nested_tuple" {
description = "A nested tuple"
type = tuple([string, tuple([number, bool])])
default = ["outer", [42, true]]
}
output "outer_element" {
value = var.nested_tuple[0] # "outer"
}
output "inner_number" {
value = var.nested_tuple[1][0] # 42
}
output "inner_bool" {
value = var.nested_tuple[1][1] # true
}
variable "simple_tuple" {
description = "A simple tuple"
type = tuple([string, number, bool])
default = ["example", 10, false]
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = var.simple_tuple[0]
}
}
output "tuple_string" {
value = var.simple_tuple[0] # "example"
}
output "tuple_number" {
value = var.simple_tuple[1] # 10
}
output "tuple_bool" {
value = var.simple_tuple[2] # false
}
variable "complex_tuple" {
description = "A complex tuple"
type = tuple([
string,
number,
bool,
list(string),
map(string)
])
default = ["example", 10, false, ["item1", "item2"], {key1 = "value1", key2 = "value2"}]
}
output "tuple_string" {
value = var.complex_tuple[0] # "example"
}
output "tuple_number" {
value = var.complex_tuple[1] # 10
}
output "tuple_bool" {
value = var.complex_tuple[2] # false
}
output "tuple_list" {
value = var.complex_tuple[3] # ["item1", "item2"]
}
output "tuple_map" {
value = var.complex_tuple[4] # {key1 = "value1", key2 = "value2"}
}