Getting a terraform plan in json format

Starting terraform v0.12 we can get the terraform plan in json format. To do so, first we will need to save the plan to a file:

$ terraform plan -out demo.plan

This file is going to be a zip file that it is not intended to be unzipped, instead we can use the terraform show command to see it's contents:

$ terraform show demo.plan

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # kubernetes_namespace.jenkins_namespace will be created
  + resource "kubernetes_namespace" "jenkins_namespace" {
      + id = (known after apply)

      + metadata {
          + generation       = (known after apply)
          + name             = "jenkins"
          + resource_version = (known after apply)
          + self_link        = (known after apply)
          + uid              = (known after apply)
        }
    }

(...)

Plan: 3 to add, 0 to change, 0 to destroy.

But we can also tell terraform show to output it in json format with far more details than this plain text output:

$ terraform show -json demo.plan | python -m json.tool
{
    "format_version": "0.1",
    "terraform_version": "0.12.29",
    "variables": {
        "additional_plugins": {
            "value": []
        },
        "jenkins_namespace": {
            "value": "jenkins"
        }
    },
    "planned_values": {
        "root_module": {
            "resources": [
                {
                    "address": "kubernetes_namespace.jenkins_namespace",
                    "mode": "managed",
                    "type": "kubernetes_namespace",
                    "name": "jenkins_namespace",
                    "provider_name": "kubernetes",
                    "schema_version": 0,
                    "values": {
                        "metadata": [
                            {
                                "annotations": null,
                                "generate_name": null,
                                "labels": null,
                                "name": "jenkins"
                            }
                        ],
                        "timeouts": null
                    }
                }
            ],
(...)

Output format is versioned, so it might change in the future, even though it is using the same format for 0.12, 0.13 and 0.14

"format_version": "0.1",

Posted on 28/01/2021