Plan Command
Learn how to use Terraform plans with the Terraform plan command
We'll cover the following
Terraform plan command#
Until now, we have been taking advantage of the fact that the command terraform apply
does a plan first and pauses for you to confirm by default.
It is, however, possible to get Terraform to do a plan without the option of applying it. To do
this, run the command terraform plan
.
A Terraform plan command will produce a plan as we have seen before, but will not give you the option to apply it. Instead, it will simply show you the plan. This command can be useful if you want to test something but do not want to run the risk of applying it by accident. It can also be useful when building Terraform deployment pipelines, as we will discuss below.
Project example#
Let’s go through an example of using the Terraform plan command:
Save the plan#
After running the terraform plan
command,
Terraform will show you the same plan as we saw at the start of this lesson. You can save that plan to a file by using the -out
parameter. Let’s try the plan again and this time
save it to a file called myplan
by using the command terraform plan -out myplan
.
Use plan using apply command#
You may be wondering what you can do with the plan that you have saved to a file. Well, you can use the plan you just saved with the apply command to apply that plan to your infrastructure.
Try running the following command terraform apply myplan
. Terraform will just apply your plan saved in the file myplan
without stopping to ask you if you wanted to do it or not. The reason being that
by running apply in this way, you have broken the terraform apply command into two phases:
- The plan phase
- The apply phase
Because you saved the plan to a file and you pass that plan to apply, you are basically telling Terraform that “this is the plan I want you to execute in the apply phase”.
📝Note: Remember to finish up by destroying the resources by running
terraform destroy
and confirming by typingyes
and pressingenter
.
Plan command advantage#
Being able to separate out the plan and apply phase enables you to write your own build-deploy pipelines by having the plan as a step and then passing that plan file onto the apply step. This gives you the option to pause if you want and allow a human operator to check the plan before you pass it to be applied.