Plan
Theterraform plan command creates an execution plan that shows what actions Terraform will take to achieve the desired state defined in your configuration files. This command does not make any actual changes to your infrastructure.
What It Does
When you runterraform plan, Terraform:
- Reads the current state of your infrastructure
- Refreshes state by querying real infrastructure (unless
-refresh=false) - Compares the current state with your configuration
- Generates an execution plan showing additions, changes, and deletions
- Validates the configuration syntax and provider requirements
When to Use It
terraform plan should be run:
- Before applying any changes to verify what will happen
- During code review to validate infrastructure changes
- In CI/CD pipelines to detect configuration drift
- After modifying Terraform configuration files
- To generate a saved plan file for later application
- To preview the impact of
-targetor-replaceflags
Basic Usage
Review the plan
Carefully review:
- Resources to be created (marked with
+) - Resources to be modified (marked with
~) - Resources to be destroyed (marked with
-) - Resources to be replaced (marked with
-/+) - Output values that will change
Saving Plans
Inspect the saved plan (optional)
View the saved plan in human-readable format:Or as JSON for automation:
Common Flags and Options
Plan Customization
-destroy
Generate a plan to destroy all managed infrastructure:
-refresh-only
Generate a plan that only updates the state to match real infrastructure:
-refresh=false
Skip refreshing state from real infrastructure (faster, but potentially inaccurate):
Targeting Specific Resources
-target=RESOURCE
Limit planning to specific resources and their dependencies:
-replace=RESOURCE
Plan to replace a specific resource instead of updating it:
Variables
-var
Set input variable values from the command line:
-var-file
Load variable values from a file:
Output Options
-out=FILE
Save the plan to a file:
-detailed-exitcode
Return different exit codes based on plan results:
0- No changes needed (plan is empty)1- Error occurred2- Successful plan with changes
-compact-warnings
Show warnings in compact form:
-no-color
Disable colored output (useful for logging):
State Management
-lock=false
Disable state locking (dangerous in team environments):
-lock-timeout=DURATION
Wait for a state lock:
Advanced Options
-parallelism=N
Limit concurrent operations (default is 10):
-input=false
Disable interactive prompts:
Best Practices
Always Plan Before Apply
Never apply changes without reviewing a plan:Use Saved Plans in CI/CD
In automated pipelines, generate and save plans for review:Review Plans Carefully
Check for:- Unexpected deletions (marked with
-) - Resource replacements (marked with
-/+) which may cause downtime - Changes to critical resources like databases
- Output values that may expose sensitive data
Use Detailed Exit Codes in Automation
Detect when infrastructure drift occurs:Combine with Version Control
Review plans during pull requests:Handle Sensitive Values
Be cautious with plans containing sensitive data:Understanding Plan Output
Resource Actions
| Symbol | Action | Description |
|---|---|---|
+ | Create | Resource will be created |
- | Destroy | Resource will be destroyed |
~ | Update | Resource will be updated in-place |
-/+ | Replace | Resource will be destroyed and recreated |
<= | Read | Data source will be read |
Attribute Changes
Forces Replacement
Some changes require resource replacement:Known After Apply
Attributes that will be determined during apply:Troubleshooting
Plan Hangs
If plan seems stuck:Inconsistent State
If you see errors about inconsistent state:Provider Version Conflicts
required_providers block.
Next Steps
After reviewing the plan:- If the plan looks correct, proceed with
terraform apply - If changes are unexpected, modify your configuration and run
planagain - Save plans for audit purposes or approval workflows
- Use
terraform showto inspect saved plans