Terraform Commands

Terraform CLI commands for infrastructure as code.

Core Workflow

terraform init

Initialize working directory

terraform plan

Show execution plan

terraform apply

Apply configuration changes

terraform apply -auto-approve

Apply without confirmation

terraform destroy

Destroy managed infrastructure

terraform destroy -auto-approve

Destroy without confirmation

terraform validate

Validate configuration syntax

State Management

terraform state list

List resources in state

terraform state show resource.name

Show resource details from state

terraform state pull

Pull current state

terraform state mv source dest

Move resource in state

terraform state rm resource.name

Remove resource from state

terraform state replace-provider old new

Replace provider in state

terraform refresh

Update state with real infrastructure

Workspaces

terraform workspace list

List workspaces

terraform workspace show

Show current workspace

terraform workspace new dev

Create new workspace

terraform workspace select dev

Switch to workspace

terraform workspace delete dev

Delete workspace

Output & Variables

terraform output

Show all outputs

terraform output name

Show specific output

terraform output -json

Show outputs as JSON

terraform apply -var="key=value"

Set variable from command line

terraform apply -var-file="vars.tfvars"

Use variables file

Import & Taint

terraform import resource.name id

Import existing resource

terraform taint resource.name

Mark resource for recreation

terraform untaint resource.name

Remove taint from resource

Planning & Targeting

terraform plan -out=plan.tfplan

Save plan to file

terraform apply plan.tfplan

Apply saved plan

terraform plan -target=resource.name

Plan specific resource

terraform apply -target=resource.name

Apply specific resource

terraform plan -refresh=false

Plan without refreshing state

terraform plan -destroy

Show destroy plan

Formatting & Validation

terraform fmt

Format configuration files

terraform fmt -recursive

Format all files recursively

terraform fmt -check

Check if files are formatted

terraform validate

Validate configuration

terraform providers

Show required providers

Graph & Dependencies

terraform graph

Generate dependency graph (DOT format)

terraform graph | dot -Tpng > graph.png

Generate visual graph

terraform providers schema -json

Export provider schemas as JSON

Backend & Remote State

terraform init -backend-config=config.tfbackend

Initialize with backend config

terraform init -migrate-state

Migrate state to new backend

terraform init -reconfigure

Reconfigure backend

terraform force-unlock LOCK_ID

Force unlock state

Console & Testing

terraform console

Interactive console for expressions

terraform test

Run integration tests

terraform show

Show current state or plan

terraform show -json

Show state as JSON

terraform version

Show Terraform version

Advanced Options

terraform apply -parallelism=10

Set parallel operations limit

terraform apply -lock=false

Disable state locking

terraform apply -refresh-only

Only refresh state

terraform apply -replace=resource.name

Force replace specific resource

terraform providers lock

Update provider lock file