Terraform plan输出文件my-plan的编码格式是什么?
Hey there! Let's break down exactly what's going on with that garbled my-plan file you generated, since you're looking to use it in source control and custom tools.
Why your plan file looks messy
First things first: The file created by terraform plan -out=my-plan is not plain text. It's a compressed binary format, which is why opening it in a text editor shows unreadable, jumbled characters. This is intentional!
Underlying structure: Protobuf + Gzip
Terraform uses a custom Protocol Buffers (Protobuf) schema to serialize all the plan data (like resource changes, variable values, and state references), then compresses the result with gzip to keep the file size small.
Protobuf is a compact, machine-focused serialization format—great for efficient data transfer and storage, but not designed for humans to read directly. This format serves a few key purposes:
- It ensures the plan can't be easily modified (so you know
terraform applywill execute exactly the changes you reviewed) - It's far more space-efficient than plain text formats like JSON or YAML
- It provides a consistent structure that Terraform and external tools can reliably parse
Working with the plan for your use case
If you want to ingest the plan into custom tools, run tests against it, or store a trackable version in source control, here are your best options:
- Convert to JSON (easiest path): Run
terraform show -json my-planto export the plan as structured, machine-readable JSON. This output is perfect for custom scripts, testing tools, and even committing to source control (since it's diffable and human-readable when needed). - Parse the raw binary: If you need to work directly with the compressed Protobuf data, you can use Terraform's internal Protobuf definitions (found in the Terraform source code, e.g., files under
internal/plans/). Use Protobuf tooling to generate parsing code in your preferred language, then decompress the gzip data before parsing.
A quick tip for source control
Skip committing the raw binary my-plan file to your repo. These files are environment-specific, can bloat your repo over time, and don't play nicely with diff tools. Instead, commit the JSON output from terraform show -json—it captures all the critical plan details in a format that's easy to track and review.
内容的提问来源于stack exchange,提问作者user1619524

