Terraform Plan

Terraform Plan [Tricks] – What you should know about

Terraform Plan it’s the first phase of the Terraform workflow. And we need to understand it as your first task when you decide to learn Terraform. Although it’s a pretty straightforward concept, still it’s crucial to understand its importance.

The main function of Terraform is to build, change, and destroy infrastructure resources to reach the expected state defined in a Terraform code.

When people say “run the Terraform,” they commonly mean executing these provisioning activities to change the natural infrastructure resources. The Terraform executable (binary) has numerous different subcommands and arguments for a broad combination of management actions. Still, these fundamental provisioning tasks are the core of Terraform, like the three most common: Plan, apply, and destroy.

What is Terraform Plan?

When you start working on any project, the first command we must execute is “terraform init.” Where the Terraform downloads all dependencies needs for your project and installs them on the same folder, called .terraform (hidden). Later, after some changes or not, you have to run the terraform plan.

What does the Terraform Plan do?

Every time we deploy a new server into the Cloud, for example. First, we planned and designed it using code. But, once we deploy, we need to check if the server is there. But how does Terraform know that this server is running if we decide to execute the Terraform later?

Terraform knows that the server is there because we learned before about the Terraform State that all-new resource created is registered with the Remote State.

Terraform Plan compares and finds all resources between your current code and the Remote State (what we have in the Cloud) and builds a “plan” that is “diff” between both. Think about how Git works; it approaches at same logic. First, you check out your code from the remote repository; then, you always compare it with what you already have on the remote “origin.” before pushing new changes.

Once it has defined the difference between the present state and the desired state, terraform plan shows a report of the modifications required to achieve the desired state. But, remember that no changes are made until you execute “terraform apply.” 

Also, when executing the plan, the Terraform validates all syntaxes.

Parameters

There are several terraform parameters that we can combine with the “terraform plan” command to change planning behavior. These parameters work for both “terraform plan” and “terraform apply“.

  • replace=ADDRESS; Replace an existing resource.
  • refresh=BOOLEAN;  Use this option carefully; if you run it as “false,” it will not update the Remote State.

Using Plan with Variables

We can pass variables using the plan command. Let’s how to do it:

terraform plan -var 'KEY=VALUE'

The example above will pass the variable “KEY” to “terraform plan.” Also, we can pass several variables by appending the -var, for each one.

Also, if you prefer, you can call the plan command, using a specific file will all variables within.

terraform plan -var-file="terraform-variables.tfvars"

We can also specify multiple files by repeating the -var-file for each.

Output Example

Terraform Plan Examples
Terraform Plan Examples

Always pay attention to the plan output. They will tell us which resource will be created and destroyed. In addition, you need to be aware of all changes before executing the apply command. Especially if you work with more members on your team, sometimes, if you are working on a branch that is not updated, the risk of destroying resources that you shouldn’t it’s bigger.

Plan to File

You can save the plan to a file when you execute it.

Example:

terraform plan -out=plan.data

The command above creates a plan and saves all information to the provided filename in an unreadable file structure that you can subsequently use with “terraform apply” to perform the planned modifications.

Leave a Comment

Your email address will not be published.