Skip to main content
0 reactions

How to Use tofu console to Test OpenTofu Expressions

Card titled tofu console, showing a main.tf with the default Lisbon and a welcome local value, and a console window where local.welcome prints Welcome to Lisbon!
Test any OpenTofu expression at a prompt before you put it in a file

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

TerminalBash
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 .tfvars file.

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

Step 1: Create a Small Configuration

In an empty folder, create main.tf with a variable and a local value:

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

TerminalBash
tofu console

Type each expression at the prompt:

tofu consolePlain text
> 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:

TerminalBash
tofu console -var city=Porto
tofu consolePlain text
> local.welcome
"Welcome to Porto!"
> exit

Clean Up

The console changes nothing. Delete the folder when you’re done.

Common Mistakes

MistakeWhat happensDo this instead
Running tofu apply while the console is openIt stops with Error acquiring the state lock: the console locks the state while it runs.Type exit first.
Using tofu console in a scriptThe 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.

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