Terraform Environment Variables

Terraform Environment Variables

In the last article, How to Debug Terraform, we talked about tips that could save a lot of time. Today, we will see that Terraform contains some Environment variables that help us automate the workflow to execute the phases needed to deploy our resources.

When we talk about CI/CD, the Terraform has some environment variables that are not so popular because we usually do not configure them in our local computer. So it makes more sense on the server or CI/CD like Gitlab.

Terraform Environment Variables

Let’s some examples of environment variables that Terraform helps us with automation.

The TF_INPUT could receive two different values, “0” or “false. We can use this environment variable to ignore all prompts that terraform may ask us to specify the values of some variables that we didn’t specify. You may already see or use “-input=false” on the command line, so the environment it’s an alternative for that argument.

How to specify Terraform Variable Values using Environment Variables

The Terraform support several variables types and our projects always have a bunch of them.  

The main idea that makes us use Terraform it’s automated our deployments and keep tracking all changes along the way in a repository like Git to keep consistency between several environments.

And because of that, we need always keep in mind that our terraform code should be dynamic to support multiple environments, avoiding changing our code to support a new environment each time we need to deploy it.

Let’s see the environment variable TF_VAR_variable_name.

We can follow this pattern TF_VAR_variable_name to specify a value for an existing variable from our Terraform project.

Let’s see one example. First, we define the variable below in our configuration:

variable "DB_HOST" {
  type = string
  description = "Specify the HOST from Database."

So, we can specify the terraform environment variables, like the example below:

export TF_VAR_DB_HOST="db.www.bitslovers.com"

So, we need to run “terraform plan” and “terraform apply,” The value for this variable will be the same as the value found on the environment variable.

Suppose you use CI/CD like GitLab. This approach’s pretty good because something you have different Runners, we can configure the variables with different values directly on the Runner server, so we don’t need to hardcode the values in a file. Check our article that explains how to execute Terraform on Gitlab.

How to append argument on Terraform Command Line

Now, let’s learn how we can append arguments on any command on Terraform. We can use the environment variable TF_CLI_ARG to append an extra argument for any Terraform CLI argument. To be more precise, let’s see one example: how this works.

TF_CLI_ARGS="-input=false" terraform apply -force 

All arguments in TF_CLI_ARGS have priority, so it’s appended after a subcommand like “apply”. For example, if we don’t use the TF_CLI_ARGS manually, it will be the same as:

 terraform apply -input=false -force

So, if we specify the value for TF_CLI_ARGS, all commands that we execute will be affected. So, if you are looking for a solution to establish a standard workflow for your CI, this environment variable could be a good alternative.

Let’s a second alternative. We also have the environment variable TF_CLI_ARGS_subcommand_name, where we can specify a subcommand from Terraform to append extra arguments.

Let’s see one example:


We appended the “-refresh=false” argument to the subcommand “plan” in the example above.

How to change the Working Directory on Terraform

Recently, I was working on a specific project where the server had a limit and high-security roles, which the file system had read-only access. And, we had issues executing Terraform from that server because the workdir from the Terraform project could be changed. So, I figure out one environment where we can specify a different working directory.

By default, the working directory from a Terraform project it’s the current directory from our project, specifically under sub-directory “.terraform“.

We can specify the environment variable TF_DATA_DIR. Like:

export TF_DATA_DIR=/tmp

Select a Workspace on Terraform

In case you use Terraform Workspace. You can automatically select which workspace you want to use when you execute your Terraform commands, so we don’t need always specify the workspace, adding an extra argument on the command line.

export TF_WORKSPACE=dev

Using the TF_WORKSPACE variable, you can define a “default” workspace, and you don’t need to specify or run “select” always.


When I started learning and working with Terraform, I wasn’t aware that there are a lot of environment variables, that could help us to automate or configure some custom workflow. It’s pretty helpful to be aware, that we may need them someday.

Follow Me On Twitter!

Leave a Comment

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

Free PDF with a useful Mind Map that illustrates everything you should know about AWS VPC in a single view.