github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/website/docs/internals/json-format.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Internals: JSON Output Format"
     4  sidebar_current: "docs-internals-json"
     5  description: |-
     6    Terraform provides a machine-readable JSON representation of state, configuration and plan.
     7  ---
     8  
     9  # JSON Output Format
    10  
    11  -> **Note:** This format is available in Terraform 0.12 and later.
    12  
    13  When Terraform plans to make changes, it prints a human-readable summary to the terminal. It can also, when run with `-out=<PATH>`, write a much more detailed binary plan file, which can later be used to apply those changes.
    14  
    15  Since the format of plan files isn't suited for use with external tools (and likely never will be), Terraform can output a machine-readable JSON representation of a plan file's changes. It can also convert state files to the same format, to simplify data loading and provide better long-term compatibility.
    16  
    17  Use `terraform show -json <FILE>` to generate a JSON representation of a plan or state file. See [the `terraform show` documentation](/docs/cli/commands/show.html) for more details.
    18  
    19  -> **Note:** The output includes a `format_version` key, which currently has major version zero to indicate that the format is experimental and subject to change. A future version will assign a non-zero major version and make stronger promises about compatibility. We do not anticipate any significant breaking changes to the format before its first major version, however.
    20  
    21  ## Format Summary
    22  
    23  The following sections describe the JSON output format by example, using a pseudo-JSON notation.
    24  
    25  Important elements are described with comments, which are prefixed with `//`.
    26  
    27  To avoid excessive repetition, we've split the complete format into several discrete sub-objects, described under separate headers. References wrapped in angle brackets (like `<values-representation>`) are placeholders which, in the real output, would be replaced by an instance of the specified sub-object.
    28  
    29  The JSON output format consists of the following objects and sub-objects:
    30  
    31  - [State Representation](#state-representation) — The complete top-level object returned by `terraform show -json <STATE FILE>`.
    32  - [Plan Representation](#plan-representation) — The complete top-level object returned by `terraform show -json <PLAN FILE>`.
    33  - [Values Representation](#values-representation) — A sub-object of both plan and state output that describes current state or planned state.
    34  - [Configuration Representation](#configuration-representation) — A sub-object of plan output that describes a parsed Terraform configuration.
    35      - [Expression Representation](#expression-representation) — A sub-object of a configuration representation that describes an unevaluated expression.
    36      - [Block Expressions Representation](#block-expressions-representation) — A sub-object of a configuration representation that describes the expressions nested inside a block.
    37  - [Change Representation](#change-representation) — A sub-object of plan output that describes planned changes to an object.
    38  
    39  ## State Representation
    40  
    41  Because state does not currently have any significant metadata not covered by the common values representation ([described below](#values-representation)), the `<state-representation>` is straightforward:
    42  
    43  ```javascript
    44  {
    45    // "values" is a values representation object derived from the values in the
    46    // state. Because the state is always fully known, this is always complete.
    47    "values": <values-representation>
    48  
    49    "terraform_version": "version.string"
    50  }
    51  ```
    52  
    53  The extra wrapping object here will allow for any extension we may need to add in future versions of this format.
    54  
    55  ## Plan Representation
    56  
    57  A plan consists of a prior state, the configuration that is being applied to that state, and the set of changes Terraform plans to make to achieve that.
    58  
    59  For ease of consumption by callers, the plan representation includes a partial representation of the values in the final state (using a [value representation](#values-representation)), allowing callers to easily analyze the planned outcome using similar code as for analyzing the prior state.
    60  
    61  ```javascript
    62  {
    63    "format_version": "0.2",
    64  
    65    // "prior_state" is a representation of the state that the configuration is
    66    // being applied to, using the state representation described above.
    67    "prior_state":  <state-representation>,
    68  
    69    // "configuration" is a representation of the configuration being applied to the
    70    // prior state, using the configuration representation described above.
    71    "configuration": <configuration-representation>,
    72  
    73    // "planned_values" is a description of what is known so far of the outcome in
    74    // the standard value representation, with any as-yet-unknown values omitted.
    75    "planned_values": <values-representation>,
    76  
    77    // "proposed_unknown" is a representation of the attributes, including any
    78    // potentially-unknown attributes. Each value is replaced with "true" or
    79    // "false" depending on whether it is known in the proposed plan.
    80    "proposed_unknown": <values-representation>,
    81  
    82    // "variables" is a representation of all the variables provided for the given
    83    // plan. This is structured as a map similar to the output map so we can add
    84    // additional fields in later.
    85    "variables": {
    86      "varname": {
    87        "value": "varvalue"
    88      },
    89    },
    90  
    91    // "changes" is a description of the individual change actions that Terraform
    92    // plans to use to move from the prior state to a new state matching the
    93    // configuration.
    94    "resource_changes": [
    95      // Each element of this array describes the action to take
    96      // for one instance object. All resources in the
    97      // configuration are included in this list.
    98      {
    99        // "address" is the full absolute address of the resource instance this
   100        // change applies to, in the same format as addresses in a value
   101        // representation
   102        "address": "module.child.aws_instance.foo[0]",
   103  
   104        // "module_address", if set, is the module portion of the above address.
   105        // Omitted if the instance is in the root module.
   106        "module_address": "module.child",
   107  
   108        // "mode", "type", "name", and "index" have the same meaning as in a
   109        // value representation.
   110        "mode": "managed",
   111        "type": "aws_instance",
   112        "name": "foo",
   113        "index": 0,
   114  
   115        // "deposed", if set, indicates that this action applies to a "deposed"
   116        // object of the given instance rather than to its "current" object.
   117        // Omitted for changes to the current object. "address" and "deposed"
   118        // together form a unique key across all change objects in a particular
   119        // plan. The value is an opaque key representing the specific deposed
   120        // object.
   121        "deposed": "deadbeef",
   122  
   123        // "change" describes the change that will be made to the indicated
   124        // object. The <change-representation> is detailed in a section below.
   125        "change": <change-representation>,
   126  
   127        // "action_reason" is some optional extra context about why the
   128        // actions given inside "change" were selected. This is the JSON
   129        // equivalent of annotations shown in the normal plan output like
   130        // "is tainted, so must be replaced" as opposed to just "must be
   131        // replaced".
   132        //
   133        // These reason codes are display hints only and the set of possible
   134        // hints may change over time. Users of this must be prepared to
   135        // encounter unrecognized reasons and treat them as unspecified reasons.
   136        //
   137        // The current set of possible values is:
   138        // - "replace_because_tainted": the object in question is marked as
   139        //   "tainted" in the prior state, so Terraform planned to replace it.
   140        // - "replace_because_cannot_update": the provider indicated that one
   141        //   of the requested changes isn't possible without replacing the
   142        //   existing object with a new object.
   143        // - "replace_by_request": the user explicitly called for this object
   144        //   to be replaced as an option when creating the plan, which therefore
   145        //   overrode what would have been a "no-op" or "update" action otherwise.
   146        //
   147        // If there is no special reason to note, Terraform will omit this
   148        // property altogether.
   149        action_reason: "replace_because_tainted"
   150      }
   151    ],
   152  
   153    // "output_changes" describes the planned changes to the output values of the
   154    // root module.
   155    "output_changes": {
   156      // Keys are the defined output value names.
   157      "foo": {
   158  
   159        // "change" describes the change that will be made to the indicated output
   160        // value, using the same representation as for resource changes except
   161        // that the only valid actions values are:
   162        //   ["create"]
   163        //   ["update"]
   164        //   ["delete"]
   165        // In the Terraform CLI 0.12.0 release, Terraform is not yet fully able to
   166        // track changes to output values, so the actions indicated may not be
   167        // fully accurate, but the "after" value will always be correct.
   168        "change": <change-representation>,
   169      }
   170    }
   171  }
   172  ```
   173  
   174  This overall plan structure, fully expanded, is what will be printed by the `terraform show -json <planfile>` command.
   175  
   176  ## Values Representation
   177  
   178  A values representation is used in both state and plan output to describe current state (which is always complete) and planned state (which omits values not known until apply).
   179  
   180  The following example illustrates the structure of a `<values-representation>`:
   181  
   182  ```javascript
   183  {
   184    // "outputs" describes the outputs from the root module. Outputs from
   185    // descendent modules are not available because they are not retained in all
   186    // of the underlying structures we will build this values representation from.
   187    "outputs": {
   188      "private_ip": {
   189        "value": "192.168.3.2",
   190        "sensitive": false
   191      }
   192    },
   193  
   194    // "root_module" describes the resources and child modules in the root module.
   195    "root_module": {
   196      "resources": [
   197        {
   198          // "address" is the absolute resource address, which callers must consider
   199          // opaque but may do full string comparisons with other address strings or
   200          // pass this verbatim to other Terraform commands that are documented to
   201          // accept absolute resource addresses. The module-local portions of this
   202          // address are extracted in other properties below.
   203          "address": "aws_instance.example[1]",
   204  
   205          // "mode" can be "managed", for resources, or "data", for data resources
   206          "mode": "managed",
   207          "type": "aws_instance",
   208          "name": "example",
   209  
   210          // If the count or for_each meta-arguments are set for this resource, the
   211          // additional key "index" is present to give the instance index key. This
   212          // is omitted for the single instance of a resource that isn't using count
   213          // or for_each.
   214          "index": 1,
   215  
   216          // "provider_name" is the name of the provider that is responsible for
   217          // this resource. This is only the provider name, not a provider
   218          // configuration address, and so no module path nor alias will be
   219          // indicated here. This is included to allow the property "type" to be
   220          // interpreted unambiguously in the unusual situation where a provider
   221          // offers a resource type whose name does not start with its own name,
   222          // such as the "googlebeta" provider offering "google_compute_instance".
   223          "provider_name": "aws",
   224  
   225          // "schema_version" indicates which version of the resource type schema
   226          // the "values" property conforms to.
   227          "schema_version": 2,
   228  
   229          // "values" is the JSON representation of the attribute values of the
   230          // resource, whose structure depends on the resource type schema. Any
   231          // unknown values are omitted or set to null, making them
   232          // indistinguishable from absent values; callers which need to distinguish
   233          // unknown from unset must use the plan-specific or configuration-specific
   234          // structures described in later sections.
   235          "values": {
   236            "id": "i-abc123",
   237            "instance_type": "t2.micro",
   238            // etc, etc
   239          },
   240  
   241          // "sensitive_values" is the JSON representation of the sensitivity of
   242          // the resource's attribute values. Only attributes which are sensitive
   243          // are included in this structure.
   244          "values": {
   245            "id": true,
   246          }
   247        }
   248      ]
   249  
   250      "child_modules": [
   251        // Each entry in "child_modules" has the same structure as the root_module
   252        // object, with the additional "address" property shown below.
   253        {
   254          // "address" is the absolute module address, which callers must treat as
   255          // opaque but may do full string comparisons with other module address
   256          // strings and may pass verbatim to other Terraform commands that are
   257          // documented as accepting absolute module addresses.
   258          "address": "module.child",
   259  
   260          // "resources" is the same as in "root_module" above
   261          "resources": [
   262              {
   263                "address": "module.child.aws_instance.foo",
   264                // etc, etc
   265              }
   266          ],
   267  
   268          // Each module object can optionally have its own
   269          // nested "child_modules", recursively describing the
   270          // full module tree.
   271          "child_modules": [ ... ],
   272        }
   273      ]
   274    }
   275  }
   276  ```
   277  
   278  The translation of attribute and output values is the same intuitive mapping from HCL types to JSON types used by Terraform's [`jsonencode`](/docs/language/functions/jsonencode.html) function. This mapping does lose some information: lists, sets, and tuples all lower to JSON arrays while maps and objects both lower to JSON objects. Unknown values and null values are both treated as absent or null.
   279  
   280  Only the "current" object for each resource instance is described. "Deposed" objects are not reflected in this structure at all; in plan representations, you can refer to the change representations for further details.
   281  
   282  The intent of this structure is to give a caller access to a similar level of detail as is available to expressions within the configuration itself. This common representation is not suitable for all use-cases because it loses information compared to the data structures it is built from. For more complex needs, use the more elaborate changes and configuration representations.
   283  
   284  ## Configuration Representation
   285  
   286  Configuration is the most complicated structure in Terraform, since it includes unevaluated expression nodes and other complexities.
   287  
   288  Because the configuration models are produced at a stage prior to expression evaluation, it is not possible to produce a values representation for configuration. Instead, we describe the physical structure of the configuration, giving access to constant values where possible and allowing callers to analyze any references to other objects that are present:
   289  
   290  ```javascript
   291  {
   292    // "provider_configs" describes all of the provider configurations throughout
   293    // the configuration tree, flattened into a single map for convenience since
   294    // provider configurations are the one concept in Terraform that can span
   295    // across module boundaries.
   296    "provider_configs": {
   297  
   298      // Keys in the provider_configs map are to be considered opaque by callers,
   299      // and used just for lookups using the "provider_config_key" property in each
   300      // resource object.
   301      "opaque_provider_ref_aws": {
   302  
   303        // "name" is the name of the provider without any alias
   304        "name": "aws",
   305  
   306        // "alias" is the alias set for a non-default configuration, or unset for
   307        // a default configuration.
   308        "alias": "foo",
   309  
   310        // "module_address" is included only for provider configurations that are
   311        // declared in a descendent module, and gives the opaque address for the
   312        // module that contains the provider configuration.
   313        "module_address": "module.child",
   314  
   315        // "expressions" describes the provider-specific content of the
   316        // configuration block, as a block expressions representation (see section
   317        // below).
   318        "expressions": <block-expressions-representation>
   319      }
   320    },
   321  
   322    // "root_module" describes the root module in the configuration, and serves
   323    // as the root of a tree of similar objects describing descendent modules.
   324    "root_module": {
   325  
   326      // "outputs" describes the output value configurations in the module.
   327      "outputs": {
   328  
   329        // Property names here are the output value names
   330        "example": {
   331          "expression": <expression-representation>,
   332          "sensitive": false
   333        }
   334      },
   335  
   336      // "resources" describes the "resource" and "data" blocks in the module
   337      // configuration.
   338      "resources": [
   339        {
   340          // "address" is the opaque absolute address for the resource itself.
   341          "address": "aws_instance.example",
   342  
   343          // "mode", "type", and "name" have the same meaning as for the resource
   344          // portion of a value representation.
   345          "mode": "managed",
   346          "type": "aws_instance",
   347          "name": "example",
   348  
   349          // "provider_config_key" is the key into "provider_configs" (shown
   350          // above) for the provider configuration that this resource is
   351          // associated with.
   352          "provider_config_key": "opaque_provider_ref_aws",
   353  
   354          // "provisioners" is an optional field which describes any provisioners.
   355          // Connection info will not be included here.
   356          "provisioners": [
   357            {
   358              "type": "local-exec",
   359  
   360              // "expressions" describes the provisioner configuration
   361              "expressions": <block-expressions-representation>
   362            },
   363          ],
   364  
   365          // "expressions" describes the resource-type-specific content of the
   366          // configuration block.
   367          "expressions": <block-expressions-representation>,
   368  
   369          // "schema_version" is the schema version number indicated by the
   370          // provider for the type-specific arguments described in "expressions".
   371          "schema_version": 2,
   372  
   373          // "count_expression" and "for_each_expression" describe the expressions
   374          // given for the corresponding meta-arguments in the resource
   375          // configuration block. These are omitted if the corresponding argument
   376          // isn't set.
   377          "count_expression": <expression-representation>,
   378          "for_each_expression": <expression-representation>
   379        },
   380      ],
   381  
   382      // "module_calls" describes the "module" blocks in the module. During
   383      // evaluation, a module call with count or for_each may expand to multiple
   384      // module instances, but in configuration only the block itself is
   385      // represented.
   386      "module_calls": {
   387  
   388        // Key is the module call name chosen in the configuration.
   389        "child": {
   390  
   391          // "resolved_source" is the resolved source address of the module, after
   392          // any normalization and expansion. This could be either a
   393          // go-getter-style source address or a local path starting with "./" or
   394          // "../". If the user gave a registry source address then this is the
   395          // final location of the module as returned by the registry, after
   396          // following any redirect indirection.
   397          "resolved_source": "./child"
   398  
   399          // "expressions" describes the expressions for the arguments within the
   400          // block that correspond to input variables in the child module.
   401          "expressions": <block-expressions-representation>,
   402  
   403          // "count_expression" and "for_each_expression" describe the expressions
   404          // given for the corresponding meta-arguments in the module
   405          // configuration block. These are omitted if the corresponding argument
   406          // isn't set.
   407          "count_expression": <expression-representation>,
   408          "for_each_expression": <expression-representation>,
   409  
   410          // "module" is a representation of the configuration of the child module
   411          // itself, using the same structure as the "root_module" object,
   412          // recursively describing the full module tree.
   413          "module": <module-configuration-representation>,
   414        }
   415      }
   416    }
   417  }
   418  ```
   419  
   420  ### Expression Representation
   421  
   422  Each unevaluated expression in the configuration is represented with an `<expression-representation>` object with the following structure:
   423  
   424  ```javascript
   425  {
   426    // "constant_value" is set only if the expression contains no references to
   427    // other objects, in which case it gives the resulting constant value. This is
   428    // mapped as for the individual values in a value representation.
   429    "constant_value": "hello",
   430  
   431    // Alternatively, "references" will be set to a list of references in the
   432    // expression. Multi-step references will be unwrapped and duplicated for each
   433    // significant traversal step, allowing callers to more easily recognize the
   434    // objects they care about without attempting to parse the expressions.
   435    // Callers should only use string equality checks here, since the syntax may
   436    // be extended in future releases.
   437    "references": [
   438      "data.template_file.foo[1].vars[\"baz\"]",
   439      "data.template_file.foo[1].vars", // implied by previous
   440      "data.template_file.foo[1]", // implied by previous
   441      "data.template_file.foo", // implied by previous
   442      "module.foo.bar",
   443      "module.foo", // implied by the previous
   444      "var.example[0]",
   445      "var.example", // implied by the previous
   446  
   447      // Partial references like "data" and "module" are not included, because
   448      // Terraform considers "module.foo" to be an atomic reference, not an
   449      // attribute access.
   450    ]
   451  }
   452  ```
   453  
   454  ### Block Expressions Representation
   455  
   456  In some cases, it is the entire content of a block (possibly after certain special arguments have already been handled and removed) that must be represented. For that, we have an `<block-expressions-representation>` structure:
   457  
   458  ```javascript
   459  {
   460    // Attribute arguments are mapped directly with the attribute name as key and
   461    // an <expression-representation> as value.
   462    "ami": <expression-representation>,
   463    "instance_type": <expression-representation>,
   464  
   465    // Nested block arguments are mapped as either a single nested
   466    // <block-expressions-representation> or an array object of these, depending on the
   467    // block nesting mode chosen in the schema.
   468    //  - "single" nesting is a direct <block-expressions-representation>
   469    //  - "list" and "set" produce arrays
   470    //  - "map" produces an object
   471    "root_block_device": <expression-representation>,
   472    "ebs_block_device": [
   473      <expression-representation>
   474    ]
   475  }
   476  ```
   477  
   478  For now we expect callers to just hard-code assumptions about the schemas of particular resource types in order to process these expression representations. In a later release we will add new inspection commands to return machine-readable descriptions of the schemas themselves, allowing for more generic handling in programs such as visualization tools.
   479  
   480  ## Change Representation
   481  
   482  A `<change-representation>` describes the change that will be made to the indicated object.
   483  
   484  ```javascript
   485  {
   486    // "actions" are the actions that will be taken on the object selected by the
   487    // properties below.
   488    // Valid actions values are:
   489    //    ["no-op"]
   490    //    ["create"]
   491    //    ["read"]
   492    //    ["update"]
   493    //    ["delete", "create"]
   494    //    ["create", "delete"]
   495    //    ["delete"]
   496    // The two "replace" actions are represented in this way to allow callers to
   497    // e.g. just scan the list for "delete" to recognize all three situations
   498    // where the object will be deleted, allowing for any new deletion
   499    // combinations that might be added in future.
   500    "actions": ["update"],
   501  
   502    // "before" and "after" are representations of the object value both before
   503    // and after the action. For ["create"] and ["delete"] actions, either
   504    // "before" or "after" is unset (respectively). For ["no-op"], the before and
   505    // after values are identical. The "after" value will be incomplete if there
   506    // are values within it that won't be known until after apply.
   507    "before": <value-representation>,
   508    "after": <value-representation>,
   509  
   510    // "after_unknown" is an object value with similar structure to "after", but
   511    // with all unknown leaf values replaced with "true", and all known leaf
   512    // values omitted. This can be combined with "after" to reconstruct a full
   513    // value after the action, including values which will only be known after
   514    // apply.
   515    "after_unknown": {
   516      "id": true
   517    },
   518  
   519    // "before_sensitive" and "after_sensitive" are object values with similar
   520    // structure to "before" and "after", but with all sensitive leaf values
   521    // replaced with true, and all non-sensitive leaf values omitted. These
   522    // objects should be combined with "before" and "after" to prevent accidental
   523    // display of sensitive values in user interfaces.
   524    "before_sensitive": {},
   525    "after_sensitive": {
   526      "triggers": {
   527        "boop": true
   528      }
   529    },
   530  
   531    // "replace_paths" is an array of arrays representing a set of paths into the
   532    // object value which resulted in the action being "replace". This will be
   533    // omitted if the action is not replace, or if no paths caused the
   534    // replacement (for example, if the resource was tainted). Each path
   535    // consists of one or more steps, each of which will be a number or a
   536    // string.
   537    "replace_paths": [["triggers"]]
   538  }
   539  ```