liveBook · Manning

Terraform show command

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

The terraform show command displays information from a Terraform state file or execution plan in human-readable form and can serialize it as JSON.

When you need to inspect a saved execution plan, `terraform show` displays its contents in human-readable form. Save the plan first, then pass its path to the command: section 5.3.6

Review a saved plan

`terraform plan -out test.tfplan` saves the plan; `terraform show test.tfplan` displays it. To review a plan programmatically, use `terraform show -json test.tfplan` to convert the saved plan to JSON. section 5.3.6 section 11.1.8

The command also reads state files. Its JSON output follows Terraform’s machine-readable UI, making it useful for tools that inspect infrastructure. It is not a complete copy of the underlying state: `serial` and `lineage` are missing. To retain those fields, capture them with `terraform state pull`, save the pulled state to a `.tfstate` file, and run `terraform show -json` on that file. section 11.1.6

Listing 11.14 Tofu.state method
import tempfile
 
class Tofu:
    def state(self) -> State:                                       #1
        pull_res = self._run(["state", "pull"])                     #2
        state_pull = pull_res.json()                                #3
 
        with tempfile.NamedTemporaryFile(suffix=".tfstate") as tmp: #4
            tmp.write(pull_res.stdout.encode())                     #5
            tmp.seek(0)                                             #6
            show_res = self._run(["show", "-json", tmp.name])       #7
 
        return State(                                               #8
            data=show_res.json(),                                   #9
            serial=state_pull["serial"],                            #10
            lineage=state_pull["lineage"],                          #10
        )

This wrapper writes pulled state to a temporary `.tfstate` file, passes it to `show -json`, and adds `serial` and `lineage` from the pulled state. section 11.1.6

For a tool that needs the full plan data rather than only planning log messages, keep a plan file even when the caller has not requested one. The tool can then pass that file to `terraform show`. section 11.1.8

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Shows direct plan-file usage and a state-wrapper procedure, including missing state fields; does not detail the command’s internals.
Examples
Uses a saved `test.tfplan` and a Python wrapper that combines pulled state with `show -json` output.
What it assumes
Assumes access to a saved plan or state; introduces direct plan review before developing a multi-command state wrapper.
What you can do after
Apply it via the Terraform CLI to review saved plans, or use its JSON output in plan and state inspection tools.
How it connects
Connects execution plans, Terraform state, the machine-readable UI, and `terraform state pull`.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.