Skip to main content
0 reactions

OpenTofu Required vs Optional Variables: How default Works

Card titled Required vs Optional Variables, comparing a required variable, which has no default and asks for a value, with an optional one, which has a default and never asks.
OpenTofu Required vs Optional Variables.

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

SyntaxHCL
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

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:

variables.tfHCL
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:

outputs.tfHCL
output "heading" {
  value = "Welcome to the ${var.shop_name}"
}

output "featured_book" {
  value = var.featured_book
}

Step 2: Let OpenTofu Ask for the Value

TerminalBash
tofu plan

OpenTofu prints the variable’s name and description, then waits. Type a value and press Enter:

Output (the lines that matter)Plain text
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:

TerminalBash
tofu plan -input=false
OutputPlain text
╷
│ 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:

terraform.tfvarsHCL
shop_name = "Riverside Bookshop"
TerminalBash
tofu plan
Output (the lines that matter)Plain text
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

MistakeWhat happensDo 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 automationThe 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.

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