An Introduction to Terraform Variables

An Introduction to Terraform Variables

In this Terraform tutorial, we’ll take you through the definition of variables and values, declarations. Terraform variables are key to mastering the basics of Terraform. We’ll discuss specific components of tfvars, such as: Input Variables, How to Assign Values, Output Variables, Local Values, Terraform Variables Best Practices

Getting Started with Terraform Variables

If you’ve read our previous blogs, or if you’re a beginner to the world of Terraform, then it’s a necessity to know about the usage of Terraform variables (tfvars). In this blog, we’ll take you through the definition of variables and values, declarations, usage examples, and leave you with some best practices to get started.

Terraform variables are key to mastering the basics of Terraform, but first, it’s important to understand the definition of a variable: known locations of memory where you can write, read, and reuse the values assigned to the variable. There are three different types of tfvars, which include: 1) input variables, 2) output variables, and 3) local values. We’ll walk you through each type, with key examples to guide you along the way.

As an added bonus, Terraform variables and their definitions can be saved in a different file, with the extension “.tfvars” or “.tfvars.json,” making them easily readable and writable.

In this article, we’ll discuss specific components of tfvars, such as:

  • Input Variables
  • How to Assign Values
  • Output Variables
  • Local Values
  • Terraform Variables Best Practices

Input Variables: Definition and Usage

These tfvars act like key pairs, where for each input variable you’re defining a block with a name, type, and then value. These blocks can be used either in the root module or in the child modules.

Let’s look at a few examples for declaring blocks:

Image for post

Source: Declaring Input Variables

In the above example code, two blocks have been declared.

  1. The first block is for a Terraform variable with the name “availability_zone_names”, where the type is a list of strings and a default value of “us-west-1a” has been assigned.
  2. The second block has the name “docker_ports”, where the type is a list of objects and these objects come in key-value pairs, where there are 3 keys:
  3. a) internal
  4. b) external, which takes the value as numbers
  5. c) protocol, which will accept string values. Accordingly, default values have been assigned to “protocol.”

As seen in the above examples, it should now be clear that Terraform variables are always declared after using the keyword “variable”. We then start to define a block by using the open curly brackets “{“. Inside the block, we can define the type, and assign default values. Finally, the block ends with a closing curly bracket “}”.

NOTE: The data types seen in the below snippet are supported:

Image for post

Source: Types and Values

Other than the above data types, another keyword “any” is used to define that a particular tfvars can be assigned with any value.

We can also have a description tag present between the opening and closing brackets. The description is used mostly for documentation purposes, and the usage of the Terraform variable is added here. Default values and descriptions are not mandatory and can be skipped if not required. The below example is a snippet from one of our configuration files where we have written a description, but skipped default values.

Image for post

How to Assign Values

Up until this point, we’ve discussed how to define and give default values. But, what if there is no default assigned? In this case, how can we assign them?

Firstly, it’s necessary to understand that Terraform variables can be defined either in the root module or child module. When they are defined in child modules, we directly pass values to these modules from their parent module or the caller module.

If your tfvars are defined in the root module, things get a bit more interesting. There are actually three different ways in which values could be assigned (with pros and cons to each, of course). We’ll walk you through your options:

How to Assign Values from the Root Module:

  1. Through commands on the CLI
  2. The “.tfvars” file
  3. Using Environment variables

1._ Through commands on the CLI_

Using the CLI is the most conventional way, but there are disadvantages. When you are using either the “plan” or “apply” commands, you may use the option “-var”. The option may be used any number of times with these commands (even in a single command). However, we would not recommend going through commands on the CLI, particularly if you have many or complex variables in your configuration modules.

Here’s an example of how to use the “apply” command with “-var”:

Image for post

  1. The “tfvars” file — a workaround

As we mentioned above, if you have many or complex variables in your configuration modules, using commands on the CLI may not be the best way. However, there is a workaround: using the definition file. When you have many variables that need to be assigned values, consider creating a file with the extension “.tfvars” or “.tfvars.json” which will make the process simpler and more efficient.

It’s easy to do — simply define a file with only the Terraform variables, with their values, and name the file with the extensions mentioned above. Once completed, you may use the “apply” command with the option “-var-file” and the file name. Below is an example of how this command may be used:

Image for post

*NOTE: *If you have any files with the name “terraform.tfvars” or “terraform.tfvars.json”, then these variable definition files are loaded automatically.

Image for post

3.** Using Environment variables**

This is a dynamic value that has been set-up on your operating system and it affects how any process on your system works. These are always in pairs. In this case, your system is searched for the environment variables whose name might be in the format as seen below:

Image for post

Below is an example of how you may assign values in this case:

Image for post

terraform devops

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Extend your DevOps Strategy For Success in the Cloud?

DevOps and Cloud computing are joined at the hip, now that fact is well appreciated by the organizations that engaged in SaaS cloud and developed applications in the Cloud. During the COVID crisis period, most of the organizations have started using cloud computing services and implementing a cloud-first strategy to establish their remote operations. Similarly, the extended DevOps strategy will make the development process more agile with automated test cases.

What Is DevOps and Is Enterprise DevOps Any Good?

What is DevOps? How are organizations transitioning to DevOps? Is it possible for organizations to shift to enterprise DevOps? Read more to find out!

DevOps Basics: What You Should Know

What is DevOps? What are the goals it helps achieves? What are its benefits? This article has answers!

Why You Should Be Adopting a DevOps Culture in 2020 - DZone DevOps

The year 2020 has arrived, and its arrival brings a lot of innovations and transformations in the Information and Technology (IT) sector to DevOps technologies.

Measuring DevOps Metrics: A How-To Guide

DevOps is supposed to help streamline the process of taking code changes and getting them to production for users to enjoy. But what exactly does it mean for the process to be "streamlined"? One way to answer this is to start measuring metrics.