github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/website/docs/language/values/outputs.html.md (about) 1 --- 2 layout: "language" 3 page_title: "Output Values - Configuration Language" 4 sidebar_current: "docs-config-outputs" 5 description: |- 6 Output values are the return values of a Terraform module. 7 --- 8 9 # Output Values 10 11 Output values make information about your infrastructure available on the 12 command line, and can expose information for other Terraform configurations to 13 use. Output values are similar to return values in programming languages. 14 15 > **Hands-on:** Try the [Output Data From 16 Terraform](https://learn.hashicorp.com/tutorials/terraform/outputs) 17 tutorial on HashiCorp Learn. 18 19 Output values have several uses: 20 21 - A child module can use outputs to expose a subset of its resource attributes 22 to a parent module. 23 - A root module can use outputs to print certain values in the CLI output after 24 running `terraform apply`. 25 - When using [remote state](/docs/language/state/remote.html), root module outputs can be 26 accessed by other configurations via a 27 [`terraform_remote_state` data source](/docs/language/state/remote-state-data.html). 28 29 Resource instances managed by Terraform each export attributes whose values 30 can be used elsewhere in configuration. Output values are a way to expose some 31 of that information to the user of your module. 32 33 -> **Note:** For brevity, output values are often referred to as just "outputs" 34 when the meaning is clear from context. 35 36 ## Declaring an Output Value 37 38 Each output value exported by a module must be declared using an `output` 39 block: 40 41 ```hcl 42 output "instance_ip_addr" { 43 value = aws_instance.server.private_ip 44 } 45 ``` 46 47 The label immediately after the `output` keyword is the name, which must be a 48 valid [identifier](/docs/language/syntax/configuration.html#identifiers). In a root module, this name is 49 displayed to the user; in a child module, it can be used to access the output's 50 value. 51 52 The `value` argument takes an [expression](/docs/language/expressions/index.html) 53 whose result is to be returned to the user. In this example, the expression 54 refers to the `private_ip` attribute exposed by an `aws_instance` resource 55 defined elsewhere in this module (not shown). Any valid expression is allowed 56 as an output value. 57 58 -> **Note:** Outputs are only rendered when Terraform applies your plan. Running 59 `terraform plan` will not render outputs. 60 61 ## Accessing Child Module Outputs 62 63 In a parent module, outputs of child modules are available in expressions as 64 `module.<MODULE NAME>.<OUTPUT NAME>`. For example, if a child module named 65 `web_server` declared an output named `instance_ip_addr`, you could access that 66 value as `module.web_server.instance_ip_addr`. 67 68 ## Optional Arguments 69 70 `output` blocks can optionally include `description`, `sensitive`, and `depends_on` arguments, which are described in the following sections. 71 72 <a id="description"></a> 73 74 ### `description` — Output Value Documentation 75 76 Because the output values of a module are part of its user interface, you can 77 briefly describe the purpose of each value using the optional `description` 78 argument: 79 80 ```hcl 81 output "instance_ip_addr" { 82 value = aws_instance.server.private_ip 83 description = "The private IP address of the main server instance." 84 } 85 ``` 86 87 The description should concisely explain the 88 purpose of the output and what kind of value is expected. This description 89 string might be included in documentation about the module, and so it should be 90 written from the perspective of the user of the module rather than its 91 maintainer. For commentary for module maintainers, use comments. 92 93 <a id="sensitive"></a> 94 95 ### `sensitive` — Suppressing Values in CLI Output 96 97 An output can be marked as containing sensitive material using the optional 98 `sensitive` argument: 99 100 ```hcl 101 output "db_password" { 102 value = aws_db_instance.db.password 103 description = "The password for logging in to the database." 104 sensitive = true 105 } 106 ``` 107 108 Terraform will hide values marked as sensitive in the messages from 109 `terraform plan` and `terraform apply`. In the following scenario, our root 110 module has an output declared as sensitive and a module call with a 111 sensitive output, which we then use in a resource attribute. 112 113 ```hcl 114 # main.tf 115 116 module "foo" { 117 source = "./mod" 118 } 119 120 resource "test_instance" "x" { 121 some_attribute = module.mod.a # resource attribute references a sensitive output 122 } 123 124 output "out" { 125 value = "xyz" 126 sensitive = true 127 } 128 129 # mod/main.tf, our module containing a sensitive output 130 131 output "a" { 132 value = "secret" 133 sensitive = true 134 } 135 ``` 136 137 When we run a plan or apply, the sensitive value is redacted from output: 138 139 ``` 140 Terraform will perform the following actions: 141 142 # test_instance.x will be created 143 + resource "test_instance" "x" { 144 + some_attribute = (sensitive) 145 } 146 147 Plan: 1 to add, 0 to change, 0 to destroy. 148 149 Changes to Outputs: 150 + out = (sensitive value) 151 ``` 152 153 -> **Note:** In Terraform versions prior to Terraform 0.14, setting an output 154 value in the root module as sensitive would prevent Terraform from showing its 155 value in the list of outputs at the end of `terraform apply`. However, the 156 value could still display in the CLI output for other reasons, like if the 157 value is referenced in an expression for a resource argument. 158 159 Terraform will still record sensitive values in the [state](/docs/language/state/index.html), 160 and so anyone who can access the state data will have access to the sensitive 161 values in cleartext. For more information, see 162 [_Sensitive Data in State_](/docs/language/state/sensitive-data.html). 163 164 <a id="depends_on"></a> 165 166 ### `depends_on` — Explicit Output Dependencies 167 168 Since output values are just a means for passing data out of a module, it is 169 usually not necessary to worry about their relationships with other nodes in 170 the dependency graph. 171 172 However, when a parent module accesses an output value exported by one of its 173 child modules, the dependencies of that output value allow Terraform to 174 correctly determine the dependencies between resources defined in different 175 modules. 176 177 Just as with 178 [resource dependencies](/docs/language/resources/behavior.html#resource-dependencies), 179 Terraform analyzes the `value` expression for an output value and automatically 180 determines a set of dependencies, but in less-common cases there are 181 dependencies that cannot be recognized implicitly. In these rare cases, the 182 `depends_on` argument can be used to create additional explicit dependencies: 183 184 ```hcl 185 output "instance_ip_addr" { 186 value = aws_instance.server.private_ip 187 description = "The private IP address of the main server instance." 188 189 depends_on = [ 190 # Security group rule must be created before this IP address could 191 # actually be used, otherwise the services will be unreachable. 192 aws_security_group_rule.local_access, 193 ] 194 } 195 ``` 196 197 The `depends_on` argument should be used only as a last resort. When using it, 198 always include a comment explaining why it is being used, to help future 199 maintainers understand the purpose of the additional dependency.