New batchZero to DevOps starts 29 Nov · limited seatsReserve your seat
Cheat sheetsTerraformTerraform
FreeSheet 05 of 9

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

Creates an execution plan without making actual changes.

Basic Usage

bash
$ 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

FlagPurpose
-out=tfplanSave plan to file for later apply
-var="key=value"Override a variable
-var-file="file.tfvars"Use a variable definition file
-target=resourceLimit plan to specific resource
-destroyPreview destruction
-refresh=falseSkip refreshing state (faster)
-detailed-exitcodeReturn exit codes for automation

Examples

Save plan to file:

bash
$ terraform plan -out=tfplan
Plan: 1 to add, 0 to change, 0 to destroy.
Saved the plan to: tfplan

Plan with environment variables:

bash
$ terraform plan -var="environment=production" -var-file="prod.tfvars"

Plan only a specific resource:

bash
$ terraform plan -target=aws_instance.example

Preview destruction:

bash
$ terraform plan -destroy

Skip refresh (faster but less accurate):

bash
$ terraform plan -refresh=false

Detailed exit codes for automation:

bash
$ terraform plan -detailed-exitcode
# 0 = no changes, 1 = error, 2 = changes present

terraform apply — Apply Changes

Executes the planned actions (creates, updates, destroys).

Basic Usage

bash
$ 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

FlagPurpose
tfplanApply a saved plan file
-auto-approveSkip confirmation prompt
-var="key=value"Override a variable
-var-file="file.tfvars"Use a variable definition file
-target=resourceApply only specific resource
-replace=resourceForce recreation of resource
-lock-timeout=10mWait up to 10m for state lock
-parallelism=10Limit concurrent operations (default 10)
-refresh=falseSkip state refresh (faster but riskier)

Examples

Apply saved plan:

bash
$ terraform apply tfplan

Apply with auto-approval (CI/CD):

bash
$ terraform apply -auto-approve

Force recreation of a resource:

bash
$ terraform apply -replace="aws_instance.example"

Apply only specific resource:

bash
$ terraform apply -target=aws_instance.example

Apply with custom parallelism:

bash
$ terraform apply -parallelism=20

Apply with lock timeout:

bash
$ terraform apply -lock-timeout=5m

terraform destroy — Destroy Infrastructure

Removes all resources managed by the configuration.

Basic Usage

bash
$ 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

FlagPurpose
-auto-approveSkip confirmation prompt
-target=resourceDestroy only specific resource
-var="key=value"Override a variable
-var-file="file.tfvars"Use a variable definition file
-lock-timeout=10mWait up to 10m for state lock
-parallelism=10Limit concurrent operations

Examples

Destroy with auto-approval:

bash
$ terraform destroy -auto-approve

Destroy only specific resource:

bash
$ terraform destroy -target=aws_instance.example

Destroy with environment variables:

bash
$ terraform destroy -var="environment=dev" -var-file="dev.tfvars"

Quick Reference — Most Used Commands

TaskCommand
Preview changesterraform plan
Save plan to fileterraform plan -out=tfplan
Plan with variablesterraform plan -var-file="prod.tfvars"
Preview destructionterraform plan -destroy
Apply changesterraform apply
Apply saved planterraform apply tfplan
Apply with auto-approveterraform apply -auto-approve
Apply with variablesterraform apply -var-file="prod.tfvars"
Force recreate resourceterraform apply -replace="resource.type.name"
Destroy all resourcesterraform destroy
Destroy with auto-approveterraform destroy -auto-approve
Destroy specific resourceterraform destroy -target=resource.type.name

Pro Tips

  1. Always plan before apply — avoid surprises in production.

  2. Save plans for production — terraform plan -out=prod.tfplan then terraform apply prod.tfplan ensures the exact planned changes are applied.

  3. Use -auto-approve only in CI/CD — never in interactive development.

  4. Use -refresh=false only when you need speed — state may become stale.

  5. Use -target carefully — changes to targeted resources may affect others via dependencies.

  6. -parallelism tuning — reduce if hitting AWS API rate limits, increase if you want faster operations.

  7. Exit codes for automation:

  • 0 = Success, no changes
  • 1 = Error
  • 2 = Success with changes

Next in TerraformState Management
Terraform cheat sheet: Planning & Applying | InfraThrone DevOps Park