Tf plan: sparse json output

Very similar to this old post, I have Terraform code but the state file was stored locally on someone’s laptop and is long gone. Re-creating the state file, or creating a plan to match existing infrastructure bringing it into Terraform, is certainly possible. We know that some unknown portion of the infrastructure has been changed outside of Terraform, so it’s not completely in sync.

My issue is that terraform plan does a great job of saying what TF thinks it needs to do. However, terraform plan -json is sparse and lacks any of the detail of the non-json output. It is missing crucial information like name property, tags, or other context that would help trying to match a TF-declared resource with the existing AWS resource it belongs to.

terraform plan -out=somefile is also not helpful because the plan file inside the generated zip is itself a binary file. Terraform code as a whole is not parsable HCL (says the hcl rubygem), so that’s out as well.

Is there something I’m doing wrong with Terraform that I could get machine-readable output from some form of terraform plan? Alternatively, is there a parser (preferably Ruby or Python) that can read Terraform code into native rb/py objects?

So far my best idea is a token parser that grabs the resource ... block, and parses everything between that resource’s { and } as HCL. Surely I’m making this much harder than it needs to be?

Hi @rjhornsby,

The supported method for parsing a plan file is using terraform itself. The plan file can be shown in json format using the command

terraform show -json planfile

This output contains all the details about the individual resource changes being planned (as well as other data, like outputs, refreshed state, and configuration).

1 Like

@jbardin - that’s perfect, thank you! This will save me a ton of time logically connecting TF declared resources to live objects. Looking at the terraform show documentation again in the context of your answer, I have no idea how I missed this.