2014-07-25 00:44:10 +02:00
|
|
|
---
|
|
|
|
layout: "docs"
|
|
|
|
page_title: "Command: output"
|
|
|
|
sidebar_current: "docs-commands-output"
|
2014-10-22 05:21:56 +02:00
|
|
|
description: |-
|
|
|
|
The `terraform output` command is used to extract the value of an output variable from the state file.
|
2014-07-25 00:44:10 +02:00
|
|
|
---
|
|
|
|
|
|
|
|
# Command: output
|
|
|
|
|
|
|
|
The `terraform output` command is used to extract the value of
|
|
|
|
an output variable from the state file.
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
2016-07-12 00:37:51 +02:00
|
|
|
Usage: `terraform output [options] [NAME]`
|
2014-07-25 00:44:10 +02:00
|
|
|
|
2016-08-21 21:17:31 +02:00
|
|
|
With no additional arguments, `output` will display all the outputs for
|
|
|
|
the root module. If an output `NAME` is specified, only the value of that
|
|
|
|
output is printed.
|
2014-07-25 00:44:10 +02:00
|
|
|
|
|
|
|
The command-line flags are all optional. The list of available flags are:
|
|
|
|
|
2016-07-13 18:38:19 +02:00
|
|
|
* `-json` - If specified, the outputs are formatted as a JSON object, with
|
2016-07-12 00:37:51 +02:00
|
|
|
a key per output. If `NAME` is specified, only the output specified will be
|
|
|
|
returned. This can be piped into tools such as `jq` for further processing.
|
2020-12-09 01:28:35 +01:00
|
|
|
* `-raw` - If specified, Terraform will convert the specified output value to a
|
|
|
|
string and print that string directly to the output, without any special
|
|
|
|
formatting. This can be convenient when working with shell scripts, but
|
|
|
|
it only supports string, number, and boolean values. Use `-json` instead
|
|
|
|
for processing complex data types.
|
2019-06-21 09:31:31 +02:00
|
|
|
* `-no-color` - If specified, output won't contain any color.
|
2014-07-25 00:44:10 +02:00
|
|
|
* `-state=path` - Path to the state file. Defaults to "terraform.tfstate".
|
2021-01-15 23:13:53 +01:00
|
|
|
Ignored when [remote state](/docs/language/state/remote.html) is used.
|
2016-08-21 21:17:31 +02:00
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
These examples assume the following Terraform output snippet.
|
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
```hcl
|
2020-11-06 20:36:11 +01:00
|
|
|
output "instance_ips" {
|
|
|
|
value = aws_instance.web.*.public_ip
|
2016-08-21 21:17:31 +02:00
|
|
|
}
|
|
|
|
|
2020-11-06 20:36:11 +01:00
|
|
|
output "lb_address" {
|
|
|
|
value = aws_alb.web.public_dns
|
2016-08-21 21:17:31 +02:00
|
|
|
}
|
2019-08-28 21:34:22 +02:00
|
|
|
|
|
|
|
output "password" {
|
|
|
|
sensitive = true
|
2020-11-06 20:36:11 +01:00
|
|
|
value = var.secret_password
|
2019-08-28 21:34:22 +02:00
|
|
|
}
|
2016-08-21 21:17:31 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
To list all outputs:
|
|
|
|
|
2020-11-06 20:36:11 +01:00
|
|
|
```shellsession
|
2016-08-21 21:17:31 +02:00
|
|
|
$ terraform output
|
2020-11-06 20:36:11 +01:00
|
|
|
instance_ips = [
|
|
|
|
"54.43.114.12",
|
|
|
|
"52.122.13.4",
|
|
|
|
"52.4.116.53"
|
|
|
|
]
|
|
|
|
lb_address = "my-app-alb-1657023003.us-east-1.elb.amazonaws.com"
|
|
|
|
password = <sensitive>
|
2016-08-21 21:17:31 +02:00
|
|
|
```
|
|
|
|
|
2019-08-28 21:34:22 +02:00
|
|
|
Note that outputs with the `sensitive` attribute will be redacted:
|
2020-11-16 16:08:55 +01:00
|
|
|
|
2020-11-06 20:36:11 +01:00
|
|
|
```shellsession
|
2019-08-28 21:34:22 +02:00
|
|
|
$ terraform output password
|
|
|
|
password = <sensitive>
|
|
|
|
```
|
|
|
|
|
2016-08-21 21:17:31 +02:00
|
|
|
To query for the DNS address of the load balancer:
|
|
|
|
|
2020-11-06 20:36:11 +01:00
|
|
|
```shellsession
|
2016-08-21 21:17:31 +02:00
|
|
|
$ terraform output lb_address
|
2020-11-06 20:36:11 +01:00
|
|
|
"my-app-alb-1657023003.us-east-1.elb.amazonaws.com"
|
2016-08-21 21:17:31 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
To query for all instance IP addresses:
|
|
|
|
|
2020-11-06 20:36:11 +01:00
|
|
|
```shellsession
|
2016-08-21 21:17:31 +02:00
|
|
|
$ terraform output instance_ips
|
2020-11-06 20:36:11 +01:00
|
|
|
instance_ips = [
|
|
|
|
"54.43.114.12",
|
|
|
|
"52.122.13.4",
|
|
|
|
"52.4.116.53"
|
2016-08-21 21:17:31 +02:00
|
|
|
]
|
|
|
|
```
|
|
|
|
|
2020-11-06 20:36:11 +01:00
|
|
|
## Use in automation
|
|
|
|
|
|
|
|
The `terraform output` command by default displays in a human-readable format,
|
2020-12-09 01:28:35 +01:00
|
|
|
which can change over time to improve clarity.
|
2016-08-21 21:17:31 +02:00
|
|
|
|
2020-12-09 01:28:35 +01:00
|
|
|
For scripting and automation, use `-json` to produce the stable JSON format.
|
|
|
|
You can parse the output using a JSON command-line parser such as
|
|
|
|
[jq](https://stedolan.github.io/jq/):
|
2020-11-06 20:36:11 +01:00
|
|
|
|
|
|
|
```shellsession
|
2020-12-09 01:28:35 +01:00
|
|
|
$ terraform output -json instance_ips | jq -r '.[0]'
|
|
|
|
54.43.114.12
|
2020-11-06 20:36:11 +01:00
|
|
|
```
|
|
|
|
|
2020-12-09 01:28:35 +01:00
|
|
|
For the common case of directly using a string value in a shell script, you
|
|
|
|
can use `-raw` instead, which will print the string directly with no extra
|
|
|
|
escaping or whitespace.
|
2020-11-06 20:36:11 +01:00
|
|
|
|
|
|
|
```shellsession
|
2020-12-09 01:28:35 +01:00
|
|
|
$ terraform output -raw lb_address
|
|
|
|
my-app-alb-1657023003.us-east-1.elb.amazonaws.com
|
2016-08-21 21:17:31 +02:00
|
|
|
```
|
2020-12-09 01:28:35 +01:00
|
|
|
|
|
|
|
The `-raw` option works only with values that Terraform can automatically
|
|
|
|
convert to strings. Use `-json` instead, possibly combined with `jq`, to
|
|
|
|
work with complex-typed values such as objects.
|
|
|
|
|
|
|
|
Terraform strings are sequences of Unicode characters rather than raw bytes,
|
|
|
|
so the `-raw` output will be UTF-8 encoded when it contains non-ASCII
|
|
|
|
characters. If you need a different character encoding, use a separate command
|
|
|
|
such as `iconv` to transcode Terraform's raw output.
|