2016-11-14 07:57:28 +01:00
---
2021-12-15 03:41:17 +01:00
page_title: 'Command: console'
description: >-
The terraform console command provides an interactive console for evaluating
expressions.
2016-11-14 07:57:28 +01:00
---
# Command: console
2018-05-14 01:59:18 +02:00
The `terraform console` command provides an interactive console for
2021-12-15 03:41:17 +01:00
evaluating [expressions](/language/expressions).
2016-11-14 07:57:28 +01:00
## Usage
main: new global option -chdir
This new option is intended to address the previous inconsistencies where
some older subcommands supported partially changing the target directory
(where Terraform would use the new directory inconsistently) where newer
commands did not support that override at all.
Instead, now Terraform will accept a -chdir command at the start of the
command line (before the subcommand) and will interpret it as a request
to direct all actions that would normally be taken in the current working
directory into the target directory instead. This is similar to options
offered by some other similar tools, such as the -C option in "make".
The new option is only accepted at the start of the command line (before
the subcommand) as a way to reflect that it is a global command (not
specific to a particular subcommand) and that it takes effect _before_
executing the subcommand. This also means it'll be forced to appear before
any other command-specific arguments that take file paths, which hopefully
communicates that those other arguments are interpreted relative to the
overridden path.
As a measure of pragmatism for existing uses, the path.cwd object in
the Terraform language will continue to return the _original_ working
directory (ignoring -chdir), in case that is important in some exceptional
workflows. The path.root object gives the root module directory, which
will always match the overriden working directory unless the user
simultaneously uses one of the legacy directory override arguments, which
is not a pattern we intend to support in the long run.
As a first step down the deprecation path, this commit adjusts the
documentation to de-emphasize the inconsistent old command line arguments,
including specific guidance on what to use instead for the main three
workflow commands, but all of those options remain supported in the same
way as they were before. In a later commit we'll make those arguments
produce a visible deprecation warning in Terraform's output, and then
in an even later commit we'll remove them entirely so that -chdir is the
single supported way to run Terraform from a directory other than the
one containing the root module configuration.
2020-09-02 00:45:12 +02:00
Usage: `terraform console [options]`
2016-11-14 07:57:28 +01:00
2019-04-10 21:17:28 +02:00
This command provides an interactive command-line console for evaluating and
2021-12-15 03:41:17 +01:00
experimenting with [expressions](/language/expressions).
2022-02-08 00:45:07 +01:00
You can use it to test interpolations before using them in configurations
and to interact with any values currently saved in
[state](/language/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
2022-02-08 22:22:44 +01:00
[built-in functions](/language/functions). The console holds a [lock on the state](/language/state/locking), and you will not be able to use the console while performing other actions that modify state.
2016-11-14 07:57:28 +01:00
2022-02-08 00:45:07 +01:00
To close the console, enter the `exit` command or press Control-C
2016-11-17 10:23:50 +01:00
or Control-D.
2021-03-25 00:17:03 +01:00
For configurations using
2021-12-15 03:41:17 +01:00
[the `local` backend](/language/settings/backends/local) only,
2021-03-25 00:17:03 +01:00
`terraform console` accepts the legacy command line option
2021-12-15 03:41:17 +01:00
[`-state`](/language/settings/backends/local#command-line-arguments).
2021-03-25 00:17:03 +01:00
2016-11-14 07:57:28 +01:00
## Scripting
The `terraform console` command can be used in non-interactive scripts
by piping newline-separated commands to it. Only the output from the
2018-05-14 01:59:18 +02:00
final command is printed unless an error occurs earlier.
2016-11-14 07:57:28 +01:00
2018-05-14 01:59:18 +02:00
For example:
2016-11-14 07:57:28 +01:00
2017-04-05 17:29:27 +02:00
```shell
2021-05-25 16:06:23 +02:00
$ echo 'split(",", "foo,bar,baz")' | terraform console
tolist([
"foo",
"bar",
"baz",
])
2016-11-14 07:57:28 +01:00
```
## Remote State
2021-12-15 03:41:17 +01:00
If [remote state](/language/state/remote) is used by the current backend,
2018-05-14 01:59:18 +02:00
Terraform will read the state for the current workspace from the backend
before evaluating any expressions.
2021-05-25 16:06:23 +02:00
## Examples
The `terraform console` command will read the Terraform configuration in the
current working directory and the Terraform 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`:
```hcl
variable "apps" {
type = map(any)
default = {
"foo" = {
"region" = "us-east-1",
},
"bar" = {
"region" = "eu-west-1",
},
"baz" = {
"region" = "ap-south-1",
},
}
}
resource "random_pet" "example" {
for_each = var.apps
}
```
Executing `terraform console` will drop you into an interactive shell where you
can test interpolations to:
Print a value from a map:
```
> var.apps.foo
{
"region" = "us-east-1"
}
```
Filter a map based on a specific value:
```
> { for key, value in var.apps : key => value if value.region == "us-east-1" }
{
"foo" = {
"region" = "us-east-1"
}
}
```
Check if certain values may not be known until apply:
```
> random_pet.example
(known after apply)
```
Test various functions:
```
> cidrnetmask("172.16.0.0/12")
"255.240.0.0"
```