Logo
CloudWithSingh
Back to all field notes
Cheatsheet
Terraform
Intermediate

Terraform CLI Field Notes

Every Terraform command you need, organized by workflow stage. A practical reference for DevOps engineers and cloud practitioners.

Parveen Singh
February 4, 2026
7 min read
42 commands
Prerequisites:Basic terminal/CLI usageUnderstanding of infrastructure as code concepts
TLDR

Terraform is HashiCorp's Infrastructure as Code tool for defining cloud resources in declarative configuration files. This reference covers every CLI command organized by workflow stage: init, validate, plan, apply, state management, and destroy.

🧭 The Terraform Workflow

Every Terraform project follows this lifecycle:

initvalidateplanapplystatedestroy

Think of it like building a house: you prep the site (init), check the blueprints (validate), preview what you're building (plan), actually build it (apply), keep track of what exists (state), and tear it down when you're done (destroy).

🚀 Getting Started

These commands help you confirm Terraform is installed and discover what's available.

CommandWhat It Does
terraform versionShows your installed Terraform version
terraform helpLists all available commands
terraform -helpSame as above — your go-to when you're stuck
When to Use

Right after installing Terraform, or when you forget a command name.

📦 Initialization

Before Terraform can do anything, it needs to download providers and set up your working directory.

CommandWhat It Does
terraform initDownloads providers & modules, sets up your project — run this first
terraform init -upgradeUpdates providers and modules to latest compatible versions
terraform init -reconfigureResets backend config (useful when switching environments)
terraform init -backend=falseInitializes without connecting to a remote backend
terraform init -migrate-stateMoves your state file to a new backend
Pro Tip

Use tfenv to manage multiple Terraform versions across projects. Install it with brew install tfenv.

Introduction to Terraform on Azure - Cloudlearn.io
Hands-on Lab

Introduction to Terraform on Azure - Cloudlearn.io

Learn the basics of Infrastructure as Code with Terraform by deploying an Azure storage account. Master essential commands and configuration concepts.

cloudlearn.ioStart Learning

✅ Validation and Formatting

Catch errors before they cost you time.

CommandWhat It Does
terraform validateChecks .tf files for syntax errors — run before every plan
terraform validate -jsonOutputs validation results in JSON (great for CI/CD)
terraform fmtAuto-formats your code to standard style
terraform fmt -recursiveFormats all .tf files in subdirectories too
terraform fmt -checkTells you if files need formatting — without changing them
terraform fmt -diffShows exactly what formatting changes would be made
When to Use

Before every commit. In real teams, fmt -check runs in your CI pipeline to enforce style.

🔮 Planning

This is Terraform's superpower — see exactly what will happen before anything changes.

CommandWhat It Does
terraform planShows what Terraform will do — your "dry run"
terraform plan -out=tfplanSaves the plan to a file for exact execution later
terraform plan -var="key=value"Pass a variable directly
terraform plan -var-file=vars.tfvarsUse a variables file — best practice
terraform plan -target=resource.namePlan for one specific resource only
terraform plan -destroyPreview what a destroy would remove
terraform plan -refresh=falseSkip checking real infrastructure state
Warning

Always review terraform plan output before applying. A careless apply on production can cause real damage.

🏗️ Apply

This is where Terraform actually creates, updates, or modifies your cloud infrastructure.

CommandWhat It Does
terraform applyApplies changes (asks for confirmation)
terraform apply tfplanApplies a previously saved plan file — safest approach
terraform apply -auto-approveSkips confirmation — use in CI/CD only
terraform apply -var-file=vars.tfvarsApply using a specific variables file
terraform apply -target=resource.nameApply changes to one specific resource
Gotcha

-auto-approve is powerful but dangerous. In production, always use saved plan files instead of auto-approve.

🗑️ Destroy

Remove infrastructure Terraform is managing. Essential for dev/test to avoid surprise cloud bills.

CommandWhat It Does
terraform destroyDestroys all managed resources (asks for confirmation)
terraform destroy -auto-approveDestroys without asking — use with extreme caution
terraform destroy -target=resource.nameDestroy only a specific resource
Pro Tip

Always destroy lab and test resources when you're done. Forgotten resources lead to surprise cloud bills.

🗄️ State Management

Terraform keeps a state file mapping your code to real cloud resources.

CommandWhat It Does
terraform state listShows all resources Terraform is managing
terraform state show <resource>Displays full details of a specific resource
terraform state pullDownloads state from remote backend
terraform state pushUploads local state to remote backend
terraform state mv <old> <new>Rename or move a resource in state
terraform state rm <resource>Removes a resource from state — Terraform "forgets" it
terraform state replace-provider <old> <new>Swap providers in state
When to Use

Troubleshooting drift, refactoring code, or when imports go wrong.

🗄️ Workspaces

Use the same Terraform code for different environments with separate state files.

CommandWhat It Does
terraform workspace listShows all workspaces
terraform workspace showDisplays current workspace name
terraform workspace new <name>Creates a new workspace
terraform workspace select <name>Switches to a different workspace
terraform workspace delete <name>Deletes a workspace

🔌 Providers and Modules

Providers connect Terraform to cloud platforms. Modules are reusable building blocks.

CommandWhat It Does
terraform providersLists required providers
terraform providers mirror <dir>Downloads providers locally
terraform getDownloads modules in your config
terraform get -updateRe-downloads latest module versions

📥 Import

Already built something manually? Import lets Terraform start managing it.

CommandWhat It Does
terraform import <resource_type.name> <cloud_id>Links an existing cloud resource to your Terraform code
Warning

Import only updates the state file. You still need to write the matching .tf code manually.

🔄 Refresh and Show

Keep your state in sync with what actually exists in the cloud.

CommandWhat It Does
terraform refreshUpdates state to match real infrastructure
terraform showDisplays current state or a saved plan
terraform show -jsonSame but in JSON for scripting

🔍 Debugging

When things go wrong, logs are your best friend.

CommandWhat It Does
TF_LOG=TRACE terraform applyMaximum verbosity
TF_LOG=DEBUG terraform planDetailed debug output
TF_LOG_PATH=terraform.log terraform applySaves logs to a file
When to Use

When you get cryptic errors, provider auth failures, or state conflicts.

🎛️ Advanced Commands

You won't use these daily, but they're powerful when needed.

CommandWhat It Does
terraform taint <resource>Marks a resource for recreation on next apply
terraform untaint <resource>Removes the taint mark
terraform force-unlock <lock_id>Releases a stuck state lock
terraform graphOutputs a dependency graph in DOT format
terraform consoleOpens an interactive shell for testing expressions
terraform testRuns Terraform's built-in testing framework
Pro Tip

taint is being replaced by terraform apply -replace=<resource> in newer versions. Prefer the newer syntax.

What's Next

Bookmark this page

Save it for your next project sprint

Start a project

Apply what you just learned hands-on

Follow on Instagram

Daily cloud tips & behind-the-scenes

Try hands-on labs

Practice in a real cloud environment

Parveen Singh

Parveen Singh

Microsoft Certified Trainer & Cloud Solutions Consultant

Related Field Notes

Found this useful?

Stay in the loop

Weekly cloud insights, no spam

Subscribe

Explore CloudLearn

Hands-on labs & projects

Start Learning

Book Training

Custom cloud training for your team

Get in Touch

On this page