
Terraform CLI Commands Explained with Real-World Examples
Terraform is one of the most popular Infrastructure as Code (IaC) tools used by DevOps engineers and cloud teams to provision, manage, and scale infrastructure reliably. Understanding Terraform CLI commands is essential for working safely with cloud resources in real-world environments.
This guide explains the most important Terraform CLI commands with practical, real-time examples, following SEO best practices such as a single H1, clean heading hierarchy, and optimized structure.
terraform init
Purpose
terraform init initializes a Terraform working directory. It downloads required provider plugins, initializes modules, and configures the backend where state files are stored.
Real-World Example
You create a new project to deploy AWS infrastructure:
terraform init
Terraform will:
- Download the AWS provider
- Initialize the S3 backend (if configured)
- Prepare the directory for further commands
This command must be run first before planning or applying changes.
terraform plan
Purpose
terraform plan shows what Terraform will change without actually modifying infrastructure. It is used to review planned actions such as resource creation, updates, or deletion.
Real-World Example
After adding an EC2 instance resource, run:
terraform plan
Terraform displays a detailed execution plan:
- Resources to be created
- Attributes to be modified
- Resources to be destroyed
In CI/CD pipelines, plans are often saved:
terraform plan -out=prod.tfplan
terraform apply
Purpose
terraform apply executes the planned changes and applies them to real cloud infrastructure.
Real-World Example
Deploy infrastructure using a saved plan:
terraform apply prod.tfplan
Terraform will:
- Create or update resources
- Store results in the state file
- Output defined output values
This approach ensures safer deployments in production environments.
terraform show
Purpose
terraform show displays the current Terraform state or the details of a saved plan file.
Real-World Example
View the current state:
terraform show
For automation and integrations:
terraform show -json
This is commonly used by monitoring tools and scripts to extract infrastructure details.
terraform destroy
Purpose
terraform destroy removes all resources managed by Terraform in the current workspace.
Real-World Example
Delete a temporary testing environment:
terraform destroy
⚠️ This action is irreversible and should be reviewed carefully before execution.
terraform validate
Purpose
terraform validate checks Terraform configuration files for syntax and structural correctness.
Real-World Example
Before committing code:
terraform validate
This helps catch errors early without interacting with cloud providers.
terraform output
Purpose
terraform output displays values defined in output blocks, such as IP addresses or URLs.
Real-World Example
View all outputs:
terraform output
Retrieve a specific value:
terraform output alb_dns
For automation:
terraform output -json
terraform state
Purpose
terraform state allows inspection and modification of the Terraform state file.
Common Commands
List managed resources:
terraform state list
Show resource details:
terraform state show aws_instance.web
Move a resource:
terraform state mv aws_instance.old aws_instance.new
State commands should be used carefully because state integrity is critical.
terraform fmt
Purpose
terraform fmt formats Terraform configuration files according to standard style conventions.
Real-World Example
Format all files:
terraform fmt
Check formatting in CI:
terraform fmt -check
This ensures consistent and readable code across teams.
terraform graph
Purpose
terraform graph generates a visual representation of resource dependencies.
Real-World Example
Generate a PNG diagram:
terraform graph | dot -Tpng > graph.png
This is useful for architecture documentation and onboarding new team members.
terraform import
Purpose
terraform import brings existing infrastructure under Terraform management.
Real-World Example
Import an existing VPC:
terraform import aws_vpc.main vpc-0a12bc34d56ef7890
The resource block must already exist in the configuration file.
terraform refresh
Purpose
terraform refresh updates the state file to match real infrastructure.
Real-World Example
Sync state after manual changes:
terraform refresh
Modern Terraform versions run refresh automatically during planning.
terraform taint
Purpose
terraform taint marks a resource for recreation during the next apply.
Real-World Example
Force recreation of an unhealthy instance:
terraform taint aws_instance.web
The resource will be destroyed and recreated on the next apply.
terraform untaint
Purpose
terraform untaint removes the taint flag from a resource.
Real-World Example
Cancel forced recreation:
terraform untaint aws_instance.web
This ensures the resource remains unchanged.
Final Thoughts
Mastering Terraform CLI commands is essential for building reliable, scalable, and maintainable infrastructure. By understanding how and when to use each command, teams can avoid costly mistakes, automate deployments, and maintain full control over cloud environments.
This guide serves as a practical reference for beginners and experienced DevOps engineers alike.
📌 Summary Table
| Command | Purpose |
|---|---|
init | Initialize working directory & providers |
plan | Preview changes |
apply | Deploy changes |
show | Show state/plan details |
destroy | Remove infrastructure |
validate | Check configuration syntax |
output | Display output variables |
state | Manage/inspect state |
fmt | Format Terraform code |
graph | Visualize resource dependencies |
import | Bring existing infra under Terraform |
refresh | Sync state with real infra |
taint | Force recreate a resource |
untaint | Cancel forced recreation |
Useful Terraform Resources
To deepen your understanding of Terraform and stay up to date with best practices, refer to the official and community resources below:
- Terraform Official Documentation – Comprehensive reference for all Terraform commands and features:
https://developer.hashicorp.com/terraform/docs - Terraform CLI Command Reference – Detailed explanations of every CLI command and flag:
https://developer.hashicorp.com/terraform/cli - HashiCorp Learn – Terraform Tutorials – Hands-on labs and real-world guided examples:
- Terraform Registry – Providers, modules, and reusable infrastructure components:
https://registry.terraform.io - Terraform Best Practices – Recommended patterns for production-grade IaC:
https://developer.hashicorp.com/terraform/cloud-docs/recommended-practices