terraform,

'terraform plan' in plain text output (the right way)

Jun 05, 2022 · 1 min read · Post a comment

Following up to the previous post terraform plan the right way with intentions to keep things simple and clean as possible, I’ll try to describe how to get a terraform plan plain text output without any weird, non-ASCII characters.

Prerequisites

  • Terraform

Solution

To sum up, terraform plan will generate an output binary file including the planned Terraform-managed resource changes if any for sure. But, if you want to save the output as a plain text somewhere else besides the console, run the following command:

terraform plan > tfplan.txt

This is not the optimal approach since the output will look something like:

module.vpc.aws_route_table.cache: Refreshing state... [id=rtb-mb3g3r8omafctdebo]
module.vpc.aws_security_group.ssh: Refreshing state... [id=sg-8a7jjz2lavujcz1ux]
module.vpc.aws_subnet.db[1]: Refreshing state... [id=subnet-2n3l5f7yml52eihn2]

The right way:

terraform plan -no-color > tfplan.txt

Output:

module.vpc.aws_route_table.cache: Refreshing state... [id=rtb-mb3g3r8omafctdebo]
module.vpc.aws_subnet.db[0]: Refreshing state... [id=subnet-8a7jjz2lavujcz1ux]
module.vpc.aws_subnet.cache[0]: Refreshing state... [id=subnet-2n3l5f7yml52eihn2]

Much better. Now, don’t try to terraform apply tfplan.txt as it won’t make any sense (keep in mind it’s a text file not a binary) plus you’ll get the following error:

╷
│ Error: Failed to load "tfplan.txt" as a plan file
│ 
│ Error: zip: not a valid zip file
╵

Related post: Failed to load tfplan as a plan file zip: not a valid zip file error in Terraform.

If you want to apply the planned changes:

terraform plan -out=tfplan
terraform apply tfplan

or simply visit the previous post referenced in the intro section for more details.

Conclusion

If you have any questions, thoughts, discussions or opinions, feel free to leave a comment below. On a side note, follow our official channel on Telegram.