Output Values
Output values are Terraform's equivalent of a return value in a programming language: after resources are applied, they expose selected attributes back to the person - or the parent module - using the configuration. Most server details (an IP address, a resource id, a self-link URI) are calculated at deployment and can only be known after creation, so an output value is how you surface them.
Declare an output with an output block; the label after the keyword is the output's
name. You can put it anywhere in a configuration, but the convention is a dedicated
outputs.tf file.
The output block and its arguments
| Argument | Required? | What it does |
|---|---|---|
value | Required | The value returned to the user of the module - usually a computed resource attribute. |
description | Optional | Explains the purpose of the output and the value expected; used for documentation. |
sensitive | Optional | Masks the value so a confidential attribute (e.g. a password) is not printed by accident. |
Here an output named picture_URL returns the uploaded object's self_link. After
apply, the URL prints in the CLI under Outputs::
For a value that Terraform computes at deployment (an id, a self-link, an IP), read it from an output, not from a user-supplied input variable. The user can't know a computed value ahead of time, and hard-coding it defeats the point.
Query outputs with terraform output
Running terraform output prints every output value defined in the project - handy for
piping a value into another command or confirming what a module exposes.
Best practices
- Output only useful, computed information. Don't regurgitate variables or restate
known inputs - for a network, useful computed attributes are
id,gateway_ipv4(the default-route gateway address), andself_link(the URI of the created resource). - Name and describe meaningfully, exactly as you would for input variables.
- Organize outputs in a file named
outputs.tf. - Mark sensitive outputs with
sensitiverather than trying to hand-encrypt them.
Output a computed attribute like id, not a value you already supplied as input - id is
known only after apply, whereas name just echoes an input:
Mark sensitive outputs
Set sensitive = true on any output that carries confidential data, such as a database
password. Terraform then relies on its built-in sensitive-state support instead of you
manually encrypting the value.
When an output (or attribute) is marked sensitive, its value is replaced with (sensitive)
in the output of terraform plan and terraform apply, so it never lands in logs or a
terminal by accident.