Terraform Command Reference
Essential Terraform commands for managing infrastructure as code.
Core Workflow
# Initialize — download providers and modules
terraform init
# Preview changes
terraform plan
# Apply changes
terraform apply
# Destroy resources
terraform destroy
# Apply without confirmation prompt
terraform apply -auto-approve
terraform destroy -auto-approve
Working with Variables
# Pass variable on command line
terraform apply -var="region=us-east-1"
# Use a variables file
terraform apply -var-file="terraform.tfvars"
# Enable verbose logging
export TF_LOG=TRACE
terraform apply
# Disable verbose logging
export TF_LOG=
Run from Any Directory
# -chdir changes to the specified directory before running
terraform -chdir=/path/to/config apply
State Management
# Show current state
terraform show
# List resources in state
terraform state list
# Show a specific resource
terraform state show aws_instance.web
# Remove a resource from state (without destroying)
terraform state rm aws_instance.web
# Import existing resource into state
terraform import aws_instance.web i-1234567890abcdef0
Workspaces
# Create a new workspace
terraform workspace create staging
# List workspaces
terraform workspace list
# Switch workspace
terraform workspace select production
# Delete a workspace
terraform workspace delete staging
Formatting and Validation
# Format code
terraform fmt
# Validate configuration syntax
terraform validate
# Show output values
terraform output
terraform output <output-name>