Understanding variables

If you've ever used any programming language, then you might be familiar with variables already. In most common case, they allow you to assign a value (a number or string or something else) to some hand-picked name and reference this value by this name inside your code. If you need to modify the value, then you just need to do it once, in a place where variable is defined.

Unlike in programming languages, variables in Terraform are more like input data for your templates: you define them before using the template. During the Terraform run, you have zero control over variables. The values of variables never change; you can't modify them inside the template.

In the previous chapter, we already tried variables in order to configure modules. We also learned that our template.tf is a module: root module. Let's define some variables for the root module.

It is a common pattern to split variables, template, and outputs into three different files. As you might remember, Terraform loads all files with the .tf extension from the current folder, so you don't need to do any extra steps to join these three files. Let's create a new file variables.tf with the following content:

variable "region" {}  

Now let's use it inside template.tf to configure AWS provider:

provider "aws" { 
  region = "${var.region}" 
} 

If we would try to apply or plan template now, Terraform would interactively ask us for the value of this variable:

$> terraform plan 
var.region 
  Enter a value: 

That's nice and sometimes convenient, but in most cases, you don't want to type values of all variables every time. Not only it's inconvenient, but it could also be dangerous: mistyping a variable value can lead to terrible consequences. If one of the variables is used inside a resource parameter that causes its recreation, then mistyping it will lead to accidental removal of the resource. Don't rely on the manual input of configuration data.

We could reduce the chances of accidental infrastructure destruction by adding a description to the variable:

variable "region" {  
  description = "AWS region. Changing it will lead to loss of complete stack." 
} 

Now the user of the template will see this description when he or she tries to apply it:

    $> terraform plan
    var.region
      AWS region. Changing it will lead to loss of complete stack.
    
      Enter a value: 

It doesn't save us from typos though. What would be an even more reliable way to protect the infrastructure from human mistakes is to have a default value for the variable:

variable "region" {  
  description = "AWS region. Changing it will lead to loss of complete stack." 
  default = "eu-central-1" 
} 

With the default value in place, Terraform won't ask for the value interactively anymore. It will pick default value unless other sources of variables are present.

There are three types of variables you can set:

  • the string variables (default ones)
  • the map variables
  • the list variables

You can only interactively set the string variables; for map and list, you have to use other methods, which we will take a look at a bit later.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.239.195