Sachin Chaurasiya

Toolbox · Infrastructure as Code

Terraform Commands

Plan/apply workflow, state operations, targeting, imports and drift checks.

Last reviewed
2026-09-18
On this page

Workflow

CommandPurpose
terraform init -backend-config=backend.hcl -upgradeInit with backend, upgrade providers
terraform fmt -recursive -checkFormatting gate
terraform validateSyntax/semantic check
terraform plan -out=tfplanSaved plan
terraform show -json tfplan > plan.jsonMachine-readable plan for policy checks
terraform apply tfplanApply exactly what was reviewed
terraform plan -detailed-exitcodeExit 2 when changes exist (drift detection)

State

CommandPurpose
terraform state listResources in state
terraform state show aws_s3_bucket.logsAttributes
terraform state mv old.name new.nameRename without recreate
terraform state rm module.legacyCaution forget a resource in shared state (does not delete it)
terraform import aws_s3_bucket.logs my-bucketAdopt existing resource
terraform force-unlock <id>Caution clear a stale lock; be sure no apply is running

Targeting and troubleshooting

CommandPurpose
terraform plan -target=module.networkLimit scope (temporary use only)
terraform apply -replace=aws_instance.webDestructive destroy and recreate the resource
terraform taint aws_instance.webLegacy (deprecated since 0.15.2); use -replace
TF_LOG=DEBUG terraform planProvider debug logs
terraform providers lock -platform=linux_amd64Lock hashes for CI platform

Workspaces and outputs

CommandPurpose
terraform workspace list / select prodWorkspaces
terraform output -jsonOutputs for scripts
terraform graph | dot -Tsvg > graph.svgDependency graph

See also