Terraform Commands Reference Guide
Core Workflow Commands
terraform init
When: First command in any Terraform directory
Where: Root of Terraform configuration directory
How: terraform init [options]
Description: Initializes working directory, downloads providers, initializes backend
Common options:
-upgrade
- Upgrade modules and plugins-reconfigure
- Reconfigure backend ignoring existing configuration-backend-config=path
- Specify backend configuration file
terraform plan
When: Before applying changes to see what will happen
Where: Root of configuration directory
How: terraform plan [options]
Description: Creates execution plan showing what actions Terraform will take
Common options:
-out=path
- Save plan to file-var="key=value"
- Set variable values-var-file=path
- Load variables from file-target=resource
- Focus on specific resource-destroy
- Plan to destroy all resources
terraform apply
When: To create/modify infrastructure
Where: Root of configuration directory
How: terraform apply [options] [plan]
Description: Executes planned changes to reach desired state
Common options:
-auto-approve
- Skip interactive approval-var="key=value"
- Set variable values-target=resource
- Apply to specific resource onlyplanfile
- Apply previously saved plan
terraform destroy
When: To remove all managed infrastructure
Where: Root of configuration directory
How: terraform destroy [options]
Description: Destroys all resources managed by configuration
Common options:
-auto-approve
- Skip confirmation prompt-target=resource
- Destroy specific resource only
State Management Commands
terraform state list
When: To see all resources in state
Where: Directory with terraform state
How: terraform state list [options]
Description: Lists all resources tracked in state file
terraform state show
When: To inspect specific resource details
Where: Directory with terraform state
How: terraform state show [resource_address]
Description: Shows detailed information about a resource
terraform state mv
When: Renaming resources or moving between modules
Where: Directory with terraform state
How: terraform state mv [source] [destination]
Description: Moves resource from one address to another
terraform state rm
When: Remove resource from state without destroying
Where: Directory with terraform state
How: terraform state rm [resource_address]
Description: Removes resource from state file
terraform state pull
When: Get current state from remote backend
Where: Directory with remote state
How: terraform state pull
Description: Downloads and outputs current state
terraform state push
When: Upload local state to remote backend
Where: Directory with local state file
How: terraform state push [path]
Description: Uploads local state file to remote backend
Import and Data Commands
terraform import
When: Bring existing AWS resources under Terraform management
Where: Directory with resource configuration
How: terraform import [resource_address] [resource_id]
Description: Associates existing infrastructure with Terraform resource
AWS Examples:
terraform import aws_instance.web i-1234567890abcdef0
terraform import aws_s3_bucket.bucket my-bucket-name
terraform refresh
When: Update state with real infrastructure (deprecated, use plan -refresh-only)
Where: Directory with terraform state
How: terraform refresh [options]
Description: Updates state file with current real infrastructure
terraform plan -refresh-only
When: Check for drift between state and actual resources
Where: Directory with terraform configuration
How: terraform plan -refresh-only
Description: Compares state with real infrastructure, suggests updates
Validation and Testing Commands
terraform validate
When: Check configuration syntax and internal consistency
Where: Directory with .tf files
How: terraform validate [options]
Description: Validates configuration files for syntax errors
terraform fmt
When: Format configuration files consistently
Where: Directory with .tf files
How: terraform fmt [options]
Description: Formats Terraform configuration files
Common options:
-recursive
- Format files in subdirectories-diff
- Show formatting changes-check
- Check if files are formatted
terraform test
When: Run configuration tests
Where: Directory with .tftest.hcl files
How: terraform test [options]
Description: Runs tests defined in test files
Provider and Module Commands
terraform providers
When: List provider requirements
Where: Directory with terraform configuration
How: terraform providers [subcommand]
Description: Shows provider dependency information
Subcommands:
terraform providers lock
- Create provider lock fileterraform providers mirror
- Save providers to local directoryterraform providers schema
- Show provider schemas
terraform get
When: Download/update modules
Where: Directory with module references
How: terraform get [options]
Description: Downloads modules referenced in configuration
Common options:
-update
- Update modules to latest versions
Output and Information Commands
terraform output
When: Get output values from state
Where: Directory with terraform state
How: terraform output [options] [name]
Description: Displays output values from configuration
Common options:
-json
- Output in JSON format-raw
- Output raw value without quotes
terraform show
When: Display current state or plan in human-readable format
Where: Directory with state or plan file
How: terraform show [options] [path]
Description: Shows current state or saved plan
Common options:
-json
- Output in JSON format
terraform version
When: Check Terraform version
Where: Anywhere
How: terraform version
Description: Shows Terraform version and provider versions
Workspace Commands
terraform workspace new
When: Create new workspace for environment isolation
Where: Directory with terraform configuration
How: terraform workspace new [name]
Description: Creates new workspace
terraform workspace select
When: Switch between workspaces
Where: Directory with terraform configuration
How: terraform workspace select [name]
Description: Switches to specified workspace
terraform workspace list
When: See available workspaces
Where: Directory with terraform configuration
How: terraform workspace list
Description: Lists all available workspaces
terraform workspace delete
When: Remove unused workspace
Where: Directory with terraform configuration
How: terraform workspace delete [name]
Description: Deletes specified workspace
Advanced and Troubleshooting Commands
terraform force-unlock
When: Remove stuck state lock
Where: Directory with locked state
How: terraform force-unlock [lock_id]
Description: Forcibly removes state lock (use cautiously)
terraform taint
When: Mark resource for recreation on next apply
Where: Directory with terraform state
How: terraform taint [resource_address]
Description: Marks resource as tainted, forcing recreation
terraform untaint
When: Remove taint from resource
Where: Directory with terraform state
How: terraform untaint [resource_address]
Description: Removes taint from resource
terraform console
When: Interactive console for testing expressions
Where: Directory with terraform configuration
How: terraform console [options]
Description: Interactive console for Terraform expressions
AWS-Specific Usage Patterns
Common AWS Resource Import Examples
# EC2 Instance
terraform import aws_instance.example i-1234567890abcdef0
# S3 Bucket
terraform import aws_s3_bucket.example bucket-name
# VPC
terraform import aws_vpc.example vpc-12345678
# Security Group
terraform import aws_security_group.example sg-12345678
# IAM Role
terraform import aws_iam_role.example role-name
Environment-based Commands
# Development environment
terraform workspace select dev
terraform plan -var-file="dev.tfvars"
terraform apply -var-file="dev.tfvars"
# Production environment
terraform workspace select prod
terraform plan -var-file="prod.tfvars"
terraform apply -var-file="prod.tfvars"
Quick Reference Cheatsheet
Daily Workflow
terraform init
- Initialize directoryterraform plan
- Review changesterraform apply
- Apply changesterraform show
- Verify state
Troubleshooting Workflow
terraform validate
- Check syntaxterraform plan -refresh-only
- Check driftterraform state list
- See resourcesterraform state show [resource]
- Inspect resource
State Management Workflow
terraform state list
- List resourcesterraform state mv
- Move resourcesterraform import
- Import existing resourcesterraform state rm
- Remove from state
Multi-Environment Workflow
terraform workspace new [env]
- Create environmentterraform workspace select [env]
- Switch environmentterraform apply -var-file="[env].tfvars"
- Deploy to environment
Note: Always run terraform plan
before terraform apply
to review changes. Use version control for your Terraform configurations and consider remote state for team collaboration.