How to Save Terraform Plan Output to a Human-Readable File
Save Terraform plan output by first generating a binary plan file with terraform plan -out=plan.tfplan, then converting it to human-readable text using terraform show -no-color plan.tfplan > plan.txt.
The HashiCorp Terraform repository manages infrastructure state through a binary plan format that ensures execution consistency. To create documentation you can read and archive, you must understand the relationship between the binary serialization in internal/command/plan.go and the text rendering logic in internal/command/show.go.
Why Terraform Uses Binary Plan Files
Terraform calculates infrastructure changes as structured data objects. When you use the -out flag, the tool serializes these objects to a binary file rather than plain text. This design, implemented in internal/command/plan.go, guarantees that the exact set of resources, dependencies, and actions planned will be executed later, eliminating the risk of configuration drift between the plan and apply phases.
Saving Terraform Plan Output in Three Steps
To capture a human-readable record of your execution plan, execute the binary creation and text conversion workflow.
Step 1: Generate the Binary Plan File
Run terraform plan with the -out flag to write the binary artifact to disk. The source code in internal/command/plan.go handles this serialization:
terraform plan -out=plan.tfplan
The resulting plan.tfplan file contains machine-readable data structures and cannot be viewed directly in a text editor.
Step 2: Convert Binary Plan to Text with terraform show
The terraform show command reads binary plan files and renders them as the standard diff output. Implemented in internal/command/show.go, this tool translates the binary representation into symbols like + (add), - (destroy), and ~ (change). Use the -no-color flag to strip ANSI codes when saving to a file:
terraform show -no-color plan.tfplan > plan.txt
Step 3: Capture Apply Output (Optional)
When executing a saved plan, capture the console output using standard shell redirection. The apply command in internal/command/apply.go executes the binary plan and prints a summary to stdout, which you can redirect or tee to a log file:
terraform apply -auto-approve plan.tfplan | tee apply.log
For drift detection after applying, you can also generate a new binary plan representing the post-apply state:
terraform apply -auto-approve -out=postapply.tfplan plan.tfplan
Complete Workflow Examples
Basic Plan-to-Text Conversion
Generate a plan, convert it to readable text, and inspect the changes:
# Create binary plan
terraform plan -out=myplan.tfplan
# Convert to human-readable format
terraform show -no-color myplan.tfplan > myplan.txt
# Review the output
cat myplan.txt
CI Pipeline Automation
Chain commands to ensure atomic plan generation and persistent logging:
terraform plan -out=plan.tfplan && \
terraform show -no-color plan.tfplan > plan.txt && \
terraform apply -auto-approve -input=false plan.tfplan | tee apply.log
Post-Apply Drift Detection
Save the state after applying to establish a baseline for future comparisons:
terraform plan -out=baseline.tfplan
terraform apply -auto-approve -out=postapply.tfplan baseline.tfplan
terraform show -no-color postapply.tfplan > drift-check.txt
Summary
- Binary plans ensure consistency: The
-outflag interraform plancreates a serialized artifact viainternal/command/plan.gothat guarantees identical execution during apply. - terraform show renders text: The
showcommand ininternal/command/show.goconverts binary plans into human-readable diff output suitable for code review. - Shell redirection captures apply logs: Pipe
terraform applytoteeor redirect stdout to save execution summaries, as the command itself does not write human-readable files directly. - Post-apply plans enable drift tracking: The
-outflag onterraform apply, implemented ininternal/command/apply.go, generates new binary plans after changes complete.
Frequently Asked Questions
Can I redirect terraform plan output directly to a text file?
No, redirecting stdout from terraform plan only captures the console display, which lacks the structured data needed for reliable execution. To save the actual plan for later application or review, use the -out flag to create a binary plan, then convert it with terraform show. This approach ensures the text representation exactly matches the binary plan Terraform will execute.
What is the difference between -out and redirecting stdout?
The -out flag writes a binary serialization containing the complete plan object, resource dependencies, and state references required for terraform apply. Redirecting stdout captures only the formatted text display, which is lossy and cannot be parsed back into an executable plan. Use -out when you need to save a plan for later execution; redirect stdout only when you need a log of the displayed output.
Does terraform apply support saving output to a file?
Terraform apply does not natively write human-readable output to a file through a dedicated flag, but you can capture console output using shell redirection or tee: terraform apply plan.tfplan | tee apply.log. Additionally, applying with -out=postapply.tfplan generates a new binary plan file representing the resulting state, which is useful for subsequent drift detection workflows.
Where are the terraform plan and show commands implemented in the source code?
The terraform plan command logic resides in internal/command/plan.go, which handles the -out flag and binary plan serialization. The terraform show command is implemented in internal/command/show.go, responsible for reading binary plan files and rendering them as human-readable text. The apply command implementation in internal/command/apply.go coordinates both executing saved plans and optionally writing post-apply plan files.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s https://instagit.com/install.md