Introduction
Outputs are specific values from a Terraform run that are highlighted as a separate part of Terraform run’s log. To extract only the outputs from the log of a Terraform run performed in Terraform Cloud (“TFC”), there are two options:
- Set up the
remote
backend and use a local executable of Terraform CLI to retrieve the outputs - Use the API to fetch the run log and isolate the outputs
Procedure
1. Using Terraform locally
This option requires setting up the remote
backend, as described in the remote backend documentation.
Once the backend is set up, the latest run’s outputs may be retrieved by running the following command.$ terraform output
One advantage of this method is the ability to supply outputs as JSON. To render outputs as JSON, pass the -json
flag to terraform output
. (More details on the command are available in the Terraform CLI Command: output documentation.)
2. Using the API
If using Terraform locally is not a suitable solution, the outputs from the run’s log can alternatively be retrieved using the API.
In the following API calls, $TOKEN
represents the user’s Terraform Cloud or Terraform Enterprise API token. Additional variables are defined with shell environment variable syntax ($VAR_NAME
). For Terraform Enterprise users, app.terraform.io
should be replaced with your Terraform Enterprise instance’s hostname.
The process for retrieving output values via the API occurs in two stages; retrieving the output ID (IDs start with wsout-
) followed by retrieving the value of the output ID. Output IDs can be retrieved using two distinct methods.
Retrieving Output IDs From the Most Recent State Version
To retrieve the outputs from the most recent state version within a workspace, the Fetch the Current State Version for a Workspace API endpoint may be used. In order to include the outputs in the response, the outputs
include query parameter must be passed.
To use the API endpoint, first retrieve the workspace ID by navigating to the workspace in the Terraform Cloud UI and selecting Settings > General. The workspace ID is listed at the top of the page, and follows the format of ws-xxxxxxxxxxxxxxxx
. Once you have the workspace ID, the API call should be formatted as follows.$ curl \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/vnd.api+json" \
"https://app.terraform.io/api/v2/workspaces/$WORKSPACE_ID/current-state-version?include=outputs"
Each of the workspace outputs will be included as an object within the .included
object of the response.
Retrieving Output IDs From a Specific State Version
To retrieve outputs from older runs on the workspace, a slightly different approach must be taken. Rather than collecting the workspace version as detailed above, navigate to the workspace and select the States tab. Within the States tab, all existing states for the workspace will be listed. Select the state version you would like to retrieve the outputs from and copy the state version ID, which follows the format of sv-xxxxxxxxxxxxxxxx
. Alternatively, the List State Versions for a Workspace API endpoint may be used.
Once you have noted the state version that you would like the outputs from, the Show a State Version API endpoint may be used with the outputs
include query parameter to retrieve the outputs for the specified state version. The API call should be formatted as follows.$ curl \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/vnd.api+json" \
"https://app.terraform.io/api/v2/state-versions/$STATE_VERSION?include=outputs"
Each of the workspace outputs will be included as an object within the .included
object of the response.
Retrieving the Values of Outputs
Once the ouput IDs have been retrieved using one of the methods described above, the value of the output may be retrieved using the Show a State Version Output API endpoint. For each of the desired outputs, the API call should be formatted as follows.$ curl \
--header "Authorization: Bearer $TOKEN" \
https://app.terraform.io/api/v2/state-version-outputs/$OUTPUT_ID
The data.attributes
object of the response contains several values representing the output including the name, type, value, and whether or not the output has been marked as sensitive.
Additional Information
If you experience issues using the methods described in this article, please contact HashiCorp Support to request for further assistance.