JSON representation of the current Terraform configuration

28 Views Asked by At

I want 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.

2

There are 2 best solutions below

0
Bryan Wieger On

This may help you: https://developer.hashicorp.com/terraform/cli/commands/show

terraform show -json

The terraform show command is used to provide human-readable output from a state or plan file. This can be used to inspect a plan to ensure that the planned operations are expected, or to inspect the current state as Terraform sees it. Machine-readable output is generated by adding the -json command-line flag.

Alternatively: https://developer.hashicorp.com/terraform/cli/commands/plan

terraform plan - json

-json - Enables the machine readable JSON UI output. This implies -input=false, so the configuration must have no unassigned variable values to continue.

0
marcjanek On

I would add something more to the answer. To get all options for the resources that are in defined providers you can use command:

terraform providers schema -json

Part of the output from this command:

"google_storage_bucket_iam_member": {
  "version": 0,
  "block": {
    "attributes": {
      "bucket": {
        "type": "string",
        "description_kind": "plain",
        "required": true
      },
      "etag": {
        "type": "string",
        "description_kind": "plain",
        "computed": true
      },
      "id": {
        "type": "string",
        "description_kind": "plain",
        "optional": true,
        "computed": true
      },
      "member": {
        "type": "string",
        "description_kind": "plain",
        "required": true
      },
      "role": {
        "type": "string",
        "description_kind": "plain",
        "required": true
      }
    },
    "block_types": {
      "condition": {
        "nesting_mode": "list",
        "block": {
          "attributes": {
            "description": {
              "type": "string",
              "description_kind": "plain",
              "optional": true
            },
            "expression": {
              "type": "string",
              "description_kind": "plain",
              "required": true
            },
            "title": {
              "type": "string",
              "description_kind": "plain",
              "required": true
            }
          },
          "description_kind": "plain"
        },
        "max_items": 1
      }
    },
    "description_kind": "plain"
  }
}

You can combine terraform show -json with terraform providers schema -json to achieve your goals.