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