0
0
TerraformComparisonBeginner · 3 min read

Terraform Plan vs Apply: Key Differences and When to Use Each

terraform plan shows what changes Terraform will make to your infrastructure without applying them, acting like a preview. terraform apply actually makes those changes, creating, updating, or deleting resources as defined.
⚖️

Quick Comparison

This table summarizes the main differences between terraform plan and terraform apply.

Aspectterraform planterraform apply
PurposePreview changes without making themExecute changes to infrastructure
Effect on InfrastructureNo changes madeCreates, updates, or deletes resources
OutputShows planned actionsShows applied actions and updates state
Use CaseReview changes before applyingDeploy or update infrastructure
State FileReads current state, no writeUpdates state file after changes
SafetySafe to run anytimeChanges can impact live resources
⚖️

Key Differences

terraform plan is a dry run that calculates and displays the changes Terraform will perform on your infrastructure. It compares your configuration files with the current state and cloud resources, then outputs a detailed list of actions like creating, modifying, or deleting resources. This command does not change anything; it only helps you understand what will happen.

In contrast, terraform apply takes the planned changes and executes them. It creates new resources, updates existing ones, or removes those no longer needed. After applying, it updates the Terraform state file to reflect the new real-world infrastructure state. This command changes your cloud environment and should be used carefully.

Using terraform plan before terraform apply is a best practice. It helps catch mistakes and avoid unexpected changes by letting you review the plan. You can even save the plan output and apply it later to ensure consistency.

⚖️

Code Comparison

Here is how you use terraform plan to preview changes for a simple AWS S3 bucket creation.

bash
terraform plan
Output
An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_s3_bucket.example will be created + resource "aws_s3_bucket" "example" { + bucket = "my-example-bucket" + acl = "private" } Plan: 1 to add, 0 to change, 0 to destroy.
↔️

terraform apply Equivalent

Here is how you use terraform apply to actually create the AWS S3 bucket after reviewing the plan.

bash
terraform apply
Output
An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_s3_bucket.example will be created + resource "aws_s3_bucket" "example" { + bucket = "my-example-bucket" + acl = "private" } Plan: 1 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Terraform will create 1 resource. Enter a value: yes aws_s3_bucket.example: Creating... aws_s3_bucket.example: Creation complete after 2s [id=my-example-bucket] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
🎯

When to Use Which

Choose terraform plan whenever you want to see what Terraform will do before making any changes. It is perfect for reviewing and validating your infrastructure updates safely. Use it to avoid surprises and confirm your intentions.

Choose terraform apply when you are ready to make real changes to your infrastructure. This command executes the plan and updates your cloud resources and state file. Always run terraform plan first to ensure the apply matches your expectations.

Key Takeaways

terraform plan previews changes without modifying infrastructure.
terraform apply executes changes and updates the state.
Always run terraform plan before terraform apply to avoid mistakes.
terraform plan is safe to run anytime; terraform apply changes live resources.
Use terraform apply only when you are confident about the planned changes.