- The Command Line
- Basic CLI Features
- Command: console
Command: console
The tofu console
command provides an interactive console for
evaluating expressions.
Usage
Usage: tofu console [options]
This command provides an interactive command-line console for evaluating and experimenting with expressions. You can use it to test interpolations before using them in configurations and to interact with any values currently saved in state. If the current state is empty or has not yet been created, you can use the console to experiment with the expression syntax and built-in functions. The console holds a lock on the state, and you will not be able to use the console while performing other actions that modify state.
To close the console, enter the exit
command or press Control-C
or Control-D.
For configurations using
the local
backend only,
tofu console
accepts the legacy command line option
-state
.
Use of variables in module sources,
backend configuration,
or encryption block
requires assigning values to root module variables
when running tofu console
.
This command also accepts the following options for tofu console:
-
-var 'NAME=VALUE'
- Sets a value for a single input variable declared in the root module of the configuration. Use this option multiple times to set more than one variable. Refer to Input Variables on the Command Line for more information. -
-var-file=FILENAME
- Sets values for potentially many input variables declared in the root module of the configuration, using definitions from a "tfvars" file. Use this option multiple times to include values from more than one file.
There are several other ways to set values for input variables in the root
module, aside from the -var
and -var-file
options. Refer to
Assigning Values to Root Module Variables for more information.
Scripting
The tofu console
command can be used in non-interactive scripts
by piping newline-separated commands to it. Only the output from the
final command is printed unless an error occurs earlier.
For example:
Remote State
If remote state is used by the current backend, OpenTofu will read the state for the current workspace from the backend before evaluating any expressions.
Examples
The tofu console
command will read the OpenTofu configuration in the
current working directory and the OpenTofu state file from the configured
backend so that interpolations can be tested against both the values in the
configuration and the state file.
With the following main.tf
:
Executing tofu console
will drop you into an interactive shell where you
can test interpolations to:
Print a value from a map:
Filter a map based on a specific value:
Check if certain values may not be known until apply:
Test various functions: