Type Constraints - Tuple
We will learn how to use Tuple type constraints in your Terraform project.
We'll cover the following
Tuple#
A tuple is a strongly typed collection of one or more values. So, for example, we could define a tuple of three values: string
, number
, number
, or two values: string
, string
. Once a tuple is defined, it always has to contain the number of values defined in that tuple. The values also have to be of the correct type and in the correct order based upon the type.
Project example#
Consider the following code:
/
- main.tf
In the above Terraform code, we define a tuple variable my_tup
with three values:
number
string
bool
The syntax for defining a tuple uses the form tuple([TYPE, TYPE...])
. As stated above, when we define a tuple with three values, we have to set it to a value with three values. Hence
why we initialise it to 4, "hello", false
.
If you run this example, you will see that the output looks exactly like a list. You can think of a tuple as a defined list that will always be of a set length and where each element will always have a certain type.
Try adding another value to the default for my_tup
, (e.g., set it to [4, "hello", false, "hey"]
). If you try doing a terraform apply
on this, you will see that Terraform gives you an error. This is because the value you are giving the tuple no longer matches the definition.
📝Note: Once you are done with the project, do not forget to run
terraform destroy
and then confirm with yes to delete all of the resources in this project.