CloudNetworking.io Terraform, AWS, DevOps & Cloud Architecture Guides
Terraform CLI + IaC Automation + DevOps Workflow

Terraform Commands Explained

Terraform commands are the operational interface between your infrastructure code and real cloud infrastructure. Commands like terraform init, terraform plan, terraform apply, and terraform destroy control how configuration is initialized, reviewed, deployed, updated, and removed.

This guide explains Terraform commands in a real DevOps style: what each command does, when to use it, what it changes, how state is involved, how providers interact with AWS/Azure/GCP/Kubernetes, and how teams safely use Terraform in CI/CD pipelines.

Command flow at a glance

Most Terraform workflows follow a safe repeatable loop: initialize, validate, preview, apply, inspect outputs, and manage state carefully.

initDownload providers and configure backend
planPreview infrastructure changes before applying
applyCreate, update, or delete real resources
stateTrack real infrastructure metadata
Architecture diagram

Terraform command architecture diagram

The diagram below shows how Terraform commands connect configuration files, variables, provider plugins, state files, remote backends, real infrastructure, output values, and CI/CD consumers.

Terraform command architecture diagram showing Terraform configuration, variables, backend configuration, Terraform CLI commands init plan apply destroy, providers, state backend, real cloud infrastructure, outputs, consumers, and DevOps workflow
Terraform CLI reads configuration, talks to providers, compares desired configuration with state, updates real infrastructure, and stores metadata in the state backend.
Terraform CLI terraform init terraform plan terraform apply Terraform State Remote Backend Cloud Providers
01

Commands are the workflow

Terraform commands are how engineers initialize, validate, preview, apply, inspect, and safely manage infrastructure changes.

02

State is central

Terraform uses state to map your configuration to real resources, so state commands must be handled carefully.

03

Providers do the work

Terraform CLI uses provider plugins to talk to AWS, Azure, GCP, Kubernetes, and many other platforms.

04

CI/CD needs guardrails

In pipelines, plan reviews, approvals, locking, and backend design are critical for safe infrastructure delivery.

What are Terraform commands?

What are Terraform commands?

Terraform commands are CLI operations used to manage the full lifecycle of infrastructure as code. They tell Terraform when to initialize a working directory, validate code, calculate a change plan, apply infrastructure changes, destroy resources, inspect outputs, and manage state.

In simple terms, your Terraform files describe the desired infrastructure, but Terraform commands are what make the workflow happen. Without commands, the configuration is only text. With commands, Terraform can compare the desired state against the current state and create an execution plan.

  • terraform init prepares the working directory.
  • terraform validate checks syntax and internal consistency.
  • terraform fmt formats Terraform files.
  • terraform plan previews changes.
  • terraform apply executes changes.
  • terraform destroy removes managed infrastructure.
  • terraform state inspects or manipulates state carefully.

Simple definition

Terraform commands are the CLI instructions used to convert Terraform configuration into controlled infrastructure actions.

Best mental model

Configuration is the design. Providers are the cloud connectors. State is the memory. Commands are the workflow engine.

Why commands matter

Why Terraform commands matter

Safety

Commands like plan and validate help teams review before making real cloud changes.

Repeatability

The same commands can run locally or in CI/CD, making infrastructure delivery consistent.

Automation

Terraform commands are pipeline-friendly, allowing controlled infrastructure provisioning and updates.

Terraform workflow

Recommended Terraform command workflow

Common local workflow
terraform init
terraform fmt
terraform validate
terraform plan
terraform apply
terraform output
Safer CI/CD workflow
terraform init
terraform fmt -check
terraform validate
terraform plan -out=tfplan

# Manual approval / pull request review

terraform apply tfplan
Core commands

Terraform commands cheat sheet

CommandPurposeTypical usage
terraform initInitializes working directory, providers, modules, backendFirst command in any Terraform folder
terraform fmtFormats Terraform configuration filesBefore commits and in CI checks
terraform validateValidates syntax and configuration consistencyBefore plan
terraform planShows proposed infrastructure changesBefore apply and during pull requests
terraform applyApplies the planned changesAfter review and approval
terraform destroyDestroys Terraform-managed resourcesTemporary environments or cleanup
terraform outputDisplays output valuesAfter apply or for downstream use
terraform stateInspects or modifies stateAdvanced troubleshooting
terraform importImports existing infrastructure into stateOnboarding unmanaged resources
terraform graphGenerates dependency graphArchitecture and dependency analysis
terraform init

terraform init

terraform init prepares the working directory. It downloads required providers, initializes modules, and configures the backend used for state storage.

terraform init examples
terraform init
terraform init -upgrade
terraform init -reconfigure
terraform init -backend-config=backend.hcl
terraform plan

terraform plan

terraform plan compares your desired configuration with the current state and shows what Terraform intends to create, update, or destroy.

terraform plan examples
terraform plan
terraform plan -var-file=dev.tfvars
terraform plan -out=tfplan
terraform show tfplan
terraform apply

terraform apply

terraform apply executes the changes described by a plan. This is the command that creates, updates, or deletes real infrastructure.

terraform apply examples
terraform apply
terraform apply tfplan
terraform apply -auto-approve
Recommendation: Avoid -auto-approve for sensitive environments unless the pipeline has strong review and approval gates.
terraform destroy

terraform destroy

terraform destroy removes resources managed by the current Terraform state. It is powerful and should be controlled carefully.

terraform destroy examples
terraform destroy
terraform destroy -var-file=dev.tfvars
terraform plan -destroy
State commands

Terraform state commands

Terraform state is the metadata record that maps your Terraform configuration to real infrastructure. State commands are useful, but should be used with care.

CommandUse
terraform state listList resources tracked in state
terraform state showShow detailed information about one resource
terraform state mvMove state address when refactoring
terraform state rmRemove resource from state without deleting real infrastructure
terraform importBring existing infrastructure under Terraform management
DevOps usage

Terraform commands in DevOps pipelines

Pull request workflow

  • Run terraform fmt -check
  • Run terraform validate
  • Run terraform plan
  • Attach plan output to pull request
  • Require approval before apply

Deployment workflow

  • Use remote backend and locking
  • Apply only reviewed plans
  • Separate workspaces or folders carefully
  • Restrict destroy permissions
  • Keep secrets out of state and logs
Best practices

Terraform commands best practices

Always review plan

Do not run apply blindly. Plan output is the safety checkpoint before real infrastructure changes.

Use remote state

Remote backends improve collaboration, locking, and state safety compared to local-only state.

Automate validation

Run fmt, validate, and plan in CI/CD so issues are caught before merge or apply.

Protect destroy

Destroy should be gated carefully, especially for shared or important environments.

Version providers

Pin provider versions to reduce unexpected changes during init or upgrade.

Store plans carefully

Saved plan files can include sensitive data, so treat them carefully in pipelines.

Common mistakes

Common Terraform command mistakes

Skipping plan review

Running apply without reviewing planned changes can delete or alter resources unexpectedly.

Using local state in teams

Local state creates collaboration problems and increases the risk of drift or state loss.

Wrong workspace or var-file

Applying with the wrong environment values is one of the most dangerous Terraform mistakes.

Manual cloud changes

Manual changes outside Terraform create drift and make plans harder to understand.

Unsafe state edits

State commands are powerful. A wrong state change can disconnect Terraform from real infrastructure.

No locking

Concurrent applies without locking can corrupt state or create unpredictable infrastructure changes.

Troubleshooting

Terraform commands troubleshooting guide

IssueLikely causeFix direction
init failsProvider download, backend config, credentials, network issueCheck backend config, provider version, credentials, and internet/proxy access
plan shows unexpected changesDrift, provider behavior, wrong variablesCheck state, var-file, workspace, and manual cloud changes
apply fails halfwayCloud API error, quota, permissions, dependency issueFix root error, rerun plan, and verify state consistency
state lock stuckInterrupted apply or backend lock issueInvestigate carefully before force-unlock
destroy wants to remove too muchWrong folder/workspace/stateStop immediately and verify backend, workspace, and plan output
FAQ

Terraform commands FAQ

What are the most important Terraform commands?
The most important commands are terraform init, terraform fmt, terraform validate, terraform plan, terraform apply, terraform destroy, terraform output, and terraform state.
Should I run terraform apply without terraform plan?
It is safer to review the plan first. In pipelines, save the plan and apply the reviewed plan file.
Why is Terraform state important?
State maps Terraform configuration to real infrastructure resources. Without accurate state, Terraform cannot reliably know what it manages.
Is terraform destroy dangerous?
Yes. It removes resources tracked by Terraform state. Use approvals, environment checks, and restricted permissions before allowing destroy.