If you have ever tried to terraform apply tfplan.txt
with tfplan.txt being a plain text terraform plan
command output file (or even tfplan.tf), you’ll get the following error message:
╷
│ Error: Failed to load "tfplan.txt" as a plan file
│
│ Error: zip: not a valid zip file
╵
Prerequisites
- Terraform
Solution
First, applying the terraform plan
output as a text file doesn’t make any sense, since TF generates a binary file and recommends saving the plan as tfplan or whatever you feel like except anything ending with .tf. I already did write about terraform plan the right way and terraform plan in plain text output (the right way) but, if you are too lazy to check it out, to sum up:
## Export plan and apply changes
terraform plan -out=tfplan
terraform apply tfplan
## Export plan to be human-readable only (don't try to apply changes it won't work!)
terraform plan -no-color > tfplan.txt
Now, sometimes missing double quotes (“”), having extra spaces or single quotes instead od double, could be the cause as well.
For instance terraform plan -var-file= './variables/prod.tfvars'
will throw an error. Instead, run:
terraform plan -var-file="./variables/prod.tfvars"
Conclusion
If you have any related unresolved issues, feel free to share them in the comment section below. On a side note, follow our official channel on Telegram.