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