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
locals {
<NAME> = <EXPRESSION>
}
locals: the block that declares local values. A configuration can have severallocalsblocks.<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
- OpenTofu installed. Installing OpenTofu (opens in a new tab) in the OpenTofu docs covers every system.
Step 1: Declare a Variable
In an empty folder, create variables.tf with a variable for the name to greet:
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:
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:
tofu apply
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:
tofu apply -var name=Ana
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:
tofu plan -var greeting=Hi
╷
│ 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 variable | Local value | |
|---|---|---|
| Declared in | variable block | locals block |
| Read as | var.<NAME> | local.<NAME> |
| Set from outside | Yes: -var, .tfvars files and environment variables | No |
| Can use other values | No: a default must be a literal value | Yes: variables, resources and other local values |
| Use it for | Settings that change between uses | A 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
| Mistake | What happens | Do this instead |
|---|---|---|
Writing locals.greeting | OpenTofu stops with Reference to undeclared resource. | Read it as local.greeting. |
| Turning every value into a local | The 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>.
Related
- OpenTofu Tutorial — Part 5: Locals, Expressions and tofu console
- How to Use Input Variables in OpenTofu
- How to Use tofu console to Test OpenTofu Expressions
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