القائمة الرئيسية

الصفحات

Complex types in Terraform

What are the complex types that we have in Terraform? 


- the complex types that we have in Terraform are * list * set * map * tuple and object


the type list


- The first complex type we'll discuss is the List type. It's an ordered collection of values. Think of it like an array in other programming languages


- Here's how you define a list:


- the elements of a list can be accessed later by their index. the index of the fist element is 0 


- if we set the type of the list elements to any, the list can contains a mix of values of different types such as 1 which is a number, "dev" which is a string and true which is of type bool


- but we set the type to a list of numbers, here only numeric values are allowed and any value of any type other than number will generate an error


Complex types in Terraform - The list type



- you see here a few examples of lists, so the first list is a list of number the second one is a list of string and the last one is a list of any that why values of different types are accepted


Complex types in Terraform - Examples of lists in Terraform



the type: set


- let's talk now about the set type


- a set is a collection of values that does not allow duplicate elements. This can be handy, when you want to ensure uniqueness within a group of elements.


- We tell terraform that the variable is of type set using the set constructor and between the parentheses, we mention the type of values that are allowed in this set 


- you have to know that unlike lists, you can not access the elements of a set using their indexes. you can access the elements of a set, by converting the set to a list using the tolist() function, as we did in this example, and then access the elements of the list by their indexes


- a set of type any can have values of different types, but if you specify the type of the elements, only values that belong to this type will be accepted


Complex types in Terraform - The set type



- let's see more examples of sets


- the first set that we have here ,is a set of numbers, so it must have only numeric values. but because we have the value "dev" which is of type string, this set will lead to an error when running the terraform plan or apply command


- the second one is a set of strings which contains 3 values: "app1", "app2" and "app3" 


- and finally for the last set. it's of type any: so it can have any value of any type


Complex types in Terraform - Examples of sets in Terraform

The type: map


- we will move now to the map type.


- a map is simply a collection of key-value pairs


- if the type of the elements of a map is any as we have here, any value given to a key will be accepted regardless of its type.


- but if you set the type to number for example, only values of type number are allowed


- after declaring a map, the values can be accessed later using the keys associated with these values


- for example here, we set the filename argument of the local_file resource, using the value given to the key called filename, of the map called file_data, and the the same for the content argument


- If we run the terraform command now, a file called count.txt will be created with the value 11 inside it


Complex types in Terraform - The map type



- let's see now how to work with a map of a specific type


- the map called numeric_data must have only numeric values, not because it's called numeric data, the name can be anything, but because it's a map of numbers, so the value associated with any key, must be of type number


- for the second map here, it's called file_data, and only values of type string are accepted 


- and finally for last map, it's a map of numbers and an error will occurs when running the Terraform plan or apply command, because the value associated with the content key, is not of type number, it's of type string


Complex types in Terraform - Examples of maps in Terraform



the type: tuple


- we will see now how to work with tuples


- a tuple is an ordered collection of elements, similar to a list,  


- here is an example of a tuple declared in the Terraform 


- Values that match the tuple type, must have a number of elements equal to the number of types mentioned between the square brackets when declaring the tuple


- for the tuple that we have declared in the variables.tf file, it will accept only 2 elements. if you set it to more or less elements than what we have mentioned between the square brackets, you will get an error


- and in addition to that, the value in each position in the tuple, must match the specified type for that position. 


- for example if we set the second element here to "three" which is a string, we will get an error, because we have mentioned when we have declared the tuple, that the second element must be of type number


Complex types in Terraform - The tuple type



-let's see now, how to access the elements of a tuple


- the elements of a tuple, can be accessed by their indexes, starting from 0 for the first element, as we did for accessing the elements of a list


- here in this example, we have used the first item of the tuple for setting the filename argument and the second element for setting the content argument of the local file


Complex types in Terraform - Accessing the elements of a tuple



The type: object


- Finally, we will talk about the object type


- an object is a collection of named attributes, similar to a map, but when working with objects, each attribute will have it own specific type


- For example here the variable called numbers is of type object and it has two attributes, one called maximum of type number, and another called minimum, also of type number


- Values that match the object type, must contain all of the specified keys, and the value for each key, must match its specified type


- a value missing any key mentioned in the object declaration, as we have here in the second example, will not be accepted and will lead to an error


- the same if the value given to the key doesn't match its type. here the minimum key is of type number, but we have set to three which is a string, so here terraform will throw an error indicating that a number is required for this key


Complew types in Terraform - The object type



finally, we will see how to access the values stored in a variable of type object.


- To access the values stored in a variable of type object, we need first of all to access the variable as we did with any variable, followed by the attribute that belongs to the object declaration


- In the example that we have here, we have used the filename attribute of the file_settings object for setting the filename of the local_file resource and the content attribute for setting the content 


- By running the terraform apply command, a file called count.txt will be created with the value 5 inside it


Complew types in Terraform - Accessing the elements of an object


You can learn more about complex types in Terraform from this video



Comments