How to use Terraform Input Variables



Terraform input variables provides an easy and more flexible way to define values so that we can change the configuration of the terraform as per our need.

What is Input Variables in Terraform?

Answer - A value defined by the user which we can use inside terraform file for more customization.


Table of Content

  1. Types of Terraform Variables
  2. Terraform Variables - string, number, bool
  3. Terraform Variables - list, set, map


Pre-requisite

Before we start working with Terraform variables, here are the pre-requisites -

  1. You must install terraform (click here on how to install terraform)
  2. You must have either AWS or Google Cloud account (Click to here for AWS and Google Cloud terraform setup tutorial)

1. Types of Terraform Variables

There are two types of variables in Terraform -

  1. Simple values
  2. Collection Variable

1.1 Simple Values variables

As the name suggests Simple Values variables are which hold only a single value. Here the types of Simple Value variables -

  1. string
  2. number
  3. bool

1.2 Collection Variable

In the collection variable, it consists of -

  1. List
  2. Map
  3. Set

2.Terraform Variables - string, number, bool

Let’s take a simple example in which we are going to set up an EC2 instance on AWS.

So to create an EC2 instance we need two things -

  1. provider
  2. resource

Here is the main.tf which we are going to parameterized using terraform variables.

 1provider "aws" {
 2   region     = "eu-central-1"
 3   access_key = "AKIATQ37NXB2OBQHAALW"
 4   secret_key = "ilKygurap8zSErv7jySTDi2796WGqMkEtN6txNHf"
 5}
 6
 7resource "aws_instance" "ec2_example" {
 8   
 9   ami           = "ami-0767046d1677be5a0"
10   instance_type = "t2.micro"
11   
12   tags = {
13           Name = "Terraform EC2"
14   }
15}

2.1 string variable type - We are going parameterized instance_type = “t2.micro”

The first rule to create a parameter in terraform file is by defining variable block

Example -

1variable "instance_type" {
2   description = "Instance type t2.micro"
3   type        = string
4   default     = "t2.micro"
5} 

For defining variable block you need
  1. description : Small or short description about the purpose of the variable
  2. type : What type of variable it is going to be ex - string, bool, number
  3. default : What would be the default value of the variable

Let’s replace the hardcoded value of instance_type with variable
1 instance_type = var.instance_type

Here is our final terraform file after replacing the hardcoded value of a variable -

 1provider "aws" {
 2   region     = "eu-central-1"
 3   access_key = "AKIATQ37NXB2OBQHAALW"
 4   secret_key = "ilKygurap8zSErv7jySTDi2796WGqMkEtN6txNHf"
 5}
 6
 7resource "aws_instance" "ec2_example" {
 8
 9   ami           = "ami-0767046d1677be5a0"
10   instance_type =  var.instance_type
11
12   tags = {
13           Name = "Terraform EC2"
14   }
15}
16
17variable "instance_type" {
18   description = "Instance type t2.micro"
19   type        = string
20   default     = "t2.micro"
21}

And now you can apply your terraform configuration

1terraform apply 

2.2 number variable type - We are going parameterized instance_count = 2

The next variable type we are going to take is number.

For example, we are going to increase the instance_count of the ec2_instances.

Let’s create the variable first -

1variable "instance_count" {
2  description = "EC2 instance count"
3  type        = number
4  default     = 2
5}  

Here is the final terraform file with instance count -

 1provider "aws" {
 2   region     = "eu-central-1"
 3   access_key = "AKIATQ37NXB2AYK7R6PQ"
 4   secret_key = "S1Yg1Qm2JNSej8EHdhPTiu5l5ZD36URs3ed2NyYT"
 5}
 6
 7resource "aws_instance" "ec2_example" {
 8
 9   ami           = "ami-0767046d1677be5a0"
10   instance_type =  "t2.micro"
11   count = var.instance_count
12
13   tags = {
14           Name = "Terraform EC2"
15   }
16}
17
18variable "instance_count" {
19  description = "EC2 instance count"
20  type        = number
21  default     = 2
22}

2.3 boolean variable type - We are going parameterized enable_vpn_gateway = false

The next variable type which we are going to discuss is bool.

The bool variable can be used to set true or false values inside your terraform file.

Here is an example to create your bool variable -

1variable "enable_public_ip" {
2  description = "Enable public IP address"
3  type        = bool
4  default     = true
5}

Let’s create a complete terraform file with bool variable -

 1provider "aws" {
 2   region     = "eu-central-1"
 3   access_key = "AKIATQ37NXB2AYK7R6PQ"
 4   secret_key = "S1Yg1Qm2JNSej8EHdhPTiu5l5ZD36URs3ed2NyYT"
 5}
 6
 7
 8resource "aws_instance" "ec2_example" {
 9
10   ami           = "ami-0767046d1677be5a0"
11   instance_type =  "t2.micro"
12   count = 1
13  associate_public_ip_address = var.enable_public_ip
14
15   tags = {
16           Name = "Terraform EC2"
17   }
18
19}
20
21variable "enable_public_ip" {
22  description = "Enable public IP address"
23  type        = bool
24  default     = true
25}

3. Terraform Variables - list, set, map

When it comes to collection input variables then we are talking about -

  1. List
  2. Map
  3. Set

3.1 List variable type

As the name suggests we are going to define a list that will contain more than one element in it.

Let’s define our first List variable -

Here is the list of IAM users

1variable "user_names" {
2  description = "IAM usernames"
3  type        = list(string)
4  default     = ["user1", "user2", "user3s"]
5}

Here is our final terraform file with List variables -

 1provider "aws" {
 2   region     = "eu-central-1"
 3   access_key = "AKIATQ37NXB2OBQHAALW"
 4   secret_key = "ilKygurap8zSErv7jySTDi2796WGqMkEtN6txNHf"
 5}
 6resource "aws_instance" "ec2_example" {
 7
 8   ami           = "ami-0767046d1677be5a0"
 9   instance_type =  "t2.micro"
10   count = 1
11
12   tags = {
13           Name = "Terraform EC2"
14   }
15
16}
17
18resource "aws_iam_user" "example" {
19  count = length(var.user_names)
20  name  = var.user_names[count.index]
21}
22
23variable "user_names" {
24  description = "IAM usernames"
25  type        = list(string)
26  default     = ["user1", "user2", "user3s"]
27}

3.2 Map variable type

Terraform also supports the map variable type where you can define the key-valye pair.

Let’s take an example where we need to define project and environment, so we can use the map variable to achieve that.

Here is an example of map variable -

1variable "project_environment" {
2  description = "project name and environment"
3  type        = map(string)
4  default     = {
5    project     = "project-alpha",
6    environment = "dev"
7  }
8}

Let’s create a Terraform file

 1provider "aws" {
 2   region     = "eu-central-1"
 3   access_key = "AKIATQ37NXB2OBQHAALW"
 4   secret_key = "ilKygurap8zSErv7jySTDi2796WGqMkEtN6txNHf"
 5}
 6resource "aws_instance" "ec2_example" {
 7
 8   ami           = "ami-0767046d1677be5a0"
 9   instance_type =  "t2.micro"
10
11   tags = var.project_environment
12
13}
14
15
16variable "project_environment" {
17  description = "project name and environment"
18  type        = map(string)
19  default     = {
20    project     = "project-alpha",
21    environment = "dev"
22  }
23}

Read More -

  1. Install terraform on Ubuntu 20.04, CentOS 8, MacOS, Windows 10, Fedora 33, Red hat 8 and Solaris 11
  2. How to setup Virtual machine on Google Cloud Platform using terraform
  3. Create EC2 Instance on AWS using terraform
  4. How to use Terraform Input Variables
  5. What is variable.tf and terraform.tfvars?
  6. How to use Terraform locals?
  7. How to use Terraform output values?
  8. Understanding terraform count, for_each and for loop?
  9. Cloud-nuke : How to nuke AWS resources and save additional AWS infrastructure cost?
  10. How to use Terraform Dynamic blocks?
  11. How to use Terraform resource meta arguments?
  12. How to use Terraform Data sources?
  13. What is terraform provisioner?
  14. Terraform how to do SSH in AWS EC2 instance?
  15. How Terraform modules works?
  16. How to run specific terraform resource?
  17. How to use Workspaces in Terraform?