Terraform Variables

Terraform Variables

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed terraform resources.

https://cloudaffaire.com/terraform-resources/

In this blog post, we will discuss terraform variables.

What is Terraform variable?

Variable provides a way to dynamically assign a value to an attribute in terraform code. Using variable, you can modularize your code for reuse. Instead of assigning values directly to the attributes you can define a variable and refer it in your code. In the future, if you want any modification to your infrastructure or apply any change, you can simply edit the variable block or file. Terraform provide us two way to use variables, directly in your main.tf file or using a separate variable file named terraform.tfvars. In this demo, we will explore inline variables.

Variable Syntax:

#syntax
variable "variable_name"
{
  type = "<variable_type>" 
    #type of the variable: string[string\integer\float\boolean]\list\map
  description = "<variable_description>" 
    #A human-friendly description for the variable
  default = <variable_value>
    #variable value, if ommited value needs to be passed during runtime
}

Variable demo:

#create a directory and get inside it
mkdir terraform && cd terraform

#create main.tf
vi main.tf
---------------
#creates a vpc with two subnets
variable "vpc_tag_name" { #variable type string
type = "string"
default = "myvpc"
}
variable "subnet_count" { #variable type integer
type = "string"
default = "2"
}
variable "subnet_public_ip" { #variable type boolean
type = "string"
default = "true"
}
variable "subnet_cidrs" { #variable type list
type = "list"
default = ["10.0.1.0/24","10.0.2.0/24"]
}
variable "region" { #variable type map
type = "map"
default = {
"Mumbai" = "ap-south-1"
"Singapore" = "ap-southeast-1"
"Sydney" = "ap-southeast-2"
}
}
provider "aws" {
access_key = "<your_access_key>"
secret_key = "<your_secret_key>"
region = "${var.region["Mumbai"]}" #get variable region value (ap-south-1)
}
resource "aws_vpc" "myvpc" {
cidr_block = "10.0.0.0/16"
tags {
Name = "${var.vpc_tag_name}" #get variable vpc_tag_name value (myvpc)
}
}
resource "aws_subnet" "myvpc_subnet" {
count = "${var.subnet_count}" #get variable subnet_count value (2)
vpc_id = "${aws_vpc.myvpc.id}"
cidr_block = "${element(var.subnet_cidrs, count.index)}" #get variable subnet_cidrs value (10.0.1.0/24 and 10.0.2.0/24)
map_public_ip_on_launch = "${var.subnet_public_ip}" #get variable subnet_public_ip value (true)
tags {
   Name = "myvpc_subnet_${count.index+1}"
  }
}
-------------
:wq

#format code
terraform fmt

#initialized terraform
terraform init

#create the resource
terraform apply

#Login to AWS console and check VPC, subnets

#destroy the resources
terraform destroy

#next, we will pass variable value during runtime using -var
#edit main.tf
vi main.tf
--------------
#creates a vpc
variable "vpc_tag_name" { #no default value provided
type = "string"
}
provider "aws" {
access_key = "<your_access_key>"
secret_key = "<your_secret_key>"
region = "ap-south-1"
}
resource "aws_vpc" "myvpc" {
cidr_block = "10.0.0.0/16"
tags {
Name = "${var.vpc_tag_name}" #get variable vpc_tag_name value
}
}
--------------
:wq

#format code
terraform fmt

#create the resource
terraform apply -var vpc_tag_name="myvpc"

#destroy the resources
terraform destroy -var vpc_tag_name="myvpc"

#next, we will pass variable value using environment variable TF_VAR_<variable_name>

#create an environment variable
#The name of the environment variable must be TF_VAR_ followed by the variable name
#terraform will autometically check if vpc_tag_name has any value assigned,
#if not then it will check if TF_VAR_vpc_tag_name has any value assigned,
#if not then it will ask for the value during runtime
export TF_VAR_vpc_tag_name="myvpc"

#create the resource
terraform apply

#destroy the resources
terraform destroy

#cleanup
cd .. && rm -rf terraform

Hope you have enjoyed this article. In the next blog post, we will discuss terraform variable file.

To get more details on terraform, please refer below terraform documentation

https://www.terraform.io/docs/index.html

 

Leave a Reply

Close Menu