Skip to content

Instantly share code, notes, and snippets.

@ksatirli
Created November 11, 2021 10:00
Show Gist options
  • Save ksatirli/e8bd6a95cfb708f82b811b6ba84aa714 to your computer and use it in GitHub Desktop.
Save ksatirli/e8bd6a95cfb708f82b811b6ba84aa714 to your computer and use it in GitHub Desktop.
Get Terraform Cloud Output Values

Get Terraform Cloud Output Values

This code is based around a question posted on Twitter

Usage

  • Log into Terraform Cloud using terraform login.
  • Apply the three outputs in main.tf using terraform apply.
  • Execute get-terraform-cloud-output-values.sh to retrieve the output values through the TFC API.

Expected Output

The expected API outputs should look like the three green strings:

Expected API Output

#!/bin/sh
# This script sends a request to a TFE API and retrieves outputs.
# TFE base URL; "app.terraform.io" is Terraform Cloud (prod)
TFE_BASE="app.terraform.io"
# TFE API Token, see https://www.terraform.io/docs/cloud/users-teams-organizations/api-tokens.html
# For this to work, you will need to be logged in through `terraform login` and have `jq` installed
TFE_TOKEN=$(jq -r ".credentials[\"${TFE_BASE}\"].token" < "${HOME}/.terraform.d/credentials.tfrc.json")
# TFE Organization name, # see https://www.terraform.io/docs/cloud/users-teams-organizations/organizations.html
TFE_ORG="ksatirli"
# TFE Workspace name, see https://www.terraform.io/docs/cloud/workspaces/naming.html
TFE_WORKSPACE="tfc-api-outputs"
# Base Request URL, for convenience:
TFE_REQUEST_URL="https://${TFE_BASE}/api/v2/organizations/${TFE_ORG}/workspaces/${TFE_WORKSPACE}"
# Request Parameters; may include run stats, README etc.
TFE_REQUEST_PARAMS="include=outputs"
echo "Configuration:"
echo " Organization: ${TFE_ORG}"
echo " Workspace: ${TFE_WORKSPACE}"
# printing the token is a bad practice
#echo " Token: ${TFE_TOKEN}"
echo
echo " Request URL: ${TFE_REQUEST_URL}"
echo " Request Params: ${TFE_REQUEST_PARAMS}"
echo
# make a GET request to TFE API and then parse the output with `jq
curl \
--show-error \
--silent \
--request "GET" \
--header "Authorization: Bearer ${TFE_TOKEN}" \
--header "Content-Type: application/vnd.api+json" \
"${TFE_REQUEST_URL}?${TFE_REQUEST_PARAMS}" \
| \
jq '.included[].attributes.value'
# see https://www.terraform.io/docs/language/settings/index.html#terraform-block-syntax
terraform {
backend "remote" {
# see https://www.terraform.io/docs/cloud/users-teams-organizations/organizations.html
organization = "ksatirli"
# see https://www.terraform.io/docs/cloud/workspaces/index.html
workspaces {
name = "tfc-api-outputs"
}
}
# see https://www.terraform.io/docs/language/settings/index.html#specifying-a-required-terraform-version
required_version = "1.0.11"
}
# see https://www.terraform.io/docs/language/values/outputs.html
output "tfc_api_output_test_1" {
description = "TFC API Output Test #1"
value = "JIRkAvX8kbmf"
}
output "tfc_api_output_test_2" {
description = "TFC API Output Test #2"
value = "ji-rk-av-x8kbmf"
}
output "tfc_api_output_test_3" {
description = "TFC API Output Test #3"
value = "✨ ji-rk-av-x8kbmf"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment