DevOps | Scripts | Automation

Powershell

How to make terraform plan file readable?

Description: Make terraform plan file readable

While writing terraform code and before applying the code directly for infra creation, we use the terraform plan command to view the environmental impact. It is always good to analyze your environment before running the terraform apply command.

When we run the terraform plan command from the local machine or through the pipeline, the terraform plan is displayed on the console. If you are using a pipeline then it is easy to browse the plan file and search for the values you need.

However, using a terminal it is difficult to scroll the terminal or search for values you need so Terraform has provided a facility to save the plan output and later you can use the same output in the apply command.

Console output example,



-Out option

Save your terraform output with the -Out parameter and provide the path of the file to save the output. For example,

terraform plan -out NonProd.tfplan

The above command stores the output file NonProd.tfplan in the same directory as the main.tf configuration file. You can provide a different path as well like C:\temp\NonProd.tfplan.

Output of the above command. This is just one resource now think about the infra that has more than 20 resources. It will be cumbersome to check the plan by just scrolling through the terminal if you want to see only 1-2 resource properties.

It is not necessary to provide the .tfplan extension to store the output, you can use any type of extension but do not use the reserved extension by terraform like .tfvars or .tf.

Open that tfplan file into any text editor and you will find it’s not in that readable format and this can be only understood by Terraform. Even if you save plan file in .txt format then it won’t be readable.

Output from Notepad++.

However, you can read this file by the terraform show command.

terraform show .\NonProd.tfplan

To make this in a readable format, you need to use the -no-color parameter with the terraform show command and save it to txt format.

terraform show -no-color .\NonProd.tfplan > NonProdPlan.txt

Now check the NonProdPlan.txt file.

Loading