github.com/hugorut/terraform@v1.1.3/website/docs/language/values/outputs.mdx (about)

     1  ---
     2  page_title: Output Values - Configuration Language
     3  description: Output values are the return values of a Terraform module.
     4  ---
     5  
     6  # Output Values
     7  
     8  Output values make information about your infrastructure available on the
     9  command line, and can expose information for other Terraform configurations to
    10  use. Output values are similar to return values in programming languages.
    11  
    12  > **Hands-on:** Try the [Output Data From
    13  > Terraform](https://learn.hashicorp.com/tutorials/terraform/outputs)
    14  > tutorial on HashiCorp Learn.
    15  
    16  Output values have several 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](/language/state/remote), root module outputs can be
    23    accessed by other configurations via a
    24    [`terraform_remote_state` data source](/language/state/remote-state-data).
    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](/language/syntax/configuration#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](/language/expressions)
    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  <a id="description"></a>
    70  
    71  ### `description` — Output Value Documentation
    72  
    73  Because the output values of a module are part of its user interface, you can
    74  briefly describe the purpose of each value using the optional `description`
    75  argument:
    76  
    77  ```hcl
    78  output "instance_ip_addr" {
    79    value       = aws_instance.server.private_ip
    80    description = "The private IP address of the main server instance."
    81  }
    82  ```
    83  
    84  The description should concisely explain the
    85  purpose of the output and what kind of value is expected. This description
    86  string might be included in documentation about the module, and so it should be
    87  written from the perspective of the user of the module rather than its
    88  maintainer. For commentary for module maintainers, use comments.
    89  
    90  <a id="sensitive"></a>
    91  
    92  ### `sensitive` — Suppressing Values in CLI Output
    93  
    94  An output can be marked as containing sensitive material using the optional
    95  `sensitive` argument:
    96  
    97  ```hcl
    98  output "db_password" {
    99    value       = aws_db_instance.db.password
   100    description = "The password for logging in to the database."
   101    sensitive   = true
   102  }
   103  ```
   104  
   105  Terraform will hide values marked as sensitive in the messages from
   106  `terraform plan` and `terraform apply`. In the following scenario, our root
   107  module has an output declared as sensitive and a module call with a
   108  sensitive output, which we then use in a resource attribute.
   109  
   110  ```hcl
   111  # main.tf
   112  
   113  module "foo" {
   114    source = "./mod"
   115  }
   116  
   117  resource "test_instance" "x" {
   118    some_attribute = module.mod.a # resource attribute references a sensitive output
   119  }
   120  
   121  output "out" {
   122    value     = "xyz"
   123    sensitive = true
   124  }
   125  
   126  # mod/main.tf, our module containing a sensitive output
   127  
   128  output "a" {
   129    value     = "secret"
   130    sensitive = true
   131  }
   132  ```
   133  
   134  When we run a plan or apply, the sensitive value is redacted from output:
   135  
   136  ```
   137  Terraform will perform the following actions:
   138  
   139    # test_instance.x will be created
   140    + resource "test_instance" "x" {
   141        + some_attribute    = (sensitive)
   142      }
   143  
   144  Plan: 1 to add, 0 to change, 0 to destroy.
   145  
   146  Changes to Outputs:
   147    + out = (sensitive value)
   148  ```
   149  
   150  -> **Note:** In Terraform versions prior to Terraform 0.14, setting an output
   151  value in the root module as sensitive would prevent Terraform from showing its
   152  value in the list of outputs at the end of `terraform apply`. However, the
   153  value could still display in the CLI output for other reasons, like if the
   154  value is referenced in an expression for a resource argument.
   155  
   156  Terraform will still record sensitive values in the [state](/language/state),
   157  and so anyone who can access the state data will have access to the sensitive
   158  values in cleartext. For more information, see
   159  [_Sensitive Data in State_](/language/state/sensitive-data).
   160  
   161  <a id="depends_on"></a>
   162  
   163  ### `depends_on` — Explicit Output Dependencies
   164  
   165  Since output values are just a means for passing data out of a module, it is
   166  usually not necessary to worry about their relationships with other nodes in
   167  the dependency graph.
   168  
   169  However, when a parent module accesses an output value exported by one of its
   170  child modules, the dependencies of that output value allow Terraform to
   171  correctly determine the dependencies between resources defined in different
   172  modules.
   173  
   174  Just as with
   175  [resource dependencies](/language/resources/behavior#resource-dependencies),
   176  Terraform analyzes the `value` expression for an output value and automatically
   177  determines a set of dependencies, but in less-common cases there are
   178  dependencies that cannot be recognized implicitly. In these rare cases, the
   179  `depends_on` argument can be used to create additional explicit dependencies:
   180  
   181  ```hcl
   182  output "instance_ip_addr" {
   183    value       = aws_instance.server.private_ip
   184    description = "The private IP address of the main server instance."
   185  
   186    depends_on = [
   187      # Security group rule must be created before this IP address could
   188      # actually be used, otherwise the services will be unreachable.
   189      aws_security_group_rule.local_access,
   190    ]
   191  }
   192  ```
   193  
   194  The `depends_on` argument should be used only as a last resort. When using it,
   195  always include a comment explaining why it is being used, to help future
   196  maintainers understand the purpose of the additional dependency.