github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/website/docs/configuration/outputs.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Output Values - Configuration Language"
     4  sidebar_current: "docs-config-outputs"
     5  description: |-
     6    Output values are the return values of a Terraform module.
     7  ---
     8  
     9  # Output Values
    10  
    11  -> **Note:** This page is about Terraform 0.12 and later. For Terraform 0.11 and
    12  earlier, see
    13  [0.11 Configuration Language: Output Values](../configuration-0-11/outputs.html).
    14  
    15  Output values are like the return values of a Terraform module, and have several
    16  uses:
    17  
    18  - A child module can use outputs to expose a subset of its resource attributes
    19    to a parent module.
    20  - A root module can use outputs to print certain values in the CLI output after
    21    running `terraform apply`.
    22  - When using [remote state](/docs/state/remote.html), root module outputs can be
    23    accessed by other configurations via a
    24    [`terraform_remote_state` data source](/docs/providers/terraform/d/remote_state.html).
    25  
    26  Resource instances managed by Terraform each export attributes whose values
    27  can be used elsewhere in configuration. Output values are a way to expose some
    28  of that information to the user of your module.
    29  
    30  -> **Note:** For brevity, output values are often referred to as just "outputs"
    31  when the meaning is clear from context.
    32  
    33  ## Declaring an Output Value
    34  
    35  Each output value exported by a module must be declared using an `output`
    36  block:
    37  
    38  ```hcl
    39  output "instance_ip_addr" {
    40    value = aws_instance.server.private_ip
    41  }
    42  ```
    43  
    44  The label immediately after the `output` keyword is the name, which must be a
    45  valid [identifier](./syntax.html#identifiers). In a root module, this name is
    46  displayed to the user; in a child module, it can be used to access the output's
    47  value.
    48  
    49  The `value` argument takes an [expression](./expressions.html)
    50  whose result is to be returned to the user. In this example, the expression
    51  refers to the `private_ip` attribute exposed by an `aws_instance` resource
    52  defined elsewhere in this module (not shown). Any valid expression is allowed
    53  as an output value.
    54  
    55  -> **Note:** Outputs are only rendered when Terraform applies your plan. Running
    56  `terraform plan` will not render outputs.
    57  
    58  ## Accessing Child Module Outputs
    59  
    60  In a parent module, outputs of child modules are available in expressions as
    61  `module.<MODULE NAME>.<OUTPUT NAME>`. For example, if a child module named
    62  `web_server` declared an output named `instance_ip_addr`, you could access that
    63  value as `module.web_server.instance_ip_addr`.
    64  
    65  ## Optional Arguments
    66  
    67  `output` blocks can optionally include `description`, `sensitive`, and `depends_on` arguments, which are described in the following sections.
    68  
    69  ### `description` — Output Value Documentation
    70  
    71  Because the output values of a module are part of its user interface, you can
    72  briefly describe the purpose of each value using the optional `description`
    73  argument:
    74  
    75  ```hcl
    76  output "instance_ip_addr" {
    77    value       = aws_instance.server.private_ip
    78    description = "The private IP address of the main server instance."
    79  }
    80  ```
    81  
    82  The description should concisely explain the
    83  purpose of the output and what kind of value is expected. This description
    84  string might be included in documentation about the module, and so it should be
    85  written from the perspective of the user of the module rather than its
    86  maintainer. For commentary for module maintainers, use comments.
    87  
    88  ### `sensitive` — Suppressing Values in CLI Output
    89  
    90  An output can be marked as containing sensitive material using the optional
    91  `sensitive` argument:
    92  
    93  ```hcl
    94  output "db_password" {
    95    value       = aws_db_instance.db.password
    96    description = "The password for logging in to the database."
    97    sensitive   = true
    98  }
    99  ```
   100  
   101  Setting an output value in the root module as sensitive prevents Terraform
   102  from showing its value in the list of outputs at the end of `terraform apply`.
   103  It might still be shown in the CLI output for other reasons, like if the
   104  value is referenced in an expression for a resource argument.
   105  
   106  Sensitive output values are still recorded in the
   107  [state](/docs/state/index.html), and so will be visible to anyone who is able
   108  to access the state data. For more information, see
   109  [_Sensitive Data in State_](/docs/state/sensitive-data.html).
   110  
   111  ### `depends_on` — Explicit Output Dependencies
   112  
   113  Since output values are just a means for passing data out of a module, it is
   114  usually not necessary to worry about their relationships with other nodes in
   115  the dependency graph.
   116  
   117  However, when a parent module accesses an output value exported by one of its
   118  child modules, the dependencies of that output value allow Terraform to
   119  correctly determine the dependencies between resources defined in different
   120  modules.
   121  
   122  Just as with
   123  [resource dependencies](./resources.html#resource-dependencies),
   124  Terraform analyzes the `value` expression for an output value and automatically
   125  determines a set of dependencies, but in less-common cases there are
   126  dependencies that cannot be recognized implicitly. In these rare cases, the
   127  `depends_on` argument can be used to create additional explicit dependencies:
   128  
   129  ```hcl
   130  output "instance_ip_addr" {
   131    value       = aws_instance.server.private_ip
   132    description = "The private IP address of the main server instance."
   133  
   134    depends_on = [
   135      # Security group rule must be created before this IP address could
   136      # actually be used, otherwise the services will be unreachable.
   137      aws_security_group_rule.local_access,
   138    ]
   139  }
   140  ```
   141  
   142  The `depends_on` argument should be used only as a last resort. When using it,
   143  always include a comment explaining why it is being used, to help future
   144  maintainers understand the purpose of the additional dependency.