Terraform, developed by HashiCorp, is an open-source infrastructure as code (IaC) tool. It empowers users to articulate and deploy infrastructure using code to facilitate the automation of the complete infrastructure lifecycle and encompass the creation, modification, and removal of resources. Terraform supports various cloud providers, on-premises data centers, and other infrastructure components, making it a versatile solution for managing infrastructure in a consistent and scalable manner.
In this Answer, we will discuss two important Terraform commands: terraform plan
and terraform apply
.
terraform plan
commandThe terraform plan
command analyzes the desired state described in the Terraform configuration files and generates an execution plan. It calculates the changes required to achieve the desired state and provides an overview of the infrastructure modifications that Terraform will apply.
Workflow
During the planning phase, Terraform examines the current state of the infrastructure and compares it to the proposed changes specified in the Terraform configuration files. It identifies any additions, modifications, or deletions required to reach the desired state and generates a report.
Syntax
terraform plan
Running the terraform plan
command allows users to review the impact of their proposed changes before applying them, ensuring they are aware of potential modifications and their consequences.
Here is the output the dummy output for the terraform plan
command:
terraform apply
commandThe terraform apply
command executes the changes specified in the Terraform configuration files. It provisions or modifies the defined infrastructure resources, creating or updating the real-world infrastructure based on the desired state described in the configuration.
Workflow
Once the user reviews the execution plan generated by the terraform plan
command and is satisfied with the proposed changes, they can proceed with the terraform apply
command to initiate the provisioning process. Terraform applies modifications to the infrastructure, creating new resources, updating existing ones, or deleting obsolete resources.
Syntax
terraform apply
Running the terraform apply
command allows users to apply the planned changes to their infrastructure, ensuring that the desired state is implemented.
Here is the dummy output after running terraform apply
command:
Let’s take a look at a simple example that distinguishes between the terraform plan
and terraform apply
commands. We begin by utilizing the terraform plan
command, which calculates the necessary changes. After executing this command, we can observe the absence of any added files in the module when using the ls
command.
However, when we proceed with the terraform apply
command, we can observe that the file specified in main.tf
is added to the module, as confirmed by the ls
command.
Note: The Terraform version used in this Answer is Terraform v1.4.6.
resource "local_file" "creating_file" { content = "Content will be written after using terraform apply" filename = "${path.module}/planVsApply.txt" }
The terraform plan
command provides an execution plan to allow users to review the proposed changes and understand their impact without actually modifying the infrastructure. On the other hand, the terraform apply
command applies the changes to the infrastructure, provisioning or modifying resources based on the desired state defined in the Terraform configuration. Understanding the differences between these commands helps users ensure the accuracy and predictability of infrastructure changes while working with Terraform.
Free Resources