Skip to content

Terraform Commands

General Help

terraform -help
terraform fmt -help
terraform version

Formatting and Validation

terraform fmt
terraform fmt --recursive
terraform fmt --diff
terraform fmt --check
terraform validate

Init and Plan

terraform init
terraform init -input=false
terraform init -migrate-state
terraform plan
terraform plan -out=tfplan
terraform plan -destroy

Apply and Destroy

terraform apply
terraform apply -auto-approve
terraform apply tfplan
terraform destroy
terraform destroy -auto-approve

State Operations

terraform show
terraform state list
terraform state mv aws_instance.old aws_instance.new
terraform state pull > state.tfstate
terraform state rm aws_instance.old
terraform refresh

Workspaces and Outputs

terraform workspace list
terraform workspace new dev
terraform workspace select dev
terraform output
terraform output -json
terraform output bucket_name

Other Useful Commands

terraform providers
terraform graph
terraform console
terraform force-unlock <lock_id>
terraform login
terraform logout

Practical Notes

  • Use fmt and validate before review.
  • Use plan to inspect the change set before apply.
  • Use state commands carefully; they directly affect Terraform's view of the world.