How to pass inputs to terraform variables

Please read about variable blocks and variable types before proceeding with this blog. When it comes to providing values to variables, you may do it in the following methods, which we will go over in detail in this article.

  1. Defaults
  2. pass through console
  3. environment variables
  4. variable flags
  5. variable files

Defaults

code to give default variables
default values

These are the values you give to a variable while declaring. If you don’t use any of the other above mentioned ways to pass value to a variable, then the value of wish is cloud-architect.

Console

Giving input to a variable via console
variable input via console

Like we discussed variables in other blog, when you don’t give a default value, it will be asked during terraform plan unless you use other means to pass the value to the variable. So here in console we type the respective value

Environment Variables

environment variables are very important incase you are passing secret info like passwords or keys. Since only admins can view these. Let’s see how we can use it.

Inorder to use, you have to use the following structure for the name of environmental variable. Let’s say you are having a variable named sqlpass then you should use TF_VAR_sqlpass i.e; you need to prefix TF_VAR_<var_name>. Below example demonstrates the same

#windows
set TF_VAR_sqlpass="SturdieTechie.Com"
#Linux
export TF_VAR_sqlpass="SturdieTechie.Com"

If you run the above commands in cmd, it will set the environment variable. The below code shows the same.

use of environment variables
environment variables usage

Variable flags

We can pass the value to variable using -var flag to terraform plan or apply. These are also called command line flags. below example shows how to pass

-var="<var_name>=<var_value>"

In the above example, we have given both var flag and default, still terraform chooses value from flags. we will define the order at the end.

Variable files

We can create variable files with the extension terraform.tfvars. The variables are defined using the name=value format. Below is one sample file that contains various types of variables configured.

#terraform.tfvars file
rg_name="myresources"
number_dbs=5
tags={"BillingGroup"="SturdieTechie"}
cert_names=["first","Second"]

Coming to the name of the variable file, it matters. If you are using terraform.tfvars as the name, then it will be used during plan/apply to fetch the values from it. But if you are changing name, let’s say, prod_vars.tfvars then you have to use var-file flag during plan/apply .

#example
terraform plan -var-file="prod_vars.tfvars"

Also, you can use the extension filename.auto.tfvars, but whatever the name is, you don’t have to use var-file flag. But if you have multiple files with same format, then terraform will process in the alphabetical order and the last one processed will be taken as the values.

Variables order

So now Let’s say you have more than one way to pass the variables, which one will be used is the biggest question. Let’s answer it.

The defaults are always used first, followed by checks for environment variables and variables files. It will start with terraform.tfvars, then look for *.auto.tfvars files and process them in alphabetical order. Finally, it will look at the command line flags. As a result, the final value standing will be chosen from among all possible inputs.

Default values
Environment variables
terraform.tfvars
*.auto.tfvars (alphabetical order)
-var or -var-file (command line flags)
Order of processing variables

Thanks for reading the blog, More and more content coming in. Please stay tuned. Happy Infra!

Leave a comment

Your email address will not be published. Required fields are marked *