Are you curious to learn what are Terraform variable types? Don’t worry! You are at the right stop. This blog deeply discusses Terraform variable types, output values, and local values used in Terraform configuration. By the end of reading this blog, You will be familiar with the Terraform variable types with the help of suitable examples. You will also get to know the benefits of using Terraform variable types and many more.
Terraform is nothing but an infrastructure as a code tool that simplifies creating, managing, and configuring IT resources. Terraform uses HCL or Hashicorp configuration language to write configuration files. The language uses variables like any other programming language to store data. When it comes to Terraform variables, there are many types of variables. The variables help to build dynamic codes with extreme flexibility.
Know that a Terraform variable is a container that can hold different values at different times. You can use Terraform variables to pass values in Terraform configuration files. It allows you to store different values, such as usernames, passwords, and IP addresses, in the variables.
It is essential to learn in-depth about Terraform variables and their types, which will help you to use the variables in codes effectively. This blog explores what Terraform variable types are and how to use them with examples in greater detail.
Let’s dive in!
Table of contents
Before diving deep into Terraform variable types, we will examine what Terraform variable block is. We use the Terraform variable block to declare a variable. The below image is a simple Terraform block. Here, the label (db_host) that follows the variable keyword is the variable name.
variable "db_host" {}
In the below example, user_id is the variable name. The variable type is the string type.
variable "user_id" {
type = string
}
If you want to enrich your career and become a professional in Terraform, then enroll in "Terraform Online Training" - This course will help you to achieve excellence in this domain. |
Know that Terraform uses some blocks to request and return values. The blocks are Terraform input variables, output values, and local values. When it comes to input Terraform variables, it comes with many types. We use the Terraform variable types to pass values between different infrastructure configurations.
Let’s learn more about the Terraform variable types, output values, and local values in a detailed way in this section. Terraform variables are used to pass values for Terraform commands like Terraform apply or Terraform plan.
We can divide Terraform input variables into two subcategories: primitive and complex variable types.
Terraform supports primitive variable types such as string, bool, and number.
Let’s dive deep into the primitive variable types one by one.
String: A string is nothing but a sequence of characters that forms a text. Strings are usually represented by double quotes. You can store any text-based value in this Terraform variable type.
For example, you can use the string variable type to define a region. The example below will brief you neatly.
variable "region" {
type = string
default = "UK- West"
}
Bool: You can use this variable type to store Boolean values like true or false. This Terraform variable type is used in conditional logic. This variable is usually represented without quotes.
For example, you can set the bool variable to true state to enable the logging function. If you want to disable the logging function, you must change the bool variable to a false state.
variable "enable_logging" {
type = bool
default = true
Number: You can store integer or floating point numbers in this Terraform variable type. Numbers are typically represented without any quote.
For example, the below example shows the use of a number variable (instance_count) to indicate the number of instances deployed in a machine. You can see five instances are deployed in the machine as per this example.
variable
"instance_count" {
type = number
default = 5
}
Terraform complex variables are also known as collection types or structural types. List, set, object, map, and tuple are the complex Terraform variable types.
Let’s go through the Terraform complex variable types in detail in the following.
List: You can use this Terraform variable type to pass many elements into Terraform configuration sequentially. The elements of a list are usually enclosed by square brackets. Commas separate the elements. You can split the list elements into multiple lines if you want to increase the readability. But the condition is that commas should separate them.
The below example shows the use of list Terraform variable type for different instance types.
variable "instance_types" {
type = list
default = ["t1.micro", "m4.large", "c6.xlarge"]
}
Object: You can use object variables to hold structured data and named attributes. In a way, the object variable type is similar to the map variable type, but the object variable has fixed types for named attributes. In the below example, 'user' is the object variable. This variable has many named attributes like name, email ID, and age.
variable "user" {
type = object({
name = string
email = string
age = number
})
default = {
name "james"
email = "james@example.com"
age = 25
}
}
Set: You can use this Terraform variable type to store an unordered set of values. The main thing is that these values are unique. In other words, there is no repetition of values.
The below example shows the use of a set variable type. Here, the name of the set variable is security_groups. This variable is defined as a set of strings.
variable "security_groups" {
type = set(string)
default = ["sg-2468", "sg-pqrs"]
}
Map: You can use the map Terraform variable type to hold key-value pairs. Maps use curly braces to enclose key-value pairs. You can separate the key-value pairs in maps using line breaks or commas. This variable allows you to add a list of named values in Terraform configuration files. Each value will have a unique key for it. Note that you can use arbitrary expressions as values in maps. In the below example, the tag is a map variable type that contains strings. It has two key-value pairs by default.
variable "tags" {
type = map (string)
default = {
instance = "m4.large"
Environment = "development"
}
}
Tuple: You can use the tuple variable type to hold an ordered collection of elements. Though lists and tuples seem to be the same, they have a key difference. Lists are mutable, or in other words, we change the elements of a list. On the other hand, tuples are immutable, so you cannot change the elements of a tuple. In the below example, ‘network addresses’ is the tuple variable where you can store two strings.
variable "network_addresses" {
type = tuple ([string, string])
default = ["192.168.1.3", "192.168.1.4"]
}
In Terraform, we use output values for exposing information to other Terraform configuration files. These values are typically defined in the output block of the Terraform configuration.
We use Terraform output values for multiple purposes. Here are they:
In Terraform, we use local values for assigning short names for expressions. So, you can use local values multiple times in a module instead of expressions. A local value in Terraform is nothing but a function’s temporary local variable.
We can use terraform environment variables to set various configurations at the system level. It changes the behavior of Terraform, like storing sensitive information. For example, TF_LOG is an environment variable that controls the logging level, and TF_WORKSPACE sets the terraform workspace.
TF_VAR_name
You can define terraform environment variable using the below format:
Here, ‘name’ represents the environment variable.
set TF_VAR_region-us-west-2
When it comes to the Windows operating system, you can use the below command to define environment variables.
Terraform variables provide users with the following key benefits.
We can avoid using hardcoding in IaC configuration by using Terraform variables. For example, you don’t need to provide values for instance types and region names directly in codes. You can give the values when they are required.
You can use the same IaC configuration for multiple environments. The environment can be a production, development, or testing environment. Moreover, you can use the same configuration for different settings by changing the values assigned to Terraform variables.
You can minimize the risk of hardcoding sensitive data such as passwords and API Keys. You can assign Terraform variables for sensitive data in IaC configurations. It lets you dynamically provide the data into the code during the execution.
Terraform variables allow you to create resources dynamically based on user inputs. For example, you can use the variables to specify a number of virtual machines. Not only that, you can use the variables to specify the size of a database server.
Terraform variables help to maintain organized configuration files. It helps to avoid the repetition of codes.
1. What is Terraform?
It is essentially an infrastructure as code (IaC) tool with which you can create and modify IT infrastructure efficiently. No matter what platform - you can use this tool to manage infrastructure in any platform. The platform can be on-premises or cloud platform. Terraform lets you use a consistent workflow to manage infrastructure throughout its life cycle.
2. What language is used in Terraform?
HCL or HashiCorp Configuration language is used in Terraform. The terraform configuration files are typically written in HCL language.
3. Why should I use Terraform?
4. What types of variables are supported by Terraform?
Terraform supports string, bool, number, list, object, set, map, and tuple variable types.
5. Is Terraform a DevOps tool?
Yes, terraform is an open-source DevOps tool created by Hashicorp. This tool is used to deploy complex IT infrastructure.
In a nutshell, terraform variable types are classified into two types: primitive variable types and complex variable types. String, number, and bool are the subset of primitive variable types. List, object, map, set, and tuple are the subset of complex variable types. More importantly, terraform variable types provide better reusability and readability for codes.
Well! We hope that this blog might have given deep insights into Terraform variable types. If you want to explore more about terraform, you can sign up for a terraform course in MindMajix. You will get certification after the completion of the training, which will help you to advance your career.
Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:
Name | Dates | |
---|---|---|
Terraform Training | Nov 26 to Dec 11 | View Details |
Terraform Training | Nov 30 to Dec 15 | View Details |
Terraform Training | Dec 03 to Dec 18 | View Details |
Terraform Training | Dec 07 to Dec 22 | View Details |
Madhuri is a Senior Content Creator at MindMajix. She has written about a range of different topics on various technologies, which include, Splunk, Tensorflow, Selenium, and CEH. She spends most of her time researching on technology, and startups. Connect with her via LinkedIn and Twitter .