Is it possible to get json output for speculative terraform plan?

I am using TFE with GitHub.
I have a need to get plan output in JSON format.
Initially I tried to use terraform plan command out -out parameter

terraform plan -no-color -out=./plan/out.txt

I am getting this error:

The “remote” backend does not support saving the generated execution plan

locally at this time.

Next I tried to use API to get JSON output

I am following documentation here

It says this is how we get plan output in JSON format:
GET /runs/:id/plan/json-output

I have a valid Speculative run URL. I can verify that URL is valid by accessing it in browser.
I added /plan/json-output to the URL

curl \
  --header "Authorization: Bearer $TOKEN" \
  --header "Content-Type: application/vnd.api+json" \
  --location \
  https://MYTFE/run/runid/plan/json-output |
  > json-output.json

   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100    49    0    49    0     0    362      0 --:--:-- --:--:-- --:--:--   365
{"errors":[{"status":"404","title":"not found"}]}

Is it possible to get plan output in JSON format for a speculative plan?

Usually this should work:

terraform plan -out=./plan/out.txt
terraform show -json ./plan/out.txt

But I don’t know what’s causing the issue with TFE in your case. :man_shrugging:

1 Like


Yes, it is possible to get the JSON plan for a speculative plan from the Terraform Cloud API, and using the endpoints you linked above. A 404 in this case is likely due to the request being unauthorized; is the token you’re providing a valid one, and one that has read access to the workspace?

1 Like

Yes it turned out to be an authorization issue.
I wish the error message was better than 404.

Hello @rsingh
Were you able to resolve the Authorization issue? If so, do you mind share that information?

Thank you!

@laurentiuspurba I was able to resolve this issue. I needed admin permission in the workspace.

1 Like

I want to do the same thing but I want to have the human readable output from the speculative plan JSON. So is it possible to convert the JSON plan to human readable output?

Hi @Satak,

Terraform CLI generates both the human-readable output and the JSON output from Terraform Core’s internal representation of a plan. There isn’t currently any way to translate from JSON output to human-readable output; instead you’d need access to the saved plan file that Terraform Cloud/Enterprise generated the JSON from, which is kept privately within the Terraform Cloud data store and not accessible for outside retrieval.

1 Like