JSON representation of the current configuration

Hi everyone!

I wanted to have a clear, machine-readable (e.g. JSON) representation of all the resources that are currently defined in my Terraform project, along with all of the resource configs/options. Is there a way to do that?

The tfstate file is not what I need, because that is a representation of the state, not the current configuration.

The tfplan file kind of contains the information I want, but upon closer inspection, it seems much more complex and difficult to work with. So it is not exactly what I need.

Can anyone help me achieve this? Thank you!

1 Like

I think terraform might not be advanced enough for this - you might need to switch to AWS’s CloudFormation.

Hi @leoqiao18,

Since Terraform configurations contain dynamic expressions rather than just static values, there isn’t really a clear transformation to JSON that would be useful to software other than Terraform: only Terraform knows how to resolve the expressions in the configuration.

If you create a saved plan file and then ask Terraform to produce a JSON representation of it then that result contains (amongst other things) a JSON representation of the configuration that represents some detail about how the values are defined:

  • terraform plan -out=tfplan
  • terraform show -json tfplan

The result of the second command is a JSON plan representation, which includes a property "configuration" that contains a JSON configuration representation.

For each argument in the configuration, Terraform includes an expression representation that has two mutually-exclusive JSON properties:

  • "constant_value" is included for arguments that are defined with constant values, like "foo" or true.
  • "references" is included for arguments that are defined with results derived from other objects in the configuration, such as by referring to other resources.

If you need more detail than that you will need to analyze the source configuration directly, using the HCL library that Terraform itself uses to parse and analyze the configuration. The exact details for this, and the difficulty of doing it, will depend on what kind of information you are hoping to derive.