Terraform variables are just one way to parameterize your Terraform files so that they’re reusable and flexible. They allow you to define values that you can input into your Terraform code so you don’t have to hard-code everything. You can imagine them as inputs or placeholders that you can change when running your Terraform plans.
Terraform variables types
Terraform has several types of variables, each designed to handle different kinds of data. Below is the full list of Terraform variable types:
- String: This is the default type that you use to store text values. Use strings for data that shouldn’t be computationally manipulated, such as usernames or tags.
- Number: This type stores numeric values you might want to compute on or deal with in numeric terms, such as scaling factors, timeouts, and saying how many instances to deploy.
- Bool: Also known as Boolean, this is utilized exclusively for true or false values. They play a crucial role in the domain of logic and conditionals utilized in configurations such as resource provisioning to turn it on or off.
- List: A list comprises values of similar type listed in sequence. This is appropriate where you are working with a set of similar objects such as an array of several configuration tags.
- Map: Maps are collections of key-value pairs, with each unique key mapping to a specific value. This form is handy, e.g., for mapping server names to their roles or configurations.
- Tuple:It is similar to lists but can have a constant number of elements, each of which may be a different type. Tuples are suitable if you need to encapsulate some specific collection of different type values together, like a coordinate of heterogenuous data types.
- Object: Objects are used to define a structure with typed attributes, with their own name of the same type. They are very flexible in that they can define complex relationships, e.g., a configuration block with various types of attributes.
- Set: Sets consist of unique values of the same type. They’re useful if you don’t want any duplicate, such as a set of distinct user IDs or choices which should remain distinct from one another.