Apply
Theterraform apply command executes the actions proposed in a Terraform plan to create, update, or delete infrastructure to match your configuration.
What It Does
When you runterraform apply, Terraform:
- Generates an execution plan (unless applying a saved plan)
- Prompts for approval (unless using
-auto-approveor a saved plan) - Executes the plan by making API calls to providers
- Updates the state file to reflect the new infrastructure state
- Displays resource changes and outputs after completion
When to Use It
Runterraform apply when you want to:
- Create new infrastructure resources
- Update existing infrastructure to match configuration changes
- Apply a previously saved plan file
- Provision infrastructure in a new environment
- Roll out configuration changes to production
Basic Usage
Run terraform apply
Apply changes to your infrastructure:Terraform will generate a plan and prompt for approval:
Applying Saved Plans
Common Flags and Options
Auto-Approval
-auto-approve
Skip the interactive approval prompt:
Destroy Mode
-destroy
Destroy all managed infrastructure:
Targeting Resources
-target=RESOURCE
Apply changes only to specific resources:
-replace=RESOURCE
Force replacement of a specific resource:
Variables
-var
Set variable values from the command line:
-var-file
Load variables from a file:
State Management
-backup=PATH
Path to backup the state file (default: -state-out path with “.backup” extension):
-lock=false
Disable state locking (dangerous):
-lock-timeout=DURATION
Wait for a state lock:
-state=PATH and -state-out=PATH
Legacy options for local backend only:
Output Control
-compact-warnings
Show warnings in compact form:
-no-color
Disable colored output:
-input=false
Disable interactive prompts:
Performance
-parallelism=N
Limit concurrent operations (default: 10):
Best Practices
Plan Before Apply
Always review changes before applying:Use Auto-Approve Sparingly
Avoid-auto-approve in production:
State Backups
Terraform automatically creates backups, but you can create manual backups:Incremental Rollouts
Use-target for incremental deployments:
Handle Sensitive Outputs
Be aware that outputs are displayed after apply:CI/CD Integration
In automated pipelines:Error Recovery
If apply fails mid-execution, Terraform updates state for completed resources:Resource Replacement Awareness
Be cautious with resources marked for replacement:- Backing up data before replacement
- Using lifecycle rules to prevent replacement:
Workspace Awareness
Verify you’re in the correct workspace:Understanding Apply Output
Progress Indicators
Resource Actions
| Action | Description | Example |
|---|---|---|
| Creating… | Resource is being created | aws_instance.web: Creating... |
| Modifying… | Resource is being updated | aws_instance.web: Modifying... |
| Destroying… | Resource is being deleted | aws_instance.web: Destroying... |
| Reading… | Data source is being read | data.aws_ami.ubuntu: Reading... |
Summary Line
- 5 added: New resources created
- 2 changed: Existing resources updated
- 1 destroyed: Resources deleted
Outputs Display
After apply, Terraform displays outputs:Troubleshooting
Apply Hangs
If apply seems stuck:State Lock Errors
Failed Apply with Partial Changes
Provider Authentication Errors
Cannot Destroy Dependencies
Next Steps
After successful apply:- Verify resources in your cloud provider console
- Test your infrastructure
- Commit state file changes (if using version control for state)
- Document any manual post-apply steps
- Use
terraform outputto retrieve output values