Learn how to export your Terraform plan and apply outputs to a readable file for easy sharing, documentation, and troubleshooting.
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:
To get a human-readable Terraform plan, you should use the terraform show
command with a plan file.
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.
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.
(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
.
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
.
terraform show
) before applying changes. This helps prevent unintended consequences and ensures your infrastructure changes are as expected..tfplan
files to version control. This provides an audit trail of planned changes and allows for easy rollback if needed.terraform plan
only shows the planned execution, while terraform apply
actually makes the changes to your infrastructure.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.terraform show
can be overwhelming. Consider using tools like grep
or less
to filter and navigate the output effectively.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
.
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.