DEV Community

Cover image for Variables in Terraform: My learnings.
Rohan Nalawade
Rohan Nalawade

Posted on

Variables in Terraform: My learnings.

Introduction:
I have been learning terraform and today i came across concept of variables in terraform. Variables allow you to store values which can be used in multiple places in terraform code. There are two types of variables:

  • Input Variables
  • Output Variables

1.Input Variables
Input variables allows you to store hardcoded values and use them in your terraform code as inputs. For example you can store instance types, region, instance count, etc. The syntax for input variables is like this:
variable "" {
default =
type =
description =
}

2. Output Variables
Output variables are used to store the output values which are generated after the terraform apply command. You can store values like public ip of ec2 instance, public dns, id of the instance, etc. The syntax for output variables is like this:
output <"variable name"> {
value =
description =
}

Data types in terraform
Terraform has two types of Data types. Primitive and complex data types.

The Primitive data types includes:

  • string
  • number
  • bool

The Complex data types includes:

  • list
  • map
  • set
  • object
  • tuple

The Primitive Data types:
1. string
string stores any text based values. For example, the input variable named region can store value as "us-east-1" or "ap-south-1".

2. number
number is used to store any integer based values. For example the input variable instance_count can have values like 2 or 3.

3. bool
bool is used to store boolean values. It can include only two type of values which are true or false. For example the variable named is_running can have value like true, which means the instance state is running or it can have value like false, which means the instance state is not running or stopped.

The Complex Data types:
1. List
The list is used to store multiple values of same data type. For example we can store multiple region values in a single list. ["ap-south-1", "us-east-1", "us-west-1"]. The list must contain values of same data type only. The list is mutable, meaning it can be modified after it is created.
The list is always ordered in nature.

2. set
The set is also similar to list but a set cannot have duplicate values. It can only contain unique values. The values in a set must be of same data type. The set is also mutable in nature. Unlike list set is not ordered.
For example ["ap-south-1", "us-east-1", "us-west-1"]. It is similar to list but it can't contain duplicate values.

3. map
The map data type is used to store key-value pairs. The keys in a map must be unique and the data type of key is always string. The values in a map can be duplicate and they can be of any data type but all the values should be of similar data type only.
For example we can use map to store tags
tags = {
name = "ec2_instance"
description = "ec2 instance for running the backend"
env = "prod"
}

4. tuple
Tuple is also similar to List and it allows you to store ordered values. The difference between list and tuple is that tuple is immutable, meaning the tuple cannot be modified once after it is created and it has fixed length. Unlike list, a tuple can have multiple data types for its values.
For example we can store network addresses in tuple as ["192.168.1.2", "192.168.1.1"]

5. object
The object is used to create variables that contain structured data and named attributes. It is similar to maps but map can contain only similar types of data for its values, whereas object can have multiple data types in its values. For example,
user = {
name = "Rohan"
age = 20
email = "rohan@gmail.com"
}

Conclusion:
Terraform variables are a great way of simplifying the codebase and reusing values without hardcoding literal strings whenever you need to reference something.

Top comments (0)