OpenTofu Error: 'No value for required variable' in non-interactive runs
Fix OpenTofu's 'No value for required variable' error on plan/apply: supply values via -var, -var-file, TF_VAR_ env vars, or a default in automation and CI pipelines.
- #opentofu
- #terraform
- #iac
- #troubleshooting
- #errors
Stuck on this OpenTofu error? Get the free incident triage checklist
A one-page PDF — the exact steps to isolate, fix, and verify a production error like this one. No spam, unsubscribe anytime.
Exact Error Message
╷
│ Error: No value for required variable
│
│ on variables.tf line 1:
│ 1: variable "environment" {
│
│ The root module input variable "environment" is not set, and has no
│ default value. Use a -var or -var-file command line argument to provide
│ a value for this variable.
╵
In an interactive terminal OpenTofu would prompt you for the value instead; the error only appears when input is disabled — in CI, with -input=false, or when stdin is not a TTY.
What It Means
A variable block with no default is a required input. When you run tofu plan or tofu apply without supplying that value, and OpenTofu cannot prompt for it interactively, the run fails immediately.
This is expected behavior, not a bug. It most often bites automation because pipelines run non-interactively, so the usual prompt fallback is unavailable and any unset required variable becomes a hard error.
Common Causes
- Running in CI or a script where stdin is not a terminal, so OpenTofu cannot prompt.
- Passing
-input=false(common in automation) while a required variable is unset. - A
terraform.tfvars/*.auto.tfvarsfile that was not committed, not in the working directory, or misnamed. - A
TF_VAR_environment variable that is unset or misspelled. - Adding a new required variable to a module without updating the callers or pipeline.
- Passing a var file with
-var-filebut pointing at the wrong path.
Diagnostic Commands
Reproduce the failure the way CI sees it, with input disabled:
tofu plan -input=false
List which variables the configuration declares and their defaults:
grep -rn 'variable "' variables.tf
Check whether the matching environment variable is actually set:
env | grep '^TF_VAR_'
Confirm your var file exists and is auto-loaded (only terraform.tfvars and *.auto.tfvars load automatically):
ls -la *.tfvars *.auto.tfvars
Step-by-Step Resolution
-
Identify the unset variable from the error — it names the variable and the file where it is declared.
-
Supply the value on the command line for a one-off run:
tofu apply -input=false -var="environment=staging"
- For repeatable runs, put values in a var file and pass it explicitly:
# staging.tfvars
environment = "staging"
region = "us-east-1"
tofu apply -input=false -var-file=staging.tfvars
- In CI, prefer environment variables so secrets stay out of the command line. The
TF_VAR_prefix maps to the variable name:
export TF_VAR_environment=staging
tofu apply -input=false
- If the variable should have a safe fallback, give it a
defaultso it is no longer required:
variable "environment" {
type = string
default = "dev"
}
- Re-run non-interactively to confirm the value is picked up:
tofu plan -input=false
Plan: 2 to add, 0 to change, 0 to destroy.
Prevention
- Always run automation with
-input=falseso a missing variable fails loudly instead of hanging on a prompt. - Keep a committed
*.auto.tfvars(or explicit-var-file) per environment so required values load automatically. - Use
TF_VAR_environment variables for secrets rather than putting them in tracked var files or on the command line. - When you add a required variable, update every caller and pipeline in the same change.
- Give variables a sensible
defaultwhen a fallback is genuinely safe, and leave it out only when the value must be explicit.
Related Errors
Invalid value for variable— the value was supplied but failed avalidationblock.Reference to undeclared input variable— usingvar.xwhere novariable "x"block exists.Duplicate variable declaration— twovariableblocks share a name.Variables not allowed— a variable used in a context (likebackend) that does not accept them.
Frequently Asked Questions
Why does it work locally but fail in CI? Locally OpenTofu prompts you for the missing value; CI runs non-interactively (or with -input=false), so there is no prompt and the run errors.
What is the difference between -var and -var-file? -var sets one value inline, while -var-file loads many values from a file; use the file for repeatable, per-environment configuration.
How do environment variables map to variables? A shell variable named TF_VAR_environment supplies the value for the OpenTofu variable environment; the suffix after TF_VAR_ is the variable name.
Should I just add a default to silence this? Only when a fallback is genuinely safe. For values that must be chosen per run — like environment or a target account — keeping the variable required prevents accidental deploys to the wrong place. For reusable troubleshooting prompts, browse the prompt library, and for more fixes see the OpenTofu guides.
Fixed it? Get 500 OpenTofu & DevOps AI prompts — free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.
Did this fix your issue?
Get 500 Battle-Tested DevOps AI Prompts — Free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.