Terraform Variables


Terraform has emerged as one of the leading provisioning tools for multi-cloud development. It uses its own syntax-based language named – HCL (Hashicorp Configuration Language), although it is itself developed in GoLang.

Terraform is used for automating and managing infrastructures, platforms, and services using declarative language. Built for the purpose of Infrastructure as Code (IaC) Solution, Terraform supports multiple cloud service providers. It is an Open Source tool developed by HashiCorp. Using IaC, we can manage infrastructure setup with configuration files. For e.g., we can deploy, update, and manage our infrastructure by defining the required resources.

What will we cover?

In this guide, we will learn about Terraform variables. We will see why Terraform variables are important, and how we should define and use them.

Why should you care about Terraform variables?

In Terraform, we can assign values to the arguments via literals like integers and strings. This may be a good approach for small infrastructure configurations, but as the size of the infrastructure grows, this approach becomes very inefficient. In such cases, we can use Terraform’s input variables, where we declare a value in only one place and then use that value in other parts of the code and files by referencing them. This approach is more efficient as it makes the code reusable and composable.

Note that Terraform uses the term ‘variables’ or ‘Terraform variables’ to reference input variables.

Defining a Variable

In Terraform, variables are defined inside a ‘variable’ named block, which has the following type of syntax:

variable VARIABLE_NAME { ARGUMENTS }

The ‘variable’ keyword itself declares the block as a ‘variable’ block type. The ‘VARIABLE_NAME’, after the variable keyword, is the name of the variable we are going to define. This name must be distinctive from other variables within the same module. Using this name, the variable is assigned a value from outside. Similarly, using the variable name, we can reference its value within the module.

Note that the following reserved identifiers cannot be used for a variable name: source, version, providers, count, for_each, lifecycle, depends_on, locals.

Arguments for the Variable Block

The following arguments can be declared inside a variable type block:

default – It sets a default value for the block. A variable is optional if a default value is present. This value will be used if no other value is specified while calling a module.

type – It specifies the types of values that can be used with the variable.

description – It is a sort of comment about the variable itself.

validation – It is a block to define conditions/checks for a variable.

sensitive – It hides confidential data to be shown while running the ‘plan’ or ‘apply’ command.

nullable – It controls whether the variable can be assigned a value within the module.

Accessing a Variable’s Values

To access a value of the variable from within the module, use the format:

var.<Name>

Where ‘var’ is a prefix and is the label after the keyword ‘variable’. This assigned value of a variable can only be accessed in expressions within the module where it was defined. As an example, consider a variable ‘key’ defined as:

variable “instance_type” {
default = “t2.micro”
}

 
We can reference the above variable as follows:

resource «aws_instance» «example» {
instance_type = «var.instance_type»
ami = var.image_id
}

 

Command Line variables

For assigning a value to a variable on the command line, Terraform uses the ‘-var’ option. For e.g., when the Terraform plan and Terraform apply command are executed, we can do as follows:

1. Passing an instance ami-id

$ terraform apply -var=«image_id=ami-xyz123»

 
2. Passing a list of instance ami-ids

$ terraform apply -var=‘image_id_list=[«ami-xyz123″,»ami-abc456»]’ -var=«instance_type=t2.micro»

 
3. Passing a Map type value

$ terraform apply -var=‘image_id_map={«us-east-1″:»ami-xyz123″,»us-east-2″:»ami-abc456»}’

 

Variable Definitions Files (.tfvars)

When there are a lot of variables, it is a good idea to put them all in a variable definition file (a .tfvars file or .tfvars.json file). We can then pass this file on the command line as follows:

$ terraform apply -var-file=«example.tfvars»

 
This file will be parsed automatically if it is named ‘terraform.tfvars’.

Note that we do not put this file in any source control system to avoid exposure to any sensitive data.

Also if we add ‘.automóvil’ to a file name before ‘.tfvars’, for example, myfile.automóvil.tfvars, this file will be loaded automatically without specifying the ‘var-file’ flag. This is the same as the ‘terraform.tfvars’ file.

Environment Variables

Environment variables, with names prefixed with TF_VAR, can be used to define input variables. For e.g., “TF_VAR_mykey” will refer to the value of a Terraform variable named “mykey”. To set a single value for this environment variable on UNIX/Linux, use the format:

$ TF_VAR_mykey = “fhj7-47DD-hLl02-pJu8”

 
Similarly, for Windows OS, use the format:

set TF_VAR_mykey = “fhj7-47DD-hLl02-pJu8”

 
Alternatively, we can also use the Terraform.tfvars file.

Wrapping Up

We are now finally closing this article, although there are many other things to cover. We have presented a high-level view of Terraform variables. Besides Terraform, there are also other tools like AWS CloudFormation, Chef, Puppet, Ansible, and so on. Each tool has its own advantages and limitations. The benefit of using Terraform is that it is not bound to a particular service provider.



Source link