mariossimou.dev
HomeBlogContact

Terraform Variables

Published on 4th October 2022

Cloud Computing

Introduction

In the previous article we had a quick introduction on terraform and briefly discussed some of its core concepts. We mentioned that Terraform has its own configuration language and in this article we will be covering some details around it. As such, in this article we will be talking about input variables, outputs and local values and include some examples for them.

Input Variables

You may have thought that input variables are like variables in any traditional programming language, however this is not true. The name is a bit misleading and an input variable serves as input/argument in a terraform module. This means that using input variables we can alter the behavior of a terraform module and reduce our codebase. For example, you may have created modules for a prod and stage environment, but both modules are very similar and the only difference between them is the target environment. Using an input variable, you can pass the target environment as an argument and update your setup to include a single module that utilises the environment argument internally.  

variable "environment" {
	description = "This variable describes the target environment"
	type = string
    default = "stage"
    validation {
    	condition = can(regex("^(stage|prod)$", var.environment))
        error_message = "The environment value should either be stage or prod."
    }
    sensitive = false
}

The code snippet above declares a variable with the name environment and we have also provided a few options within it.

  • description: option is used to set some documentation for the variable. The description should provide enough information for the reader to understand how the variable is used.
  • type: option is used to declare the type of the input variable. If its not specified, terraform will accept any value by default, however, it's a good practise to limit the range of the available types to only the necessary ones. In addition, terraform supports a range of data types including string, number, bool, list, map, object, set and tuple.
  • default: option is used to assign a default value to the variable. By default, variables are required in terraform, however, if we assign a default value to a variable this behavior changes. The variable becomes optional and the default value is used as a fallback.
  • validation: block has been recently introduced in terraform and its used to validate an input value in a configuration. In our case we set a regular expression where we accept only stage and prod values. Any other value will be rejected and the error_message will be shown to the user.
  • sensitive: is used to tell terraform that the input is a secret and its value should not been shown in stdout or state. This is often the case when we build the execution plan with terraform plan or terraform apply commands.

Since we now know how to declare an input variable, the next step is: How can we set it?

Whenever we run terraform plan or terraform apply the cli will be asking for the value of the input variable, however, this can be very frustrating, especially when you need to set multiple variables. To avoid that, there are a few other options that we can use.

My preferred approach is to declare a terraform.tfvars file and include the value within it. Terraform will automatically load the value within the file, so there isn't anything complex for us to do. As such, to continue with our previous example, the definition of the environment variable should be added in a terraform.tfvars file in the form environment=stage.

Other approaches for setting an input variable are:

  • Using environment variables prefixed with the name TF_VAR. So our example should be TF_VAR_ENVIRONMENT=stage terraform plan
  • Using the -var option in the cli. This will result to this code terraform plan -var="environment=stage"

Lastly, to access a terraform variable within a terraform module you use the var.NAME syntax. For example, to access the environment variable within the module you can use var.environment.

Output Values

While input variables are arguments we pass in a terraform module, an output value is what is returned after a terraform module has been executed. An output value can be used for a couple of cases, either to pass information from a child to a parent module or print some information on the screen after we run terraform apply. Below you can see an example how to declare an output value:

output "server_id" {
	description = "id of the EC2 instance"
    value = aws_ec2_instance.server.id
}

In this example we declare an output block with the name server_id which includes two options, description and value. The value contains the id of the EC2 instance we have just launched, and the description provides some documentation about it.

Local Values

Local values are used to temporarily assign a variable within a terraform module. This is often the case if you have a value that repeats itself and you want to abstract it in a variable and reuse it. The following code declares a block of two local values:

locals {
	environment = "stage"
    owner = "team"
}

The above example declares the values environment and owner and you can access them using the local.NAME syntax. For example, to access the environment and owner values you can use local.environment and local.owner, respectively.

Summary

In this tutorial we discussed terraform variables which includes:

  • input variables: are used to pass arguments in a terraform module and use those variables within it. Based on the value of the variable we are able to alter the behavior of the module.
  • output values: are used to return a value from a terraform module. It can either be used to pass values from a child to a parent module, or print some information on the screen.
  • local values: are used within a module to temporarily assign a variable and reuse it within the module.

Sources

Designed by
mariossimou.dev
All rights reserved © mariossimou.dev 2023