github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/website/docs/language/expressions/references.html.md (about)

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