Terraform Commands
This section provides a comprehensive reference for Terraform commands, including common use cases and best practices.
Basic Commands
Initialization and Planning
# Initialize Terraform working directory
terraform init
# Format Terraform files
terraform fmt
# Validate Terraform files
terraform validate
# Show execution plan
terraform plan
# Show execution plan with output file
terraform plan -out=tfplan
Resource Management
# Apply changes
terraform apply
# Apply changes from plan file
terraform apply tfplan
# Destroy infrastructure
terraform destroy
# Show current state
terraform show
# List resources
terraform state list
Common Use Cases
Working with State
# Move resource in state
terraform state mv SOURCE_ADDRESS DESTINATION_ADDRESS
# Remove resource from state
terraform state rm ADDRESS
# Import existing resource
terraform import ADDRESS ID
Working with Variables
# Set variable value
terraform apply -var="instance_type=t2.micro"
# Set variable file
terraform apply -var-file="prod.tfvars"
# Override variable
terraform apply -var="region=us-west-2" -var="environment=staging"
Output Management
# Show outputs
terraform output
# Show specific output
terraform output instance_ip
# Show outputs in JSON format
terraform output -json
Best Practices
- State Management: Use remote state storage
terraform { backend "s3" { bucket = "my-terraform-state" key = "prod/terraform.tfstate" region = "us-west-2" } }
- Variable Organization: Use separate variable files
# variables.tf variable "environment" { description = "Environment name" type = string } # prod.tfvars environment = "production"
- Module Usage: Organize code into modules
module "vpc" { source = "./modules/vpc" environment = var.environment cidr_block = var.vpc_cidr }
Troubleshooting
Common Issues
- State Lock Issues
# Force unlock state terraform force-unlock LOCK_ID
- Provider Issues
# Reinitialize with specific provider terraform init -upgrade -plugin-dir=/path/to/provider
- Resource Dependencies
# Show dependency graph terraform graph