github.com/hugorut/terraform@v1.1.3/website/docs/cli/commands/output.mdx (about) 1 --- 2 page_title: 'Command: output' 3 description: >- 4 The `terraform output` command is used to extract the value of an output 5 variable from the state file. 6 --- 7 8 # Command: output 9 10 The `terraform output` command is used to extract the value of 11 an output variable from the state file. 12 13 ## Usage 14 15 Usage: `terraform output [options] [NAME]` 16 17 With no additional arguments, `output` will display all the outputs for 18 the root module. If an output `NAME` is specified, only the value of that 19 output is printed. 20 21 The command-line flags are all optional. The list of available flags are: 22 23 * `-json` - If specified, the outputs are formatted as a JSON object, with 24 a key per output. If `NAME` is specified, only the output specified will be 25 returned. This can be piped into tools such as `jq` for further processing. 26 * `-raw` - If specified, Terraform will convert the specified output value to a 27 string and print that string directly to the output, without any special 28 formatting. This can be convenient when working with shell scripts, but 29 it only supports string, number, and boolean values. Use `-json` instead 30 for processing complex data types. 31 * `-no-color` - If specified, output won't contain any color. 32 * `-state=path` - Path to the state file. Defaults to "terraform.tfstate". 33 Ignored when [remote state](/language/state/remote) is used. 34 35 -> **Note:** When using the `-json` or `-raw` command-line flag, any sensitive 36 values in Terraform state will be displayed in plain text. For more information, 37 see [Sensitive Data in State](/language/state/sensitive-data). 38 39 ## Examples 40 41 These examples assume the following Terraform output snippet. 42 43 ```hcl 44 output "instance_ips" { 45 value = aws_instance.web.*.public_ip 46 } 47 48 output "lb_address" { 49 value = aws_alb.web.public_dns 50 } 51 52 output "password" { 53 sensitive = true 54 value = var.secret_password 55 } 56 ``` 57 58 To list all outputs: 59 60 ```shellsession 61 $ terraform output 62 instance_ips = [ 63 "54.43.114.12", 64 "52.122.13.4", 65 "52.4.116.53" 66 ] 67 lb_address = "my-app-alb-1657023003.us-east-1.elb.amazonaws.com" 68 password = <sensitive> 69 ``` 70 71 Note that outputs with the `sensitive` attribute will be redacted: 72 73 ```shellsession 74 $ terraform output password 75 password = <sensitive> 76 ``` 77 78 To query for the DNS address of the load balancer: 79 80 ```shellsession 81 $ terraform output lb_address 82 "my-app-alb-1657023003.us-east-1.elb.amazonaws.com" 83 ``` 84 85 To query for all instance IP addresses: 86 87 ```shellsession 88 $ terraform output instance_ips 89 instance_ips = [ 90 "54.43.114.12", 91 "52.122.13.4", 92 "52.4.116.53" 93 ] 94 ``` 95 96 ## Use in automation 97 98 The `terraform output` command by default displays in a human-readable format, 99 which can change over time to improve clarity. 100 101 For scripting and automation, use `-json` to produce the stable JSON format. 102 You can parse the output using a JSON command-line parser such as 103 [jq](https://stedolan.github.io/jq/): 104 105 ```shellsession 106 $ terraform output -json instance_ips | jq -r '.[0]' 107 54.43.114.12 108 ``` 109 110 For the common case of directly using a string value in a shell script, you 111 can use `-raw` instead, which will print the string directly with no extra 112 escaping or whitespace. 113 114 ```shellsession 115 $ terraform output -raw lb_address 116 my-app-alb-1657023003.us-east-1.elb.amazonaws.com 117 ``` 118 119 The `-raw` option works only with values that Terraform can automatically 120 convert to strings. Use `-json` instead, possibly combined with `jq`, to 121 work with complex-typed values such as objects. 122 123 Terraform strings are sequences of Unicode characters rather than raw bytes, 124 so the `-raw` output will be UTF-8 encoded when it contains non-ASCII 125 characters. If you need a different character encoding, use a separate command 126 such as `iconv` to transcode Terraform's raw output.