Introduction
When you retrieve a JSON-formatted execution plan from the HCP Terraform API, the output contains the complete details of the plan run. This tutorial guides you on how to use the HCP Terraform API and the jq command-line utility to retrieve only the resource_changes object from the plan.
Expected Outcome
After completing this tutorial, you will be able to isolate and display only the resource changes from a JSON-formatted execution plan.
Procedure
-
Retrieve the plan ID from a specific run.
You can find the
plan-idby querying the run's details and filtering the JSON response for the plan relationship data.$ curl \ --silent \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/vnd.api+json" \ --location \ https://app.terraform.io/api/v2/runs/run-hWpJYg54Rc57V4cw | \ jq '.data.relationships.plan.data.id'
The command returns the plan ID.
"plan-o8NeGBUGRm7Rj3sg"
-
Retrieve the JSON execution plan and filter for resource changes.
Use the plan ID from the previous step to fetch the JSON output for that plan and pipe the result to
jqto extract the.resource_changesobject.$ curl \ --silent \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/vnd.api+json" \ --location \ https://app.terraform.io/api/v2/plans/plan-o8NeGBUGRm7Rj3sg/json-output | \ jq '.resource_changes'
The command returns only the
resource_changesarray from the plan.