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.
|
2014-07-25 00:44:10 +02:00
|
|
|
* `-state=path` - Path to the state file. Defaults to "terraform.tfstate".
|
2017-03-15 18:20:26 +01:00
|
|
|
Ignored when [remote state](/docs/state/remote.html) is used.
|
2015-05-27 17:11:31 +02:00
|
|
|
* `-module=module_name` - The module path which has needed output.
|
|
|
|
By default this is the root path. Other modules can be specified by
|
|
|
|
a period-separated list. Example: "foo" would reference the module
|
|
|
|
"foo" but "foo.bar" would reference the "bar" module in the "foo"
|
|
|
|
module.
|
2016-08-21 21:17:31 +02:00
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
These examples assume the following Terraform output snippet.
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
output "lb_address" {
|
|
|
|
value = "${aws_alb.web.public_dns}"
|
|
|
|
}
|
|
|
|
|
|
|
|
output "instance_ips" {
|
2016-09-13 12:31:51 +02:00
|
|
|
value = ["${aws_instance.web.*.public_ip}"]
|
2016-08-21 21:17:31 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
To list all outputs:
|
|
|
|
|
|
|
|
```text
|
|
|
|
$ terraform output
|
|
|
|
```
|
|
|
|
|
|
|
|
To query for the DNS address of the load balancer:
|
|
|
|
|
|
|
|
```text
|
|
|
|
$ terraform output lb_address
|
|
|
|
my-app-alb-1657023003.us-east-1.elb.amazonaws.com
|
|
|
|
```
|
|
|
|
|
|
|
|
To query for all instance IP addresses:
|
|
|
|
|
|
|
|
```text
|
|
|
|
$ terraform output instance_ips
|
|
|
|
test = [
|
|
|
|
54.43.114.12,
|
|
|
|
52.122.13.4,
|
|
|
|
52.4.116.53
|
|
|
|
]
|
|
|
|
```
|
|
|
|
|
|
|
|
To query for a particular value in a list, use `-json` and a JSON
|
|
|
|
command-line parser such as [jq](https://stedolan.github.io/jq/).
|
|
|
|
For example, to query for the first instance's IP address:
|
|
|
|
|
|
|
|
```text
|
|
|
|
$ terraform output -json instance_ips | jq '.value[0]'
|
|
|
|
```
|