Planning & Applying
A comprehensive developer cheatsheet covering Terraform execution and lifecycle commands, featuring essential CLI commands, common flags, and operational workflows for previewing infrastructure changes (terraform plan), safely applying configurations (terraform apply), destroying resources (terraform destroy), and implementing automation best practices.
Table of Contents
- terraform plan — Preview Changes
- terraform apply — Apply Changes
- terraform destroy — Destroy Infrastructure
- Quick Reference — Most Used Commands
- Pro Tips
terraform plan — Preview Changes
Creates an execution plan without making actual changes.
Basic Usage
$ terraform plan
Terraform will perform the following actions:
+ create
# aws_instance.example will be created
+ resource "aws_instance" "example" {
+ ami = "ami-0abcdef1234567890"
+ instance_type = "t2.micro"
}
Plan: 1 to add, 0 to change, 0 to destroy.Common Flags
| Flag | Purpose |
|---|---|
-out=tfplan | Save plan to file for later apply |
-var="key=value" | Override a variable |
-var-file="file.tfvars" | Use a variable definition file |
-target=resource | Limit plan to specific resource |
-destroy | Preview destruction |
-refresh=false | Skip refreshing state (faster) |
-detailed-exitcode | Return exit codes for automation |
Examples
Save plan to file:
$ terraform plan -out=tfplan
Plan: 1 to add, 0 to change, 0 to destroy.
Saved the plan to: tfplanPlan with environment variables:
$ terraform plan -var="environment=production" -var-file="prod.tfvars"Plan only a specific resource:
$ terraform plan -target=aws_instance.examplePreview destruction:
$ terraform plan -destroySkip refresh (faster but less accurate):
$ terraform plan -refresh=falseDetailed exit codes for automation:
$ terraform plan -detailed-exitcode
# 0 = no changes, 1 = error, 2 = changes presentterraform apply — Apply Changes
Executes the planned actions (creates, updates, destroys).
Basic Usage
$ terraform apply
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Enter a value: yes
aws_instance.example: Creating...
aws_instance.example: Creation complete
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.Common Flags
| Flag | Purpose |
|---|---|
tfplan | Apply a saved plan file |
-auto-approve | Skip confirmation prompt |
-var="key=value" | Override a variable |
-var-file="file.tfvars" | Use a variable definition file |
-target=resource | Apply only specific resource |
-replace=resource | Force recreation of resource |
-lock-timeout=10m | Wait up to 10m for state lock |
-parallelism=10 | Limit concurrent operations (default 10) |
-refresh=false | Skip state refresh (faster but riskier) |
Examples
Apply saved plan:
$ terraform apply tfplanApply with auto-approval (CI/CD):
$ terraform apply -auto-approveForce recreation of a resource:
$ terraform apply -replace="aws_instance.example"Apply only specific resource:
$ terraform apply -target=aws_instance.exampleApply with custom parallelism:
$ terraform apply -parallelism=20Apply with lock timeout:
$ terraform apply -lock-timeout=5mterraform destroy — Destroy Infrastructure
Removes all resources managed by the configuration.
Basic Usage
$ terraform destroy
Plan: 0 to add, 0 to change, 1 to destroy.
Do you really want to destroy all resources?
Enter a value: yes
aws_instance.example: Destroying...
aws_instance.example: Destruction complete
Destroy complete! Resources: 1 destroyed.Common Flags
| Flag | Purpose |
|---|---|
-auto-approve | Skip confirmation prompt |
-target=resource | Destroy only specific resource |
-var="key=value" | Override a variable |
-var-file="file.tfvars" | Use a variable definition file |
-lock-timeout=10m | Wait up to 10m for state lock |
-parallelism=10 | Limit concurrent operations |
Examples
Destroy with auto-approval:
$ terraform destroy -auto-approveDestroy only specific resource:
$ terraform destroy -target=aws_instance.exampleDestroy with environment variables:
$ terraform destroy -var="environment=dev" -var-file="dev.tfvars"Quick Reference — Most Used Commands
| Task | Command |
|---|---|
| Preview changes | terraform plan |
| Save plan to file | terraform plan -out=tfplan |
| Plan with variables | terraform plan -var-file="prod.tfvars" |
| Preview destruction | terraform plan -destroy |
| Apply changes | terraform apply |
| Apply saved plan | terraform apply tfplan |
| Apply with auto-approve | terraform apply -auto-approve |
| Apply with variables | terraform apply -var-file="prod.tfvars" |
| Force recreate resource | terraform apply -replace="resource.type.name" |
| Destroy all resources | terraform destroy |
| Destroy with auto-approve | terraform destroy -auto-approve |
| Destroy specific resource | terraform destroy -target=resource.type.name |
Pro Tips
-
Always
planbeforeapply— avoid surprises in production. -
Save plans for production —
terraform plan -out=prod.tfplanthenterraform apply prod.tfplanensures the exact planned changes are applied. -
Use
-auto-approveonly in CI/CD — never in interactive development. -
Use
-refresh=falseonly when you need speed — state may become stale. -
Use
-targetcarefully — changes to targeted resources may affect others via dependencies. -
-parallelismtuning — reduce if hitting AWS API rate limits, increase if you want faster operations. -
Exit codes for automation:
0= Success, no changes1= Error2= Success with changes