terraform,

'terraform plan' the right way

Jun 03, 2022 · 1 min read · Post a comment
'terraform plan' the right way

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 of the pending changes if any. Now, the plan is exportable to a file and this is where it can gets 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 a invalid character error caused by terraform validate, although it would pass initially if you run terraform apply plan.tf. Everytime 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 prolly 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):

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.