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
