Output data from Terraform
Terraform output values let you export structured data about your resources. You can use this data to configure other parts of your infrastructure with automation tools, or as a data source for another Terraform workspace. Outputs are also how you expose data from a child module to a root module.
In this tutorial, you will use Terraform to deploy application infrastructure
on AWS and use outputs to get information about the resources. Then, you will
use the sensitive
flag to reduce the risk of inadvertently disclosing the
database administrator username and password. You will also learn how to format outputs into machine-readable JSON.
Prerequisites
You can complete this tutorial using the same workflow with either Terraform Community Edition or HCP Terraform. HCP Terraform is a platform that you can use to manage and execute your Terraform projects. It includes features like remote state and execution, structured plan output, workspace resource summaries, and more.
Select the HCP Terraform tab to complete this tutorial using HCP Terraform.
This tutorial assumes that you are familiar with the Terraform workflow. If you are new to Terraform, complete the Get Started collection first.
In order to complete this tutorial, you will need the following:
- Terraform v1.2+ installed locally.
- An AWS account with local credentials configured for use with Terraform.
Note
Some of the infrastructure in this tutorial may not qualify for the AWS free tier. Destroy the infrastructure at the end of the tutorial to avoid unnecessary charges. We are not responsible for any charges that you incur.
Create infrastructure
Clone the example repository for this tutorial, which contains Terraform configuration for a web application including a VPC, load balancer, EC2 instances, and a database.
Change to the repository directory.
Initialize this configuration.
Now apply the configuration. Respond yes
to the prompt to confirm the operation.
Output VPC and load balancer information
You can add output declarations anywhere in your Terraform configuration files.
However, we recommend defining them in a separate file called outputs.tf
to
make it easier for users to understand your configuration and review its expected outputs.
Add a block to outputs.tf
to show the ID of the VPC.
While the description
argument is optional, you should include it in all
output declarations to document the intent and content of the output.
You can use the result of any Terraform
expression
as the value of an output. Add the following definitions to outputs.tf
.
The lb_url
output uses string
interpolation
to create a URL from the load balancer's domain name. The web_server_count
output uses the length()
function to
calculate the number of instances attached to the load balancer.
Terraform stores output values in the configuration's state file. In order to see these outputs,
you need to update the state by applying this new configuration, even though the
infrastructure will not change. Respond to the confirmation prompt with a yes
.
Query outputs
After creating the outputs, use the
terraform output
command to query all of them.
Next, query an individual output by name.
Starting with version 0.14, Terraform wraps string outputs in quotes by
default. You can use the -raw
flag when querying a specified output for
machine-readable format.
Use the lb_url
output value with the -raw
flag to cURL the load balancer
and verify the response.
If you are using HCP Terraform, you can also find a table of your configuration's outputs on your workspace's overview page.
Redact sensitive outputs
You can designate Terraform outputs as sensitive. Terraform will redact the values of sensitive outputs to avoid accidentally printing them out to the console. Use sensitive outputs to share sensitive data from your configuration with other Terraform modules, automation tools, or HCP Terraform workspaces.
Terraform will redact sensitive outputs when planning, applying, or destroying your configuration, or when you query all of your outputs. Terraform will not redact sensitive outputs in other cases, such as when you query a specific output by name, query all of your outputs in JSON format, or when you use outputs from a child module in your root module.
Add the following output blocks to your outputs.tf
file. Note that the sensitive
attribute is set to true
.
Apply this change to add these outputs to your state file, and respond to the
confirmation prompt with yes
.
Notice that Terraform redacts the values of the outputs marked as sensitive.
Use terraform output
to query the database password by name, and notice that
Terraform will not redact the value when you specify the output by name.
Terraform stores all output values, including those marked as sensitive, as plain text in your state file.
Use the grep
command to see the values of the sensitive
outputs in your state file.
The sensitive
argument for outputs can help avoid inadvertent exposure of
those values. However, you must still keep your Terraform state secure to avoid
exposing these values.
Generate machine-readable output
The Terraform CLI output is designed to be parsed by humans. To get
machine-readable format for automation, use the -json
flag for JSON-formatted
output.
Terraform does not redact sensitive output values with the -json
option,
because it assumes that an automation tool will use the output.
Clean up your infrastructure
Before moving on, destroy the infrastructure you created in this tutorial to
avoid incurring unnecessary costs. Be sure to respond to the confirmation
prompt with yes
.
If you used HCP Terraform for this tutorial, after destroying your resources, delete the learn-terraform-outputs
workspace from your HCP Terraform organization.
Next steps
In this tutorial you used Terraform outputs to query data about your infrastructure. Terraform outputs let you share data between Terraform configurations, and with other tools and automation. Outputs are also the only way to share data from a child module to your configuration's root module.
Now that you know how to use Terraform outputs, check out the following resources for more information.
- Read the Terraform Outputs documentation.
- Manage sensitive data in state.
- Use and create Terraform modules.
- Connect HCP Terraform Workspaces with run triggers, and use outputs from one workspace to configure another workspace.