• Overview
    • Enforce Policy as Code
    • Infrastructure as Code
    • Inject Secrets into Terraform
    • Integrate with Existing Workflows
    • Manage Kubernetes
    • Manage Virtual Machine Images
    • Multi-Cloud Deployment
    • Network Infrastructure Automation
    • Terraform CLI
    • Terraform Cloud
    • Terraform Enterprise
  • Registry
  • Tutorials
    • About the Docs
    • Intro to Terraform
    • Configuration Language
    • Terraform CLI
    • Terraform Cloud
    • Terraform Enterprise
    • Provider Use
    • Plugin Development
    • Registry Publishing
    • Integration Program
    • Terraform Tools
    • CDK for Terraform
    • Glossary
  • Community
GitHubTerraform Cloud
Download

    Terraform CLI

  • Overview
  • Basic CLI Features
    • Overview
    • init
    • get
    • Overview
    • plan
    • apply
    • destroy
    • Overview
    • login
    • logout
    • Overview
    • console
    • fmt
    • validate
    • 0.13upgrade
    • 0.12upgrade
    • Overview
    • graph
    • output
    • show
    • state list
    • state show
    • Overview
    • import
    • Usage Tips
    • Resource Importability
    • Overview
    • Resource Addressing
    • state
      • Overview
      • state list
      • state show
      • refresh
      • Overview
      • taint
      • untaint
      • Overview
      • state mv
      • state rm
      • state replace-provider
      • Overview
      • state pull
      • state push
      • force-unlock
    • Overview
      • Overview
      • workspace list
      • workspace select
      • workspace new
      • workspace delete
      • workspace show
    • Overview
    • Plugin Signing
    • providers
    • version
    • providers lock
    • providers mirror
    • providers schema
    • Overview
    • CLI Configuration
    • Environment Variables
    • Overview
    • Terraform Cloud Settings
    • Initializing and Migrating
    • Command Line Arguments
    • Running Terraform in Automation
    • GitHub Actions
    • Overview
    • apply
    • console
    • destroy
    • env
    • fmt
    • force-unlock
    • get
    • graph
    • import
    • init
    • login
    • logout
    • output
    • plan
    • providers
    • providers lock
    • providers mirror
    • providers schema
    • push (deprecated)
    • refresh
    • show
    • state
    • state list
    • state mv
    • state pull
    • state push
    • state replace-provider
    • state rm
    • state show
    • taint
    • test (deprecated)
    • untaint
    • validate
    • version
    • workspace
    • workspace list
    • workspace select
    • workspace new
    • workspace delete
    • workspace show
    • 0.12upgrade
    • 0.13upgrade
    • Overview
    • apply
    • console
    • destroy
    • env
    • fmt
    • force-unlock
    • get
    • graph
    • import
    • init
    • login
    • logout
    • output
    • plan
      • providers
      • providers lock
      • providers mirror
      • providers schema
    • push (deprecated)
    • refresh
    • show
      • state
      • state list
      • state mv
      • state pull
      • state push
      • state replace-provider
      • state rm
      • state show
    • taint
    • test (deprecated)
    • untaint
    • validate
    • version
      • workspace
      • workspace list
      • workspace select
      • workspace new
      • workspace delete
      • workspace show
    • 0.12upgrade
    • 0.13upgrade
    • APT Packages for Debian and Ubuntu
    • Yum Packages for Red Hat Enterprise Linux, Fedora, and Amazon Linux

  • Terraform Internals

  • Other Docs

  • Intro to Terraform
  • Configuration Language
  • Terraform CLI
  • Terraform Cloud
  • Terraform Enterprise
  • Provider Use
  • Plugin Development
  • Registry Publishing
  • Integration Program
  • Terraform Tools
  • CDK for Terraform
  • Glossary
Type '/' to Search

»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 list of available flags are:

  • -json - If specified, the outputs are formatted as a JSON object, with 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.
  • -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.

output "instance_ips" {
  value = aws_instance.web.*.public_ip
}

output "lb_address" {
  value = aws_alb.web.public_dns
}

output "password" {
  sensitive = true
  value = var.secret_password
}
output "instance_ips" {
  value = aws_instance.web.*.public_ip
}

output "lb_address" {
  value = aws_alb.web.public_dns
}

output "password" {
  sensitive = true
  value = var.secret_password
}

To list all outputs:

$ terraform output
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>
$ terraform output
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>

Note that outputs with the sensitive attribute will be redacted:

$ terraform output password
password = <sensitive>
$ terraform output password
password = <sensitive>

To query for the DNS address of the load balancer:

$ terraform output lb_address
"my-app-alb-1657023003.us-east-1.elb.amazonaws.com"
$ terraform output lb_address
"my-app-alb-1657023003.us-east-1.elb.amazonaws.com"

To query for all instance IP addresses:

$ terraform output instance_ips
instance_ips = [
  "54.43.114.12",
  "52.122.13.4",
  "52.4.116.53"
]
$ terraform output instance_ips
instance_ips = [
  "54.43.114.12",
  "52.122.13.4",
  "52.4.116.53"
]

»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:

$ terraform output -json instance_ips | jq -r '.[0]'
54.43.114.12
$ terraform output -json instance_ips | jq -r '.[0]'
54.43.114.12

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.

$ terraform output -raw lb_address
my-app-alb-1657023003.us-east-1.elb.amazonaws.com
$ terraform output -raw lb_address
my-app-alb-1657023003.us-east-1.elb.amazonaws.com

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.

github logoEdit this page
  • Overview
  • Docs
  • Extend
  • Privacy
  • Security
  • Press Kit
  • Consent Manager