Back to articles
Terraform Has a Free JSON API That Most DevOps Engineers Miss
How-ToDevOps

Terraform Has a Free JSON API That Most DevOps Engineers Miss

via Dev.to DevOpsAlex Spinov

Terraform CLI and Terraform Cloud both expose APIs that let you manage infrastructure state, trigger runs, and inspect plans programmatically. Terraform CLI JSON Output Every Terraform command supports machine-readable JSON: terraform plan -json -out = plan.tfplan terraform show -json plan.tfplan terraform output -json terraform show -json Terraform Cloud API Free tier available with full REST API: curl -H "Authorization: Bearer $TF_TOKEN " \ https://app.terraform.io/api/v2/organizations/my-org/workspaces curl -X POST -H "Authorization: Bearer $TF_TOKEN " \ -H "Content-Type: application/vnd.api+json" \ https://app.terraform.io/api/v2/runs \ -d '{"data":{"type":"runs","relationships":{"workspace":{"data":{"type":"workspaces","id":"ws-abc123"}}}}}' Building a Cost Estimator import json , subprocess result = subprocess . run ( [ " terraform " , " show " , " -json " , " plan.tfplan " ], capture_output = True , text = True ) plan = json . loads ( result . stdout ) for change in plan [ " res

Continue reading on Dev.to DevOps

Opens in a new tab

Read Full Article
7 views

Related Articles