tofu console is an interactive prompt where you type an OpenTofu® expression and see its value straight away, using the variables, local values and state of the configuration in the folder. It’s the quickest way to test an expression before you put it in a file.
Syntax
tofu console [options]
-var 'NAME=VALUE': sets an input variable for this session. Repeat it for more variables.-var-file=FILENAME: reads variable values from a.tfvarsfile.
At the > prompt, type an expression and press Return (Enter on Windows and Linux). Type exit, or press Control-C or Control-D, to leave. The options are in Command: console (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: Create a Small Configuration
In an empty folder, create main.tf with a variable and a local value:
variable "city" {
type = string
default = "Lisbon"
}
locals {
welcome = "Welcome to ${var.city}!"
}
Step 2: Evaluate Expressions
This configuration uses no providers, so the console works without tofu init. Start it in the folder:
tofu console
Type each expression at the prompt:
> var.city
"Lisbon"
> local.welcome
"Welcome to Lisbon!"
> "${var.city} in spring"
"Lisbon in spring"
> exit
The last line is a new expression, typed straight into the console: it never existed in the file.
Step 3: Try Another Value
Give the variable another value for one session with -var. The file doesn’t change:
tofu console -var city=Porto
> local.welcome
"Welcome to Porto!"
> exit
Clean Up
The console changes nothing. Delete the folder when you’re done.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Running tofu apply while the console is open | It stops with Error acquiring the state lock: the console locks the state while it runs. | Type exit first. |
Using tofu console in a script | The docs warn it isn’t designed for scripts, and some functions may not work as intended. | Use it to experiment, and put the expression in a file. |
FAQ
Can I see resource attributes in the console?
Yes, once they’re in the state. The docs show that a value OpenTofu only knows after apply prints as (known after apply).
Does the console need a configuration?
No. In an empty folder it still evaluates expressions and functions, so you can try the language on its own.
Conclusion
Run tofu console in your project, type an expression, and see its value at once. Leave with exit before you run anything that changes the state.
Related
- OpenTofu Tutorial — Part 5: Locals, Expressions and tofu console
- OpenTofu Locals vs Variables: When to Use Each
- OpenTofu String Templates and Heredocs Explained
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