OpenTofu: A Beginners Guide to a Terraform Fork Including Migration From Terraform

OpenTofu Basics

Demo Using Variables in OpenTofu

In this guide, we’ll cover multiple methods to pass input variables into your OpenTofu configurations. You’ll learn how to:

  • Inject variables via environment variables
  • Understand OpenTofu’s variable precedence
  • Supply custom variable files
  • Declare and override variables in your project

By the end, you’ll be confident working with TF_VAR_ exports, .tfvars files, and command-line flags.


1. Injecting Variables with Environment Variables

The simplest approach is to set an environment variable prefixed with TF_VAR_. OpenTofu picks these up automatically:

export TF_VAR_filename="./data/baseball.txt"

The image shows a Visual Studio Code interface with a task on using environment variables in OpenTofu scripts, alongside a terminal window. The left panel displays a file explorer, and the main panel contains a README file with instructions.

Note

Using TF_VAR_ is ideal for CI/CD pipelines or local development when you want to avoid hardcoding sensitive values.


2. Variable Definition Precedence

OpenTofu resolves variables based on a fixed priority list. The highest precedence is given to values passed with -var or -var-file. Below is the full order:

Precedence LevelMethod
1Command line flags (-var, -var-file)
2Environment variables (TF_VAR_*)
3Auto-loaded files (*.auto.tfvars)
4Default values defined in the variable block
5No value (error if variable has no default)

The image shows a coding environment with a task question about variable definition precedence in Terraform on the left, and a Visual Studio Code editor with a welcome message and terminal on the right.

For more details, see OpenTofu Input Variables{:target="_blank"}.


3. Supplying a Custom Variable File

You can group variables in a file (e.g., variables.tfvars) and pass it with -var-file:

tofu apply -var-file=variables.tfvars

OpenTofu will load all key-value pairs from that file.


Hands-On: Working with Variables in a Sample Project

Navigate to /root/OpenTofu/projects/variables. The directory contains:

  • basket.auto.tfvars
  • main.tf
  • terraform.tfvars
  • throw.auto.tfvars

The image shows a code editor with a file explorer on the left, displaying a directory structure, and a terminal at the bottom. The main section shows a file named `basket.auto.tfvars` with a line of code setting a filename variable.

4. Running tofu plan Without Declared Variables

If main.tf references var.filename but you haven’t declared the variable, you’ll see:

$ cd /root/OpenTofu/projects/variables
$ tofu plan
Warning: Value for undeclared variable
Error: Reference to undeclared input variable

  on main.tf line 2, in resource "local_file" "games":
   2:   filename = var.filename

An input variable with the name "filename" has not been declared. This variable can be declared with a variable "filename" {} block.

Warning

OpenTofu requires every referenced variable to be declared. Always include a variable block to avoid plan/apply failures.

5. Declaring the filename Variable

Create a variables.tf file next to main.tf:

variable "filename" {
  description = "Path to the output file"
  type        = string
}

Re-run tofu plan—the error should disappear.


6. Overriding Variables on the Command Line

To set or override filename at runtime:

tofu apply -var='filename=/root/tennis.txt'

Since -var has the highest precedence, it will override any environment or .tfvars settings.

The image shows a coding environment with a task question on the left asking about a command line flag in OpenTofu, and a code editor on the right displaying a Terraform configuration file and terminal output with warnings and errors related to undeclared variables.


Watch Video

Watch video content

Practice Lab

Practice lab

Previous
Demo Variables