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.
| Aspect | terraform plan | terraform apply |
|---|---|---|
| Purpose | Preview changes without making them | Execute changes to infrastructure |
| Effect on Infrastructure | No changes made | Creates, updates, or deletes resources |
| Output | Shows planned actions | Shows applied actions and updates state |
| Use Case | Review changes before applying | Deploy or update infrastructure |
| State File | Reads current state, no write | Updates state file after changes |
| Safety | Safe to run anytime | Changes 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.
terraform plan
terraform apply Equivalent
Here is how you use terraform apply to actually create the AWS S3 bucket after reviewing the plan.
terraform apply
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.terraform plan before terraform apply to avoid mistakes.terraform plan is safe to run anytime; terraform apply changes live resources.terraform apply only when you are confident about the planned changes.