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