Skip to content

Instantly share code, notes, and snippets.

@bruceharrison1984
Last active July 17, 2020 22:04
Show Gist options
  • Save bruceharrison1984/d28416821be864e0f476c21616187d10 to your computer and use it in GitHub Desktop.
Save bruceharrison1984/d28416821be864e0f476c21616187d10 to your computer and use it in GitHub Desktop.
Get outputs from terraform.io
param(
[string]$workspaceName = $(throw "Please specify a workspace name"),
[string]$organizationName = $(throw "Please specify an organization name"),
[string]$terraformIoToken = $(throw "Please specify a terraform Io Token")
)
$ErrorActionPreference = "Stop"
$baseUri = "https://app.terraform.io/"
$authHeader = @{ Authorization = "Bearer $terraformIoToken" }
$contentType = "application/vnd.api+json; charset=utf-8"
Write-Host "Getting state information for $organizationName - $workspaceName"
$workspaceResp = Invoke-RestMethod -Uri "$baseUri/api/v2/organizations/$organizationName/workspaces/$workspaceName" -Headers $authHeader -ContentType $contentType
$currentStateVersionLink = $workspaceResp.data.relationships.'current-state-version'.links.related
Write-Host "Current state link: $currentStateVersionLink"
$uri = $baseUri + $currentStateVersionLink + "?include=outputs"
$outputResp = Invoke-RestMethod -Uri $uri -Headers $authHeader -ContentType $contentType
$outputs = @{};
$outputResp.included | % attributes | % { $outputs."$($_.Name)" = $_.Value }
$outputs = $outputs.GetEnumerator() | sort -Property Name
$outputs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment