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"
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 Level | Method |
---|---|
1 | Command line flags (-var , -var-file ) |
2 | Environment variables (TF_VAR_* ) |
3 | Auto-loaded files (*.auto.tfvars ) |
4 | Default values defined in the variable block |
5 | No value (error if variable has no default) |
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
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.
Links and References
- OpenTofu Documentation
- Terraform Variables
- Managing Terraform Variables{:target="_blank"}
Watch Video
Watch video content
Practice Lab
Practice lab