1) Using environment variables
To provide an input variable from the environment, export it using the TF_VAR_ prefix followed by the variable name. For example, to set a variable namedfilename:
TF_VAR_<name> pattern and treats them as input values for the root module.
Environment variables set with
TF_VAR_<name> are convenient for CI or temporary local overrides. They are lower priority than command-line flags, but generally take precedence over variable definition files such as *.tfvars.- CI/CD secrets or temporary overrides that you do not want checked into source control.
- Local overrides during development without changing
.tfvarsfiles. - Quick testing of alternate inputs.
2) Variable definition precedence
When multiple sources supply a value for the same variable, OpenTofu follows a deterministic precedence order. The general precedence (highest to lowest) is:| Precedence (highest → lowest) | Source |
|---|---|
| 1. Last-specified CLI flags | -var and -var-file flags provided on the command line |
| 2. Environment variables | TF_VAR_<name> |
| 3. Variable definition files | *.tfvars, terraform.tfvars, and *.auto.tfvars (auto-loaded) |
| 4. Declared defaults in configuration | variable "<name>" { default = ... } in .tf files |
- If you pass multiple
-varor-var-fileflags, the flag specified last on the command line takes priority among them. - Auto-loaded
*.auto.tfvarsfiles are combined; explicit-var-filewill override values from those files.
3) Using a custom variable file with apply
To supply a custom variables file at apply time, use the-var-file flag:
-var-file is a CLI flag, its values take precedence over both environment variables and auto-loaded *.tfvars files. This is useful when you need an environment-specific or sensitive values file that should override defaults.
4) Example project and troubleshooting
Assume the following files exist under/root/opentofu-projects/variables:
main.tfvariables.tf(may be missing initially)throw.auto.tfvarsbasket.auto.tfvarsterraform.tfvars
main.tf (a resource that expects a variable filename):
throw.auto.tfvars might contain:
main.tf references var.filename but the configuration does not declare a variable "filename" {} block, running tofu plan will warn that a value was found in an auto-loaded file and then error because the input variable is undeclared. Example console output:
variables.tf):
default; declaring the variable allows OpenTofu to accept values from .tfvars files, environment variables, or CLI flags. After adding the variable block, tofu plan will proceed and use the appropriate value according to the precedence rules above.
5) Example: command-line -var overrides
Passing a value via the CLI -var flag overrides values from .tfvars files and environment variables:
throw.auto.tfvarscontainsfilename = "/root/baseball.txt"- Environment:
TF_VAR_filename="/root/baseball.txt" - Command line:
-var 'filename="/root/tennis.txt"'
/root/tennis.txt because -var on the command line has the highest priority.
If you have multiple
.tfvars files and *.auto.tfvars files, remember that auto-loaded files are combined; however explicit -var-file or -var on the CLI will override those values.Summary
- Export variables for quick, ephemeral overrides:
export TF_VAR_<name>=<value>. - Always declare variables in the root module with
variable "<name>" {}to avoid undeclared variable errors. - Use
tofu apply -var-file=FILEortofu apply -var 'name=value'to provide or override values at runtime; CLI flags take highest precedence. - Auto-loaded
*.auto.tfvarsfiles are convenient for defaults but can be overridden by environment variables and the CLI.
Links and references
- OpenTofu website: https://opentofu.org/
- OpenTofu documentation: https://opentofu.org/docs/
- Terraform variables documentation (compatible reference for variable behavior): https://developer.hashicorp.com/terraform/language/values/variables