Terraform Commands Explained
Learn the most important Terraform commands: init , fmt , validate , plan , apply , destroy , output , and state .
Basic workflow
terraform init terraform fmt terraform validate terraform plan terraform apply
terraform init
Downloads providers, initializes backend, and prepares the working directory.
terraform init
terraform fmt
Formats Terraform files to standard style.
terraform fmt -recursive
terraform validate
Checks whether configuration syntax and structure are valid.
terraform validate
terraform plan
Shows what Terraform will create, update, or destroy.
terraform plan
terraform apply
Applies the planned infrastructure changes.
terraform apply
terraform destroy
Destroys infrastructure managed by the current configuration and state.
terraform destroy
Useful commands
terraform output terraform state list terraform workspace list terraform providers terraform version
Common mistakes
- Running apply without reviewing plan.
- Running destroy in the wrong environment.
- Not running init after changing backend/provider.
- Skipping fmt and validate in CI/CD.
Related guides
- Terraform Learning Path
- Install Terraform
Use a reviewed plan instead of an unreviewed apply
Saving the planned changes allows an approver to inspect exactly what will be applied, but the plan file may include secrets or sensitive state. Ensure there is no intervening state change. Never run terraform destroy against production to 'reset' an unexplained error.
terraform fmt -check
terraform validate
terraform plan -out=review.tfplan
terraform show review.tfplan
# Apply only after explicit approval:
# terraform apply review.tfplanRelated Melochip article: https://melochip.com/posts/terraform-state-file-explained. Examples are illustrative and have not been independently executed.
Practical guidance and verification
Why saved plans matter
The plan compares configuration and state with the provider's current information, but cannot promise that every later operation will succeed. For controlled deployment, create a saved plan with terraform plan -out=review.tfplan and apply that same artifact after reviewing the change. If configuration or cloud resources change between steps, regenerate and review the plan. Treat plan files as sensitive because they can expose secrets and infrastructure details.
Approvals and locking
Before planning, run terraform fmt -check and terraform validate. Select the intended account, region, workspace and remote state. Ensure supported state locking prevents concurrent apply operations. In CI, protect the plan artifact and limit its retention. A successful terraform apply should be followed by health checks of the affected service.
Destroy safely
A destroy request can remove important cloud resources. Review its full plan, check backups and data-retention requirements, and get explicit production approval. Routine use of targeted operations can conceal dependency effects, so reserve them for documented exceptional cases. See https://developer.hashicorp.com/terraform/cli/commands.
Join the discussion
0 CommentsAsk questions, add context, or share what worked.
More from Melochip Editorial
Learn what Playwright testing is, why modern QA teams use it, how testers can get started, and the major types...
8 min read · Updated Nov 16, 2026
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Updated Nov 16, 2026
Learn Kubernetes security best practices with RBAC, Secrets, ServiceAccounts, Pod securityContext, NetworkPoli...
6 min read · Updated Nov 16, 2026Related articles
Learn what Playwright testing is, why modern QA teams use it, how testers can get started, and the major types...
8 min read · Updated Nov 16, 2026
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Updated Nov 16, 2026
Learn Kubernetes security best practices with RBAC, Secrets, ServiceAccounts, Pod securityContext, NetworkPoli...
6 min read · Updated Nov 16, 2026
Learn Kubernetes monitoring and logs with kubectl logs, events, metrics-server, top commands, Prometheus, Graf...
6 min read · Updated Nov 16, 2026Stay in the loop. Get new Melochip articles
Receive Melochip article updates when new articles are available. Explicit opt-in only.
No comments yet. Start the discussion.