github.com/hugorut/terraform@v1.1.3/website/docs/language/expressions/references.mdx (about) 1 --- 2 page_title: References to Values - Configuration Language 3 description: >- 4 Reference values in configurations, including resources, input variables, 5 local and block-local values, module outputs, data sources, and workspace 6 data. 7 --- 8 9 # References to Named Values 10 11 > **Hands-on:** Try the [Create Dynamic Expressions](https://learn.hashicorp.com/tutorials/terraform/expressions?in=terraform/configuration-language&utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS) tutorial on HashiCorp Learn. 12 13 Terraform makes several kinds of named values available. Each of these names is 14 an expression that references the associated value; you can use them as 15 standalone expressions, or combine them with other expressions to compute new 16 values. 17 18 ## Types of Named Values 19 20 The main kinds of named values available in Terraform are: 21 22 * Resources 23 * Input variables 24 * Local values 25 * Child module outputs 26 * Data sources 27 * Filesystem and workspace info 28 * Block-local values 29 30 The sections below explain each kind of named value in detail. 31 32 Although many of these names use dot-separated paths that resemble 33 [attribute notation](/language/expressions/types#indices-and-attributes) for elements of object values, they are not 34 implemented as real objects. This means you must use them exactly as written: 35 you cannot use square-bracket notation to replace the dot-separated paths, and 36 you cannot iterate over the "parent object" of a named entity; for example, you 37 cannot use `aws_instance` in a `for` expression to iterate over every AWS 38 instance resource. 39 40 ### Resources 41 42 `<RESOURCE TYPE>.<NAME>` represents a [managed resource](/language/resources) of 43 the given type and name. 44 45 The value of a resource reference can vary, depending on whether the resource 46 uses `count` or `for_each`: 47 48 * If the resource doesn't use `count` or `for_each`, the reference's value is an 49 object. The resource's attributes are elements of the object, and you can 50 access them using [dot or square bracket notation](/language/expressions/types#indices-and-attributes). 51 * If the resource has the `count` argument set, the reference's value is a 52 _list_ of objects representing its instances. 53 * If the resource has the `for_each` argument set, the reference's value is a 54 _map_ of objects representing its instances. 55 56 Any named value that does not match another pattern listed below 57 will be interpreted by Terraform as a reference to a managed resource. 58 59 For more information about how to use resource references, see 60 [references to resource attributes](#references-to-resource-attributes) below. 61 62 ### Input Variables 63 64 `var.<NAME>` is the value of the [input variable](/language/values/variables) of the given name. 65 66 If the variable has a type constraint (`type` argument) as part of its 67 declaration, Terraform will automatically convert the caller's given value 68 to conform to the type constraint. 69 70 For that reason, you can safely assume that a reference using `var.` will 71 always produce a value that conforms to the type constraint, even if the caller 72 provided a value of a different type that was automatically converted. 73 74 In particular, note that if you define a variable as being of an object type 75 with particular attributes then only _those specific attributes_ will be 76 available in expressions elsewhere in the module, even if the caller actually 77 passed in a value with additional attributes. You must define in the type 78 constraint all of the attributes you intend to use elsewhere in your module. 79 80 ### Local Values 81 82 `local.<NAME>` is the value of the [local value](/language/values/locals) of the given name. 83 84 Local values can refer to other local values, even within the same `locals` 85 block, as long as you don't introduce circular dependencies. 86 87 ### Child Module Outputs 88 89 `module.<MODULE NAME>` is an value representing the results of 90 [a `module` block](/language/modules/syntax). 91 92 If the corresponding `module` block does not have either `count` nor `for_each` 93 set then the value will be an object with one attribute for each output value 94 defined in the child module. To access one of the module's 95 [output values](/language/values/outputs), use `module.<MODULE NAME>.<OUTPUT NAME>`. 96 97 If the corresponding `module` uses `for_each` then the value will be a map 98 of objects whose keys correspond with the keys in the `for_each` expression, 99 and whose values are each objects with one attribute for each output value 100 defined in the child module, each representing one module instance. 101 102 If the corresponding module uses `count` then the result is similar to for 103 `for_each` except that the value is a _list_ with the requested number of 104 elements, each one representing one module instance. 105 106 ### Data Sources 107 108 `data.<DATA TYPE>.<NAME>` is an object representing a 109 [data resource](/language/data-sources) of the given data 110 source type and name. If the resource has the `count` argument set, the value 111 is a list of objects representing its instances. If the resource has the `for_each` 112 argument set, the value is a map of objects representing its instances. 113 114 For more information, see 115 [References to Resource Attributes](#references-to-resource-attributes), which 116 also applies to data resources aside from the addition of the `data.` prefix 117 to mark the reference as for a data resource. 118 119 ### Filesystem and Workspace Info 120 121 * `path.module` is the filesystem path of the module where the expression 122 is placed. 123 * `path.root` is the filesystem path of the root module of the configuration. 124 * `path.cwd` is the filesystem path of the current working directory. In 125 normal use of Terraform this is the same as `path.root`, but some advanced 126 uses of Terraform run it from a directory other than the root module 127 directory, causing these paths to be different. 128 * `terraform.workspace` is the name of the currently selected 129 [workspace](/language/state/workspaces). 130 131 Use the values in this section carefully, because they include information 132 about the context in which a configuration is being applied and so may 133 inadvertently hurt the portability or composability of a module. 134 135 For example, if you use `path.cwd` directly to populate a path into a resource 136 argument then later applying the same configuration from a different directory 137 or on a different computer with a different directory structure will cause 138 the provider to consider the change of path to be a change to be applied, even 139 if the path still refers to the same file. 140 141 Similarly, if you use any of these values as a form of namespacing in a shared 142 module, such as using `terraform.workspace` as a prefix for globally-unique 143 object names, it may not be possible to call your module more than once in 144 the same configuration. 145 146 Aside from `path.module`, we recommend using the values in this section only 147 in the root module of your configuration. If you are writing a shared module 148 which needs a prefix to help create unique names, define an input variable 149 for your module and allow the calling module to define the prefix. The 150 calling module can then use `terraform.workspace` to define it if appropriate, 151 or some other value if not: 152 153 ```hcl 154 module "example" { 155 # ... 156 157 name_prefix = "app-${terraform.workspace}" 158 } 159 ``` 160 161 ### Block-Local Values 162 163 Within the bodies of certain blocks, or in some other specific contexts, 164 there are other named values available beyond the global values listed above. 165 These local names are described in the documentation for the specific contexts 166 where they appear. Some of most common local names are: 167 168 * `count.index`, in resources that use 169 [the `count` meta-argument](/language/meta-arguments/count). 170 * `each.key` / `each.value`, in resources that use 171 [the `for_each` meta-argument](/language/meta-arguments/for_each). 172 * `self`, in [provisioner](/language/resources/provisioners/syntax) and 173 [connection](/language/resources/provisioners/connection) blocks. 174 175 -> **Note:** Local names are often referred to as _variables_ or 176 _temporary variables_ in their documentation. These are not [input 177 variables](/language/values/variables); they are just arbitrary names 178 that temporarily represent a value. 179 180 The names in this section relate to top-level configuration blocks only. 181 If you use [`dynamic` blocks](/language/expressions/dynamic-blocks) to dynamically generate 182 resource-type-specific _nested_ blocks within `resource` and `data` blocks then 183 you'll refer to the key and value of each element differently. See the 184 `dynamic` blocks documentation for details. 185 186 ## Named Values and Dependencies 187 188 Constructs like resources and module calls often use references to named values 189 in their block bodies, and Terraform analyzes these expressions to automatically 190 infer dependencies between objects. For example, an expression in a resource 191 argument that refers to another managed resource creates an implicit dependency 192 between the two resources. 193 194 ## References to Resource Attributes 195 196 The most common reference type is a reference to an attribute of a resource 197 which has been declared either with a `resource` or `data` block. Because 198 the contents of such blocks can be quite complicated themselves, expressions 199 referring to these contents can also be complicated. 200 201 Consider the following example resource block: 202 203 ```hcl 204 resource "aws_instance" "example" { 205 ami = "ami-abc123" 206 instance_type = "t2.micro" 207 208 ebs_block_device { 209 device_name = "sda2" 210 volume_size = 16 211 } 212 ebs_block_device { 213 device_name = "sda3" 214 volume_size = 20 215 } 216 } 217 ``` 218 219 The documentation for [`aws_instance`](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance) 220 lists all of the arguments and nested blocks supported for this resource type, 221 and also lists a number of attributes that are _exported_ by this resource 222 type. All of these different resource type schema constructs are available 223 for use in references, as follows: 224 225 * The `ami` argument set in the configuration can be used elsewhere with 226 the reference expression `aws_instance.example.ami`. 227 * The `id` attribute exported by this resource type can be read using the 228 same syntax, giving `aws_instance.example.id`. 229 * The arguments of the `ebs_block_device` nested blocks can be accessed using 230 a [splat expression](/language/expressions/splat). For example, to obtain a list of 231 all of the `device_name` values, use 232 `aws_instance.example.ebs_block_device[*].device_name`. 233 * The nested blocks in this particular resource type do not have any exported 234 attributes, but if `ebs_block_device` were to have a documented `id` 235 attribute then a list of them could be accessed similarly as 236 `aws_instance.example.ebs_block_device[*].id`. 237 * Sometimes nested blocks are defined as taking a logical key to identify each 238 block, which serves a similar purpose as the resource's own name by providing 239 a convenient way to refer to that single block in expressions. If `aws_instance` 240 had a hypothetical nested block type `device` that accepted such a key, it 241 would look like this in configuration: 242 243 ```hcl 244 device "foo" { 245 size = 2 246 } 247 device "bar" { 248 size = 4 249 } 250 ``` 251 252 Arguments inside blocks with _keys_ can be accessed using index syntax, such 253 as `aws_instance.example.device["foo"].size`. 254 255 To obtain a map of values of a particular argument for _labelled_ nested 256 block types, use a [`for` expression](/language/expressions/for): 257 `{for k, device in aws_instance.example.device : k => device.size}`. 258 259 When a resource has the 260 [`count`](/language/meta-arguments/count) 261 argument set, the resource itself becomes a _list_ of instance objects rather than 262 a single object. In that case, access the attributes of the instances using 263 either [splat expressions](/language/expressions/splat) or index syntax: 264 265 * `aws_instance.example[*].id` returns a list of all of the ids of each of the 266 instances. 267 * `aws_instance.example[0].id` returns just the id of the first instance. 268 269 When a resource has the 270 [`for_each`](/language/meta-arguments/for_each) 271 argument set, the resource itself becomes a _map_ of instance objects rather than 272 a single object, and attributes of instances must be specified by key, or can 273 be accessed using a [`for` expression](/language/expressions/for). 274 275 * `aws_instance.example["a"].id` returns the id of the "a"-keyed resource. 276 * `[for value in aws_instance.example: value.id]` returns a list of all of the ids 277 of each of the instances. 278 279 Note that unlike `count`, splat expressions are _not_ directly applicable to resources managed with `for_each`, as splat expressions must act on a list value. However, you can use the `values()` function to extract the instances as a list and use that list value in a splat expression: 280 281 * `values(aws_instance.example)[*].id` 282 283 ### Sensitive Resource Attributes 284 285 When defining the schema for a resource type, a provider developer can mark 286 certain attributes as _sensitive_, in which case Terraform will show a 287 placeholder marker `(sensitive)` instead of the actual value when rendering 288 a plan involving that attribute. 289 290 A provider attribute marked as sensitive behaves similarly to an 291 [an input variable declared as sensitive](/language/values/variables#suppressing-values-in-cli-output), 292 where Terraform will hide the value in the plan and apply messages and will 293 also hide any other values you derive from it as sensitive. 294 However, there are some limitations to that behavior as described in 295 [Cases where Terraform may disclose a sensitive variable](/language/values/variables#cases-where-terraform-may-disclose-a-sensitive-variable). 296 297 If you use a sensitive value from a resource attribute as part of an 298 [output value](/language/values/outputs) then Terraform will require 299 you to also mark the output value itself as sensitive, to confirm that you 300 intended to export it. 301 302 Terraform will still record sensitive values in the [state](/language/state), 303 and so anyone who can access the state data will have access to the sensitive 304 values in cleartext. For more information, see 305 [_Sensitive Data in State_](/language/state/sensitive-data). 306 307 -> **Note:** Treating values derived from a sensitive resource attribute as 308 sensitive themselves was introduced in Terraform v0.15. Earlier versions of 309 Terraform will obscure the direct value of a sensitive resource attribute, 310 but will _not_ automatically obscure other values derived from sensitive 311 resource attributes. 312 313 ### Values Not Yet Known 314 315 When Terraform is planning a set of changes that will apply your configuration, 316 some resource attribute values cannot be populated immediately because their 317 values are decided dynamically by the remote system. For example, if a 318 particular remote object type is assigned a generated unique id on creation, 319 Terraform cannot predict the value of this id until the object has been created. 320 321 To allow expressions to still be evaluated during the plan phase, Terraform 322 uses special "unknown value" placeholders for these results. In most cases you 323 don't need to do anything special to deal with these, since the Terraform 324 language automatically handles unknown values during expressions, so that 325 for example adding a known value to an unknown value automatically produces 326 an unknown value as the result. 327 328 However, there are some situations where unknown values _do_ have a significant 329 effect: 330 331 * The `count` meta-argument for resources cannot be unknown, since it must 332 be evaluated during the plan phase to determine how many instances are to 333 be created. 334 335 * If unknown values are used in the configuration of a data resource, that 336 data resource cannot be read during the plan phase and so it will be deferred 337 until the apply phase. In this case, the results of the data resource will 338 _also_ be unknown values. 339 340 * If an unknown value is assigned to an argument inside a `module` block, 341 any references to the corresponding input variable within the child module 342 will use that unknown value. 343 344 * If an unknown value is used in the `value` argument of an output value, 345 any references to that output value in the parent module will use that 346 unknown value. 347 348 * Terraform will attempt to validate that unknown values are of suitable 349 types where possible, but incorrect use of such values may not be detected 350 until the apply phase, causing the apply to fail. 351 352 Unknown values appear in the `terraform plan` output as `(not yet known)`.