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
- Types of Terraform Variables
- Terraform Variables - string, number, bool
- Terraform Variables - list, set, map
Pre-requisite
Before we start working with Terraform variables, here are the pre-requisites -
- You must install terraform (click here on how to install terraform)
- 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 -
- Simple values
- 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 -
- string
- number
- bool
1.2 Collection Variable
In the collection variable, it consists of -
- List
- Map
- 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 -
- provider
- 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
- description : Small or short description about the purpose of the variable
- type : What type of variable it is going to be ex -
string
,bool
,number
... - 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 -
- List
- Map
- 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 -
- Install terraform on Ubuntu 20.04, CentOS 8, MacOS, Windows 10, Fedora 33, Red hat 8 and Solaris 11
- How to setup Virtual machine on Google Cloud Platform using terraform
- Create EC2 Instance on AWS using terraform
- How to use Terraform Input Variables
- What is variable.tf and terraform.tfvars?
- How to use Terraform locals?
- How to use Terraform output values?
- Understanding terraform count, for_each and for loop?
- Cloud-nuke : How to nuke AWS resources and save additional AWS infrastructure cost?
- How to use Terraform Dynamic blocks?
- How to use Terraform resource meta arguments?
- How to use Terraform Data sources?
- What is terraform provisioner?
- Terraform how to do SSH in AWS EC2 instance?
- How Terraform modules works?
- How to run specific terraform resource?
- How to use Workspaces in Terraform?
- Securing AWS secrets using HashiCorp Vault with Terraform?
- Managing Terraform states?
- Terraform state locking using DynamoDB (aws_dynamodb_table)
- Terraform and AWS credentials handling?
- Terraform and AWS Multi account Setup?
Terragrunt -
Posts in this Series
- How to use Terragrunt?
- Terraform and AWS Multi account Setup?
- Terraform and AWS credentials handling?
- How to fix-error configuring S3 Backend no valid credential sources for S3 Backend found?
- Terraform state locking using DynamoDB (aws_dynamodb_table)?
- Managing Terraform states?
- Securing AWS secrets using HashiCorp Vault with Terraform?
- How to use Workspaces in Terraform?
- How to run specific terraform resource, module, target?
- How Terraform modules works?
- Terraform how to do SSH in AWS EC2 instance?
- What is terraform provisioner?
- Is terraform destroy needed before terraform apply?
- How to fix terraform error Your query returned no results. Please change your search criteria and try again?
- How to use Terraform Data sources?
- How to use Terraform resource meta arguments?
- How to use Terraform Dynamic blocks?
- Terraform - How to nuke AWS resources and save additional AWS infrastructure cost?
- Understanding terraform count, for_each and for loop?
- How to use Terraform output values?
- How to use Terraform locals?
- How to fix error configuring Terraform AWS Provider error validating provider credentials error calling sts GetCallerIdentity SignatureDoesNotMatch?
- How to fix Invalid function argument on line in provider credentials file google Invalid value for path parameter no file exists
- How to fix error value for undeclared variable a variable named was assigned on the command line?
- What is variable.tf and terraform.tfvars?
- How to use Terraform Input Variables
- Terraform create EC2 Instance on AWS
- How to fix Error creating service account googleapi Error 403 Identity and Access Management (IAM) API has not been used in project before or it is disabled
- How to setup Virtual machine on Google Cloud Platform
- Install terraform on Ubuntu 20.04, CentOS 8, MacOS, Windows 10, Fedora 33, Red hat 8 and Solaris 11