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