terraform,

'terraform plan' the right way

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

terraform plan assess the current TF configuration to decide whatever the desired state is up-to-date by comparing it with the current one, thus creating a so-called plan with all pending changes if any. Now, the plan is exportable to a file and this is where it can get a bit confusing for those who are not reading the docs. With that being said, let me show you how to properly run terraform plan.

Prerequisites

  • Terraform

Solution

By intuition, many beginners will try to output the terraform plan to a plan.tf file which confusingly is not the right way to do so. The command being terraform plan -out=plan.tf will simply lead to an invalid character error caused by terraform validate, although it would pass initially if you run terraform apply plan.tf. Every time you have new TF managed resources that needs to be deployed, you’ll need to remove any plan output file leftovers first. And that’s counterproductive, inconsistent and impractical right?!

Even though the output file extension could be .tf (and as a matter of fact probably you can save it under any extension you want to), according to the official docs the command will write the expected changes if any, in a “vague” (binary) file format. So, correct me if I’m wrong but, it seems like the extension doesn’t matter in a way, except for whatever_you_put_as_plan_output_filename.tf.

Therefore, tfplan, plan.tfplan and even tf.plan.out will do, though the first being the recommended one.

Note(s): If terraform plan -out=tfplan throws Error: Too many command line arguments, try replacing the equal sign (=) with a space e.g. terraform plan -out tfplan.

Conclusion

TL;DR:
terraform plan -out=plan.tf → BAD!
terraform plan -out=tfplan → GOOD!

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.