Terraform commands used in DevOps.
1. Core Terraform Commands (Daily use)
1) terraform init
Purpose:
Initializes the project, downloads providers, sets up backend.
When used:
First time in a project
After changing providers or backend
terraform init
2) terraform plan
Purpose:
Shows what Terraform will create, change, or delete (dry run).
Why important in DevOps:
Used in CI pipeline
Used for approvals before apply
terraform plan
With variables:
terraform plan -var-file="dev.tfvars"
3) terraform apply
Purpose:
Creates or updates infrastructure.
terraform apply
Auto approve (used in CI/CD carefully):
terraform apply -auto-approve
4) terraform destroy
Purpose:
Deletes all infrastructure created by Terraform.
terraform destroy
Auto approve:
terraform destroy -auto-approve
2. Validation and Formatting (Code quality)
5) terraform validate
Purpose:
Checks syntax and basic config errors.
terraform validate
Used in CI to catch errors early.
6) terraform fmt
Purpose:
Auto formats Terraform code (clean code style).
terraform fmt
Recursive formatting:
terraform fmt -recursive
3. State Management Commands (Very important in DevOps)
7) terraform state list
Shows all resources Terraform is managing.
terraform state list
8) terraform state show
Shows details of a specific resource from state.
terraform state show aws_instance.web
9) terraform state mv
Move resource in state (rename resource without recreating infra).
terraform state mv aws_instance.old aws_instance.new
Used during refactoring.
10) terraform state rm
Remove resource from state without deleting actual infra.
terraform state rm aws_instance.web
Used when infra is managed manually or by another tool.
4. Advanced Lifecycle Commands
11) terraform refresh
Purpose:
Sync Terraform state with real cloud infra.
terraform refresh
(Used less now, often replaced by plan/apply behavior)
12) terraform taint (Deprecated in new versions but still seen)
Marks resource for recreation on next apply.
terraform taint aws_instance.web
terraform apply
13) terraform untaint
terraform untaint aws_instance.web
5. Workspace Commands (Multi environment: dev, qa, prod)
14) terraform workspace list
terraform workspace list
15) terraform workspace new
terraform workspace new dev
terraform workspace new prod
16) terraform workspace select
terraform workspace select dev
Used for environment separation.
6. Import & Debugging Commands
17) terraform import
Import existing cloud resource into Terraform state.
terraform import aws_instance.web i-0abc12345def
Used when infra already exists.
18) terraform output
Show output values.
terraform output
terraform output public_ip
19) terraform providers
Show providers used in the project.
terraform providers
20) terraform version
terraform version
Used in debugging CI/CD issues.
7. Terraform Commands used in CI/CD (Industry pipelines)
Typical DevOps pipeline flow:
terraform init
terraform fmt -check
terraform validate
terraform plan
terraform apply -auto-approve
For destroy pipeline:
terraform destroy -auto-approve
8. Real Industry Workflow (Example)
Developer pushes Terraform code
CI Pipeline runs:
terraform initterraform fmt -checkterraform validateterraform plan
Manager approves plan
Then CD pipeline runs:
terraform apply
9. Most Important Commands for Interview
If interviewer asks “Which Terraform commands do you use daily?”
You should say:
terraform init
terraform plan
terraform apply
terraform destroy
terraform validate
terraform fmt
terraform state list
terraform import
terraform workspace
10. One-line summary (Interview ready)
In DevOps, Terraform commands like init, plan, apply, validate, fmt, state, import, and workspace are used to provision, manage, validate, and automate infrastructure changes safely through CI/CD pipelines.
11. Quick Cheat Sheet
terraform init -> setup project
terraform plan -> preview changes
terraform apply -> create/update infra
terraform destroy -> delete infra
terraform validate -> config check
terraform fmt -> format code
terraform state list -> list resources
terraform import -> bring existing infra
terraform workspace -> manage envs
terraform output -> show outputs