Leo Qiao
Leo Qiao

Reputation: 11

JSON representation of the current Terraform configuration

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.

Upvotes: 0

Views: 379

Answers (3)

LoganMzz
LoganMzz

Reputation: 1619

I had the same question and here solution I have opted for:

  • Generate and save the Terraform plan: terraform plan -refresh=false -out=terraform.tfplan

  • Generate a JSON representation of plan file: terraform show -json terraform.tfplan

Useful links:

Upvotes: 1

marcjanek
marcjanek

Reputation: 71

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.

Upvotes: 0

Bryan Wieger
Bryan Wieger

Reputation: 9

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.

Upvotes: 0

Related Questions