github.com/hugorut/terraform@v1.1.3/website/docs/cli/commands/console.mdx (about)

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