šŸ¶
Terraform

Terraform Plan Output to Readable File: A Guide

By Filip on 10/08/2024

Learn how to export your Terraform plan and apply outputs to a readable file for easy sharing, documentation, and troubleshooting.

Terraform Plan Output to Readable File: A Guide

Table of Contents

Introduction

When working with Terraform, understanding the execution plan before applying changes to your infrastructure is crucial. While terraform plan provides this information, the output is not easily digestible. To view the plan in a human-readable format, follow these steps:

Step-by-Step Guide

To get a human-readable Terraform plan, you should use the terraform show command with a plan file.

  1. Generate a plan file: Run terraform plan -out=tfplan.out to save the execution plan to a file named "tfplan.out". This file is in a binary format, not directly human-readable.

  2. Convert the plan to a readable format: Use the command terraform show tfplan.out to display the plan in a user-friendly format within your terminal.

  3. (Optional) Save the readable output to a file: Redirect the output of the previous command to a file using terraform show tfplan.out > readable_plan.txt. This will create a file named "readable_plan.txt" containing the human-readable plan.

While you can redirect the output of terraform plan directly to a file, this approach is not recommended for storing and reusing the plan. The -out flag with terraform plan is specifically designed for saving an executable plan that can be later applied using terraform apply.

Code Example

This code snippet explains how to create a human-readable Terraform plan. It starts by creating a simple Terraform configuration file. Then, it uses the 'terraform plan' command to generate a plan file. The 'terraform show' command is used to display the plan in a user-friendly format, which can optionally be saved to a text file. The code emphasizes that while you can redirect the output of 'terraform plan' directly, it's recommended to use the '-out' flag for saving an executable plan.

This example demonstrates how to generate a human-readable Terraform plan.

1. Create a Terraform Configuration File (e.g., main.tf):

resource "google_storage_bucket" "example" {
  name          = "my-tf-test-bucket"
  location      = "US"
  force_destroy = false
}

2. Generate a Plan File:

terraform plan -out=tfplan.out

This command generates an execution plan and saves it to the "tfplan.out" file.

3. Convert the Plan to a Readable Format:

terraform show tfplan.out

This command displays the plan in a user-friendly format within your terminal.

4. (Optional) Save the Readable Output to a File:

terraform show tfplan.out > readable_plan.txt

This command redirects the output of the previous command to a file named "readable_plan.txt".

Important Note:

While you can redirect the output of terraform plan directly to a file, this is not recommended for storing and reusing the plan. The -out flag with terraform plan is specifically designed for saving an executable plan that can be later applied using terraform apply.

Additional Notes

  • Importance of Review: Always review the human-readable plan (terraform show) before applying changes. This helps prevent unintended consequences and ensures your infrastructure changes are as expected.
  • Version Control: It's good practice to commit your .tfplan files to version control. This provides an audit trail of planned changes and allows for easy rollback if needed.
  • Plan vs. Apply: Remember that terraform plan only shows the planned execution, while terraform apply actually makes the changes to your infrastructure.
  • Alternative Output Formats: While terraform show provides a user-friendly format, you can explore tools like terraform-plan-parser or jq for further customization and integration with other tools.
  • Security Considerations: Be mindful of sensitive information that might be present in your plan output, especially when sharing or storing it. Consider using tools to redact or mask sensitive data.
  • Large Plans: For very large plans, the output of terraform show can be overwhelming. Consider using tools like grep or less to filter and navigate the output effectively.
  • Automation: Integrate the generation and review of human-readable plans into your CI/CD pipelines to ensure consistent and auditable infrastructure changes.
  • Collaboration: Share the human-readable plan with your team for review and collaboration, especially in larger projects. This promotes transparency and helps catch potential issues early on.

Summary

Step Command Description
1. Generate Plan File (binary) terraform plan -out=tfplan.out Creates a file named "tfplan.out" containing the execution plan in a binary format.
2. Convert to Readable Format terraform show tfplan.out Displays the plan in a user-friendly format in the terminal.
3. (Optional) Save Readable Output terraform show tfplan.out > readable_plan.txt Saves the human-readable plan to a file named "readable_plan.txt".

Note: While you can redirect terraform plan output directly, it's not recommended for storing reusable plans. Use the -out flag for creating executable plans for later use with terraform apply.

Conclusion

In conclusion, generating a human-readable Terraform plan is essential for understanding and verifying infrastructure changes before applying them. Using the terraform show command with a plan file allows for easy review and collaboration, reducing the risk of unintended consequences. Remember to integrate this practice into your workflow for safer and more transparent infrastructure management.

References

Were You Able to Follow the Instructions?

šŸ˜Love it!
šŸ˜ŠYes
šŸ˜Meh-gical
šŸ˜žNo
šŸ¤®Clickbait