An OpenTofu® variable with a default is optional: when you don’t set it, OpenTofu uses the default. A variable without one is required, and OpenTofu won’t plan until it has a value.
This guide shows both kinds side by side, what OpenTofu does when a required value is missing, and how to give it one.
Syntax
variable "<NAME>" {
type = <TYPE>
}
variable "<NAME>" {
type = <TYPE>
default = <VALUE>
}
- The first block has no
default, so the variable is required. - The second has
default = <VALUE>, so it’s optional. The value must match<TYPE>.
See Default values (opens in a new tab) in the OpenTofu docs.
Prerequisites
- OpenTofu installed. Installing OpenTofu (opens in a new tab) in the OpenTofu docs covers every system.
Step 1: Declare a Required and an Optional Variable
In an empty folder, create variables.tf. shop_name has no default; featured_book has one:
variable "shop_name" {
description = "Name of the shop, shown in the page heading."
type = string
}
variable "featured_book" {
description = "Book shown under New this week."
type = string
default = "Salt and Starlight by Nadia Quill"
}
Create outputs.tf, so the plan shows both values:
output "heading" {
value = "Welcome to the ${var.shop_name}"
}
output "featured_book" {
value = var.featured_book
}
Step 2: Let OpenTofu Ask for the Value
tofu plan
OpenTofu prints the variable’s name and description, then waits. Type a value and press Enter:
var.shop_name
Name of the shop, shown in the page heading.
Enter a value: Riverside Bookshop
Changes to Outputs:
+ featured_book = "Salt and Starlight by Nadia Quill"
+ heading = "Welcome to the Riverside Bookshop"
You can apply this plan to save these new output values to the OpenTofu state, without
changing any real infrastructure.
The optional featured_book used its default, and nobody was asked.
Step 3: See What Happens When No One Can Answer
Scripts and pipelines can’t answer a question. -input=false tells OpenTofu not to ask, as automation does:
tofu plan -input=false
╷
│ Error: No value for required variable
│
│ on variables.tf line 1:
│ 1: variable "shop_name" {
│
│ The root module input variable "shop_name" is not set, and has no default value. Use a
│ -var or -var-file command line argument to provide a value for this variable.
╵
Step 4: Give the Value in a File
Create terraform.tfvars, which OpenTofu reads by itself:
shop_name = "Riverside Bookshop"
tofu plan
Changes to Outputs:
+ featured_book = "Salt and Starlight by Nadia Quill"
+ heading = "Welcome to the Riverside Bookshop"
You can apply this plan to save these new output values to the OpenTofu state, without
changing any real infrastructure.
No question this time: the file answered it.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Giving a required variable a placeholder default, such as "change-me" | Nobody is asked, and the placeholder ends up in real resources. | Leave the default out, so OpenTofu insists on a value. |
| Leaving a required variable unset in automation | The run fails with No value for required variable. | Set it in a .tfvars file or with -var. |
FAQ
Can I set a required variable to null?
Only if it allows null. Variables allow null by default; add nullable = false to refuse it. See Disallowing Null Input Values (opens in a new tab).
Does tofu apply ask too?
Yes. tofu apply asks for missing values the same way, before it shows the plan.
Conclusion
Leave out default when a value has no sensible default and must come from the person running OpenTofu; give a default when most runs use the same value.
Related
- OpenTofu Tutorial — Part 4: Variables, tfvars and Outputs
- How to Use Input Variables in OpenTofu
- OpenTofu .tfvars vs .auto.tfvars
Additional Resources
OpenTofu® is a registered trademark of LF Projects, LLC. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by the OpenTofu project.
0 comments