Command: output
The terraform output
command is used to extract the value of
an output variable from the state file.
Usage
Usage: terraform output [options] [NAME]
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.
The command-line flags are all optional. The following flags are available:
-json
- If specified, the outputs are formatted as a JSON object, with a key per output. IfNAME
is specified, only the output specified will be returned. This can be piped into tools such asjq
for further processing.-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.-no-color
- If specified, output won't contain any color.-state=path
- Path to the state file. Defaults to "terraform.tfstate". Ignored when remote state is used.
Note: When using the -json
or -raw
command-line flag, any sensitive
values in Terraform state will be displayed in plain text. For more information,
see Sensitive Data in State.
Examples
These examples assume the following Terraform output snippet.
To list all outputs:
Note that Terraform does not redact the value when you specify the output by name:
To query for the DNS address of the load balancer:
To query for all instance IP addresses:
Use in automation
The terraform output
command by default displays in a human-readable format,
which can change over time to improve clarity.
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:
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.
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.