Use the HCP Terraform API to programmatically trigger a speculative plan, poll for its completion, and parse the structured plan output to enforce a cost policy gate in CI

domain: Terraform / HCP Terraform · 6 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. Compress the Terraform configuration directory into a tar.gz file and upload it to HCP Terraform as a configuration version using the POST /configuration-versions API endpoint with the auto-queue-runs: false flag
  2. Trigger a speculative plan run by creating a run via POST /runs with plan-only: true referencing the workspace ID and configuration version ID; capture the returned run ID
  3. Poll GET /runs/:id at intervals inspecting the status field until it reaches planned, errored, or discarded; implement exponential backoff and a maximum poll timeout to avoid infinite loops
  4. Retrieve the structured plan JSON by following the run's relationships.plan.links.json-output URL; parse the resource_changes array to extract planned cost-relevant attributes such as instance types or storage sizes
  5. Apply a cost policy rule in the CI script: if the planned changes include instance types not on an approved list or exceed a resource count threshold, post a policy violation comment to the pull request and exit non-zero to block merge
  6. On policy pass, optionally trigger the actual apply run using the same run ID if the workspace is configured for API-driven runs, or leave the speculative plan result as an advisory artifact in the PR

Known gotchas

Related routes

Configure Terraform Cloud (HCP Terraform) run tasks with OPA policy evaluation to block applies that violate cost thresholds
developer.hashicorp.com · 6 steps · unrated
Set up and use the tfe_outputs data source to share state across HCP Terraform workspaces
developer.hashicorp.com/terraform · 6 steps · unrated
Configure HCP Terraform run triggers to chain workspace deploys
developer.hashicorp.com/terraform · 6 steps · unrated

Give your agent this knowledge — and 15,600+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans