Skip to main content
0 reactions

OpenTofu Locals vs Variables: When to Use Each

Card titled Locals vs Variables, comparing a variable, read as var.name and set from outside with -var or .tfvars files, with locals, read as local.greeting, worked out inside and not settable from outside.
A variable comes from outside; a local value is worked out inside

OpenTofu® has two kinds of named values that look alike: input variables and local values. A variable is set from outside the configuration; a local value is worked out inside it and can’t be set from outside. This guide shows both side by side, and where each one belongs.

Syntax

SyntaxHCL
locals {
  <NAME> = <EXPRESSION>
}
  • locals: the block that declares local values. A configuration can have several locals blocks.
  • <NAME>: the local value’s name. It must be unique in the module.
  • <EXPRESSION>: any expression, such as text, a number, or a value built from variables, resources and other local values.

To read a local value, write local.<NAME>, with no “s”. An input variable is declared in a variable block and read as var.<NAME>. Both are described in Local Values (opens in a new tab) and Input Variables (opens in a new tab) in the OpenTofu docs.

Prerequisites

Step 1: Declare a Variable

In an empty folder, create variables.tf with a variable for the name to greet:

variables.tfHCL
variable "name" {
  description = "Who to greet."
  type        = string
  default     = "World"
}

Step 2: Build a Local Value from It

Create main.tf. The local value greeting puts the variable into a sentence, and an output prints it:

main.tfHCL
locals {
  greeting = "Hello, ${var.name}!"
}

output "greeting" {
  value = local.greeting
}

Step 3: Apply It

This configuration uses no providers, so there’s nothing for tofu init to download: tofu apply works straight away. Run it and type yes:

TerminalBash
tofu apply
Output (the lines that matter)Plain text
Outputs:

greeting = "Hello, World!"

Step 4: Set the Variable from Outside

A variable takes a new value without any change to the files. Set it on the command line with -var:

TerminalBash
tofu apply -var name=Ana
Output (the lines that matter)Plain text
Outputs:

greeting = "Hello, Ana!"

The local value followed, because it’s built from the variable.

Step 5: Try to Set the Local Value

A local value has no such door. Try to set greeting the same way:

TerminalBash
tofu plan -var greeting=Hi
OutputPlain text
╷
│ Error: Value for undeclared variable
│ 
│ A variable named "greeting" was assigned on the command line, but the root module does
│ not declare a variable of that name. To use this value, add a "variable" block to the
│ configuration.
╵

OpenTofu only accepts values for declared variables. A local value is changed in the file, nowhere else.

Which One to Use

Input variableLocal value
Declared invariable blocklocals block
Read asvar.<NAME>local.<NAME>
Set from outsideYes: -var, .tfvars files and environment variablesNo
Can use other valuesNo: a default must be a literal valueYes: variables, resources and other local values
Use it forSettings that change between usesA value worked out from others, used in several places

The docs advise using local values in moderation: for a value used in many places that’s likely to change, so you change it in one place.

Clean Up

This example created nothing but a state file. Delete the folder when you’re done.

Common Mistakes

MistakeWhat happensDo this instead
Writing locals.greetingOpenTofu stops with Reference to undeclared resource.Read it as local.greeting.
Turning every value into a localThe real values hide behind names, and the file gets harder to read.Keep local values for expressions you use more than once.

FAQ

Can a local value use a variable?

Yes, as greeting does here. It can also use resource attributes and other local values.

Is a local value visible to other modules?

No. The docs say a local value can only be used in the module where it’s declared. To hand a value to another module, use an output.

Conclusion

Use an input variable for a value that comes from outside, and a local value for one the configuration works out itself. Declare locals in a locals block and read them as local.<NAME>.

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