github.com/pulumi/terraform@v1.4.0/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.
    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  The following values are available:
   122  
   123  - `path.module` is the filesystem path of the module where the expression is placed.
   124    We do not recommend using `path.module` in write operations because it can produce
   125    different behavior depending on whether you use remote or local module sources.
   126    Multiple invocations of local modules use the same source directory, overwriting
   127    the data in `path.module` during each call. This can lead to race conditions and
   128    unexpected results.
   129  - `path.root` is the filesystem path of the root module of the configuration.
   130  - `path.cwd` is the filesystem path of the original working directory from where you
   131    ran Terraform before applying any `-chdir` argument. This path is an absolute path
   132    that includes details about the filesystem structure. It is also useful in some
   133    advanced cases where Terraform is run from a directory other than the root module
   134    directory. We recommend using `path.root` or `path.module` over `path.cwd` where
   135    possible.
   136  - `terraform.workspace` is the name of the currently selected
   137    [workspace](/language/state/workspaces).
   138  
   139  Use the values in this section carefully, because they include information
   140  about the context in which a configuration is being applied and so may
   141  inadvertently hurt the portability or composability of a module.
   142  
   143  For example, if you use `path.cwd` directly to populate a path into a resource
   144  argument then later applying the same configuration from a different directory
   145  or on a different computer with a different directory structure will cause
   146  the provider to consider the change of path to be a change to be applied, even
   147  if the path still refers to the same file.
   148  
   149  Similarly, if you use any of these values as a form of namespacing in a shared
   150  module, such as using `terraform.workspace` as a prefix for globally-unique
   151  object names, it may not be possible to call your module more than once in
   152  the same configuration.
   153  
   154  Aside from `path.module`, we recommend using the values in this section only
   155  in the root module of your configuration. If you are writing a shared module
   156  which needs a prefix to help create unique names, define an input variable
   157  for your module and allow the calling module to define the prefix. The
   158  calling module can then use `terraform.workspace` to define it if appropriate,
   159  or some other value if not:
   160  
   161  ```hcl
   162  module "example" {
   163    # ...
   164  
   165    name_prefix = "app-${terraform.workspace}"
   166  }
   167  ```
   168  
   169  ### Block-Local Values
   170  
   171  Within the bodies of certain blocks, or in some other specific contexts,
   172  there are other named values available beyond the global values listed above.
   173  These local names are described in the documentation for the specific contexts
   174  where they appear. Some of most common local names are:
   175  
   176  * `count.index`, in resources that use
   177    [the `count` meta-argument](/language/meta-arguments/count).
   178  * `each.key` / `each.value`, in resources that use
   179    [the `for_each` meta-argument](/language/meta-arguments/for_each).
   180  * `self`, in [provisioner](/language/resources/provisioners/syntax) and
   181    [connection](/language/resources/provisioners/connection) blocks.
   182  
   183  -> **Note:** Local names are often referred to as _variables_ or
   184  _temporary variables_ in their documentation. These are not [input
   185  variables](/language/values/variables); they are just arbitrary names
   186  that temporarily represent a value.
   187  
   188  The names in this section relate to top-level configuration blocks only.
   189  If you use [`dynamic` blocks](/language/expressions/dynamic-blocks) to dynamically generate
   190  resource-type-specific _nested_ blocks within `resource` and `data` blocks then
   191  you'll refer to the key and value of each element differently. See the
   192  `dynamic` blocks documentation for details.
   193  
   194  ## Named Values and Dependencies
   195  
   196  Constructs like resources and module calls often use references to named values
   197  in their block bodies, and Terraform analyzes these expressions to automatically
   198  infer dependencies between objects. For example, an expression in a resource
   199  argument that refers to another managed resource creates an implicit dependency
   200  between the two resources.
   201  
   202  ## References to Resource Attributes
   203  
   204  The most common reference type is a reference to an attribute of a resource
   205  which has been declared either with a `resource` or `data` block. Because
   206  the contents of such blocks can be quite complicated themselves, expressions
   207  referring to these contents can also be complicated.
   208  
   209  Consider the following example resource block:
   210  
   211  ```hcl
   212  resource "aws_instance" "example" {
   213    ami           = "ami-abc123"
   214    instance_type = "t2.micro"
   215  
   216    ebs_block_device {
   217      device_name = "sda2"
   218      volume_size = 16
   219    }
   220    ebs_block_device {
   221      device_name = "sda3"
   222      volume_size = 20
   223    }
   224  }
   225  ```
   226  
   227  The documentation for [`aws_instance`](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance)
   228  lists all of the arguments and nested blocks supported for this resource type,
   229  and also lists a number of attributes that are _exported_ by this resource
   230  type. All of these different resource type schema constructs are available
   231  for use in references, as follows:
   232  
   233  * The `ami` argument set in the configuration can be used elsewhere with
   234    the reference expression `aws_instance.example.ami`.
   235  * The `id` attribute exported by this resource type can be read using the
   236    same syntax, giving `aws_instance.example.id`.
   237  * The arguments of the `ebs_block_device` nested blocks can be accessed using
   238    a [splat expression](/language/expressions/splat). For example, to obtain a list of
   239    all of the `device_name` values, use
   240    `aws_instance.example.ebs_block_device[*].device_name`.
   241  * The nested blocks in this particular resource type do not have any exported
   242    attributes, but if `ebs_block_device` were to have a documented `id`
   243    attribute then a list of them could be accessed similarly as
   244    `aws_instance.example.ebs_block_device[*].id`.
   245  * Sometimes nested blocks are defined as taking a logical key to identify each
   246    block, which serves a similar purpose as the resource's own name by providing
   247    a convenient way to refer to that single block in expressions. If `aws_instance`
   248    had a hypothetical nested block type `device` that accepted such a key, it
   249    would look like this in configuration:
   250  
   251    ```hcl
   252      device "foo" {
   253        size = 2
   254      }
   255      device "bar" {
   256        size = 4
   257      }
   258    ```
   259  
   260    Arguments inside blocks with _keys_ can be accessed using index syntax, such
   261    as `aws_instance.example.device["foo"].size`.
   262  
   263    To obtain a map of values of a particular argument for _labelled_ nested
   264    block types, use a [`for` expression](/language/expressions/for):
   265    `{for k, device in aws_instance.example.device : k => device.size}`.
   266  
   267  When a resource has the
   268  [`count`](/language/meta-arguments/count)
   269  argument set, the resource itself becomes a _list_ of instance objects rather than
   270  a single object. In that case, access the attributes of the instances using
   271  either [splat expressions](/language/expressions/splat) or index syntax:
   272  
   273  * `aws_instance.example[*].id` returns a list of all of the ids of each of the
   274    instances.
   275  * `aws_instance.example[0].id` returns just the id of the first instance.
   276  
   277  When a resource has the
   278  [`for_each`](/language/meta-arguments/for_each)
   279  argument set, the resource itself becomes a _map_ of instance objects rather than
   280  a single object, and attributes of instances must be specified by key, or can
   281  be accessed using a [`for` expression](/language/expressions/for).
   282  
   283  * `aws_instance.example["a"].id` returns the id of the "a"-keyed resource.
   284  * `[for value in aws_instance.example: value.id]` returns a list of all of the ids
   285    of each of the instances.
   286  
   287  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:
   288  
   289  * `values(aws_instance.example)[*].id`
   290  
   291  ### Sensitive Resource Attributes
   292  
   293  When defining the schema for a resource type, a provider developer can mark
   294  certain attributes as _sensitive_, in which case Terraform will show a
   295  placeholder marker `(sensitive value)` instead of the actual value when rendering
   296  a plan involving that attribute.
   297  
   298  A provider attribute marked as sensitive behaves similarly to an
   299  [an input variable declared as sensitive](/language/values/variables#suppressing-values-in-cli-output),
   300  where Terraform will hide the value in the plan and apply messages and will
   301  also hide any other values you derive from it as sensitive.
   302  However, there are some limitations to that behavior as described in
   303  [Cases where Terraform may disclose a sensitive variable](/language/values/variables#cases-where-terraform-may-disclose-a-sensitive-variable).
   304  
   305  If you use a sensitive value from a resource attribute as part of an
   306  [output value](/language/values/outputs) then Terraform will require
   307  you to also mark the output value itself as sensitive, to confirm that you
   308  intended to export it.
   309  
   310  Terraform will still record sensitive values in the [state](/language/state),
   311  and so anyone who can access the state data will have access to the sensitive
   312  values in cleartext. For more information, see
   313  [_Sensitive Data in State_](/language/state/sensitive-data).
   314  
   315  -> **Note:** Treating values derived from a sensitive resource attribute as
   316  sensitive themselves was introduced in Terraform v0.15. Earlier versions of
   317  Terraform will obscure the direct value of a sensitive resource attribute,
   318  but will _not_ automatically obscure other values derived from sensitive
   319  resource attributes.
   320  
   321  ### Values Not Yet Known
   322  
   323  When Terraform is planning a set of changes that will apply your configuration,
   324  some resource attribute values cannot be populated immediately because their
   325  values are decided dynamically by the remote system. For example, if a
   326  particular remote object type is assigned a generated unique id on creation,
   327  Terraform cannot predict the value of this id until the object has been created.
   328  
   329  Terraform uses special unknown value placeholders for information that it
   330  cannot predict during the plan phase. The Terraform language automatically
   331  handles unknown values in expressions. For example, adding a known value to an
   332  unknown value automatically produces an unknown value as a result.
   333  
   334  However, there are some situations where unknown values _do_ have a significant
   335  effect:
   336  
   337  * The `count` meta-argument for resources cannot be unknown, since it must
   338    be evaluated during the plan phase to determine how many instances are to
   339    be created.
   340  
   341  * If unknown values are used in the configuration of a data resource, that
   342    data resource cannot be read during the plan phase and so it will be deferred
   343    until the apply phase. In this case, the results of the data resource will
   344    _also_ be unknown values.
   345  
   346  * If an unknown value is assigned to an argument inside a `module` block,
   347    any references to the corresponding input variable within the child module
   348    will use that unknown value.
   349  
   350  * If an unknown value is used in the `value` argument of an output value,
   351    any references to that output value in the parent module will use that
   352    unknown value.
   353  
   354  * Terraform will attempt to validate that unknown values are of suitable
   355    types where possible, but incorrect use of such values may not be detected
   356    until the apply phase, causing the apply to fail.
   357  
   358  Unknown values appear in the `terraform plan` output as `(known after apply)`.