github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/website/docs/cli/commands/output.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Command: output"
     4  sidebar_current: "docs-commands-output"
     5  description: |-
     6    The `terraform output` command is used to extract the value of an output variable from the state file.
     7  ---
     8  
     9  # Command: output
    10  
    11  The `terraform output` command is used to extract the value of
    12  an output variable from the state file.
    13  
    14  ## Usage
    15  
    16  Usage: `terraform output [options] [NAME]`
    17  
    18  With no additional arguments, `output` will display all the outputs for
    19  the root module. If an output `NAME` is specified, only the value of that
    20  output is printed.
    21  
    22  The command-line flags are all optional. The list of available flags are:
    23  
    24  * `-json` - If specified, the outputs are formatted as a JSON object, with
    25      a key per output. If `NAME` is specified, only the output specified will be
    26      returned. This can be piped into tools such as `jq` for further processing.
    27  * `-raw` - If specified, Terraform will convert the specified output value to a
    28      string and print that string directly to the output, without any special
    29      formatting. This can be convenient when working with shell scripts, but
    30      it only supports string, number, and boolean values. Use `-json` instead
    31      for processing complex data types.
    32  * `-no-color` - If specified, output won't contain any color.
    33  * `-state=path` - Path to the state file. Defaults to "resource_state.json".
    34      Ignored when [remote state](/docs/language/state/remote.html) is used.
    35  
    36  ## Examples
    37  
    38  These examples assume the following Terraform output snippet.
    39  
    40  ```hcl
    41  output "instance_ips" {
    42    value = aws_instance.web.*.public_ip
    43  }
    44  
    45  output "lb_address" {
    46    value = aws_alb.web.public_dns
    47  }
    48  
    49  output "password" {
    50    sensitive = true
    51    value = var.secret_password
    52  }
    53  ```
    54  
    55  To list all outputs:
    56  
    57  ```shellsession
    58  $ terraform output
    59  instance_ips = [
    60    "54.43.114.12",
    61    "52.122.13.4",
    62    "52.4.116.53"
    63  ]
    64  lb_address = "my-app-alb-1657023003.us-east-1.elb.amazonaws.com"
    65  password = <sensitive>
    66  ```
    67  
    68  Note that outputs with the `sensitive` attribute will be redacted:
    69  
    70  ```shellsession
    71  $ terraform output password
    72  password = <sensitive>
    73  ```
    74  
    75  To query for the DNS address of the load balancer:
    76  
    77  ```shellsession
    78  $ terraform output lb_address
    79  "my-app-alb-1657023003.us-east-1.elb.amazonaws.com"
    80  ```
    81  
    82  To query for all instance IP addresses:
    83  
    84  ```shellsession
    85  $ terraform output instance_ips
    86  instance_ips = [
    87    "54.43.114.12",
    88    "52.122.13.4",
    89    "52.4.116.53"
    90  ]
    91  ```
    92  
    93  ## Use in automation
    94  
    95  The `terraform output` command by default displays in a human-readable format,
    96  which can change over time to improve clarity.
    97  
    98  For scripting and automation, use `-json` to produce the stable JSON format.
    99  You can parse the output using a JSON command-line parser such as
   100  [jq](https://stedolan.github.io/jq/):
   101  
   102  ```shellsession
   103  $ terraform output -json instance_ips | jq -r '.[0]'
   104  54.43.114.12
   105  ```
   106  
   107  For the common case of directly using a string value in a shell script, you
   108  can use `-raw` instead, which will print the string directly with no extra
   109  escaping or whitespace.
   110  
   111  ```shellsession
   112  $ terraform output -raw lb_address
   113  my-app-alb-1657023003.us-east-1.elb.amazonaws.com
   114  ```
   115  
   116  The `-raw` option works only with values that Terraform can automatically
   117  convert to strings. Use `-json` instead, possibly combined with `jq`, to
   118  work with complex-typed values such as objects.
   119  
   120  Terraform strings are sequences of Unicode characters rather than raw bytes,
   121  so the `-raw` output will be UTF-8 encoded when it contains non-ASCII
   122  characters. If you need a different character encoding, use a separate command
   123  such as `iconv` to transcode Terraform's raw output.