github.com/jaredpalmer/terraform@v1.1.0-alpha20210908.0.20210911170307-88705c943a03/website/docs/cli/commands/console.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Command: console"
     4  sidebar_current: "docs-commands-console"
     5  description: "The terraform console command provides an interactive console for
     6    evaluating expressions."
     7  ---
     8  
     9  # Command: console
    10  
    11  The `terraform console` command provides an interactive console for
    12  evaluating [expressions](/docs/language/expressions/index.html).
    13  
    14  ## Usage
    15  
    16  Usage: `terraform console [options]`
    17  
    18  This command provides an interactive command-line console for evaluating and
    19  experimenting with [expressions](/docs/language/expressions/index.html).
    20  This is useful for testing interpolations before using them in configurations,
    21  and for interacting with any values currently saved in
    22  [state](/docs/language/state/index.html).
    23  
    24  If the current state is empty or has not yet been created, the console can be
    25  used to experiment with the expression syntax and
    26  [built-in functions](/docs/language/functions/index.html).
    27  
    28  You can close the console with the `exit` command or by pressing Control-C
    29  or Control-D.
    30  
    31  For configurations using
    32  [the `local` backend](/docs/language/settings/backends/local.html) only,
    33  `terraform console` accepts the legacy command line option
    34  [`-state`](/docs/language/settings/backends/local.html#command-line-arguments).
    35  
    36  ## Scripting
    37  
    38  The `terraform console` command can be used in non-interactive scripts
    39  by piping newline-separated commands to it. Only the output from the
    40  final command is printed unless an error occurs earlier.
    41  
    42  For example:
    43  
    44  ```shell
    45  $ echo 'split(",", "foo,bar,baz")' | terraform console
    46  tolist([
    47    "foo",
    48    "bar",
    49    "baz",
    50  ])
    51  ```
    52  
    53  ## Remote State
    54  
    55  If [remote state](/docs/language/state/remote.html) is used by the current backend,
    56  Terraform will read the state for the current workspace from the backend
    57  before evaluating any expressions.
    58  
    59  ## Examples
    60  
    61  The `terraform console` command will read the Terraform configuration in the
    62  current working directory and the Terraform state file from the configured
    63  backend so that interpolations can be tested against both the values in the
    64  configuration and the state file.
    65  
    66  With the following `main.tf`:
    67  
    68  ```hcl
    69  variable "apps" {
    70    type = map(any)
    71    default = {
    72      "foo" = {
    73        "region" = "us-east-1",
    74      },
    75      "bar" = {
    76        "region" = "eu-west-1",
    77      },
    78      "baz" = {
    79        "region" = "ap-south-1",
    80      },
    81    }
    82  }
    83  
    84  resource "random_pet" "example" {
    85    for_each = var.apps
    86  }
    87  ```
    88  
    89  Executing `terraform console` will drop you into an interactive shell where you
    90  can test interpolations to:
    91  
    92  Print a value from a map:
    93  
    94  ```
    95  > var.apps.foo
    96  {
    97    "region" = "us-east-1"
    98  }
    99  ```
   100  
   101  Filter a map based on a specific value:
   102  
   103  ```
   104  > { for key, value in var.apps : key => value if value.region == "us-east-1" }
   105  {
   106    "foo" = {
   107      "region" = "us-east-1"
   108    }
   109  }
   110  ```
   111  
   112  Check if certain values may not be known until apply:
   113  
   114  ```
   115  > random_pet.example
   116  (known after apply)
   117  ```
   118  
   119  Test various functions:
   120  
   121  ```
   122  > cidrnetmask("172.16.0.0/12")
   123  "255.240.0.0"
   124  ```