LinuxCommandLibrary

terraform-output

Display Terraform output values

TLDR

With no additional arguments, output will display all outputs for the root module

$ terraform output
copy

Output only a value with specific name
$ terraform output [name]
copy

Convert the output value to a raw string (useful for shell scripts)
$ terraform output -raw
copy

Format the outputs as a JSON object, with a key per output (useful with jq)
$ terraform output -json
copy

SYNOPSIS

terraform output [OPTIONS] [NAME]

PARAMETERS

-json
    Output the values in a machine-readable JSON format. If a specific output NAME is provided, it outputs a JSON string for that value; otherwise, it outputs a JSON object containing all outputs.

-raw
    When requesting a single output value that is a string, this option prints the raw string value without any extra quotes or formatting. This is particularly useful for piping the output directly into other shell commands.

-state=PATH
    Specifies the path to the Terraform state file (e.g., terraform.tfstate) to read output values from. If omitted, Terraform will use the default state file in the current working directory or the configured remote state.

-no-color
    Disables colorized output. Useful for logs or environments where color output is not desired or supported.

NAME
    An optional argument to specify a single output variable by its name to display. If omitted, all output variables defined in the state file will be displayed.

DESCRIPTION

The terraform output command is used to extract and display the output values from a Terraform state file. These output values are defined within your Terraform configuration (.tf files) using the output block and typically represent important information about the infrastructure provisioned, such as IP addresses, DNS names, or resource IDs.

This command is crucial for interacting with deployed infrastructure, integrating Terraform with other automation tools, or passing data between different Terraform configurations. By default, it prints all defined output values in a human-readable format. However, it also supports a machine-readable JSON format, making it suitable for scripting and programmatic access to infrastructure details. It retrieves information from the local terraform.tfstate file by default, but can also specify a different state file or interact with remote state if configured.

CAVEATS

The terraform output command relies on a valid and accessible Terraform state file. If the state file is corrupted, missing, or if the Terraform configuration has not been applied (meaning no state exists), the command will fail. Care should be taken when using outputs that might contain sensitive information (e.g., passwords, API keys); while Terraform can mark outputs as sensitive to redact them in console output, they are still present in the state file. Always use the -json flag for programmatic parsing to avoid issues with human-readable formatting variations.

USAGE EXAMPLES

Display all outputs:

terraform output

Display a specific output value:
terraform output public_ip

Display a specific output in raw format (useful for piping):
terraform output -raw instance_id > instance.txt

Display all outputs as JSON:
terraform output -json

Extract a specific JSON value using jq:
terraform output -json | jq -r '.vpc_id.value'

OUTPUT FORMATS

By default, terraform output produces human-readable output, which is easy to scan but can be challenging to parse programmatically. The -json flag is highly recommended for scripting or integrating with other tools, as it provides a stable and predictable JSON structure. When a single output is requested with -json, it outputs just the JSON string representing that value (e.g., "my-string"); when all outputs are requested, it outputs a JSON object where keys are output names and values are objects containing value, type, and sensitive attributes.

HISTORY

The output command has been a fundamental part of Terraform since its initial public release by HashiCorp in 2014. It was designed from the outset to serve as the primary mechanism for extracting valuable information about deployed infrastructure from the Terraform state, enabling introspection, integration with other systems, and chained automation workflows. Its core functionality has remained consistent, though enhancements like the -json flag have been added over time to improve its utility in scripting and API interactions.

SEE ALSO

terraform(1), terraform-plan(1), terraform-apply(1), terraform-destroy(1), jq(1)

Copied to clipboard