Terraform & OpenTofu CLI Commands Cheat Sheet
Production Terraform & OpenTofu CLI reference matrix covering init, plan, apply, state manipulation, targeting, workspaces, and debugging with 1-click copy.
Core Workflow (8 commands)
terraform init
Initialize working directory, download provider plugins and remote modules defined in configuration.
terraform init -upgrade
Re-evaluates provider version constraints and upgrades installed plugins to newest permitted versions.
terraform validate
Verifies configuration syntax, argument types, and internal consistency without accessing cloud APIs.
terraform fmt -recursive
Automatically rewrites all Terraform configuration files in canonical style and indentation.
terraform plan -out=tfplan.binary
Creates speculative execution plan against real infrastructure state and saves to reproducible binary file.
terraform apply tfplan.binary
Executes exact previously saved execution plan file with zero prompts, preventing drift race conditions.
terraform apply -auto-approve
Executes changes immediately without interactive confirmation prompt. Ideal for automated CI/CD pipelines.
terraform destroy -auto-approve
Destroys all managed remote infrastructure resources described in the working directory state.
State Management (6 commands)
terraform state list
Lists all resource addresses currently recorded in the Terraform remote state file.
terraform state show aws_instance.web
Displays comprehensive attribute breakdown, IPs, and metadata for a specific resource address.
terraform state rm aws_s3_bucket.legacy
Removes resource from Terraform state without destroying the actual cloud resource.
terraform state mv module.vpc.aws_vpc.main module.network.aws_vpc.main
Renames or moves resource within state file to preserve existing cloud infrastructure without recreating.
terraform state pull > state_backup.json
Downloads and displays state from remote backend (S3/GCS/Consul) to stdout.
terraform force-unlock <LOCK_ID>
Manually unlocks remote state backend after a crashed or interrupted apply execution.
Targeting & Workspaces (6 commands)
terraform apply -target=module.vpc.aws_subnet.public
Applies changes exclusively to specified resource and direct dependencies, ignoring remaining config.
terraform apply -var="environment=production" -var="instance_count=4"
Passes input variables directly from command line arguments, overriding default variable files.
terraform apply -var-file="production.tfvars"
Loads variable values from dedicated environment definition file (e.g., staging.tfvars or prod.tfvars).
terraform output -json
Extracts and formats all defined output values into machine-readable JSON for downstream automation.
terraform workspace new production
Creates and switches to a new isolated state workspace with independent infrastructure resources.
terraform workspace select staging
Switches current active workspace to existing staging environment state file.
Import & Debugging (4 commands)
terraform import aws_s3_bucket.assets my-existing-bucket-name
Imports pre-existing real-world cloud resource into Terraform state tracking without downtime.
export TF_LOG=DEBUG && terraform plan
Enables verbose low-level HTTP and RPC debugging logs to identify authentication or timeout errors.
terraform graph | dot -Tsvg > graph.svg
Generates visual directed acyclic graph (DAG) of all module and resource execution dependencies.
terraform console
Interactive evaluation shell for testing Terraform expressions, HCL functions, and interpolation.