Chapter 13: Troubleshooting & Debugging
The Friction
- ✕ Cryptic Errors: Error: 403 Forbidden (With no context).
- ✕ Ghost Resources: Terraform fails but resource was created.
- ✕ Dependency Loops: Circular logic preventing plan generation.
The R8way Solution
- ✓ Structured Debugging: Step-by-step guides for common failures.
- ✓ State Surgery: Runbooks for terragrunt state rm/import.
- ✓ Log Aggregation: Centralized logs for deeper analysis.
13.1 Common Issues & Solutions
State Locking Errors
Error:
The error Error: Error acquiring the state lock
occurs when another process is holding the DynamoDB lock.
Causes:
- Another operation in progress
- Previous operation crashed (stuck lock)
- CI/CD pipeline conflict
Solutions:
1. Wait 5-10 minutes (locks auto-expire)
2. Check for active operations:
- Manually release stuck lock:
Prevention:
- Use component-specific operations
- Implement lock timeout in CI/CD
- Monitor for stuck locks
Permission Denied
Error:
The error Error: AccessDenied: Access Denied
indicates insufficient IAM permissions.
Causes:
- IAM role lacks permissions
- Wrong AWS credentials
- Incorrect role ARN
Solutions:
1. Verify IAM role:
- Check permissions:
- Verify credentials:
Module Not Found
Error:
The error Error: Failed to download module
happens when Terraform cannot locate the source.
Causes:
- Module source path incorrect
- Network connectivity issues
- Module version unavailable
Solutions:
1. Verify module source:
- Re-run scaffold:
- Re-initialize:
Dependency Failures
Error:
The error Error: Error reading dependency output
occurs when a dependent module hasn't been applied.
Causes:
- Dependency not deployed
- Dependency outputs missing
- Mock outputs incorrect
Solutions:
1. Deploy dependency first:
- Check dependency outputs:
- Verify mock outputs:
Terraform Crashes
Error:
The error Error: Unexpected error occurred
suggests a provider bug or state corruption.
Causes:
- Resource conflicts
- State corruption
- Provider bugs
Solutions:
1. Check Terraform logs:
- Refresh state:
- Import missing resources:
13.2 Debugging Techniques
Verbose Logging
Enable Debug Logging:
Log Levels:
- TRACE: Most verbose
- DEBUG: Detailed
- INFO: Normal (default)
- WARN: Warnings only
- ERROR: Errors only
State Inspection
List Resources:
Show Resource:
State Format:
Dependency Graphs
Generate Graph:
View Dependencies:
Plan Output Analysis
Save Plan:
Analyze Changes:
13.3 Error Message Reference
Common Error Messages
| Error | Cause | Solution |
|---|---|---|
| Error acquiring state lock | Lock exists | Wait or release lock |
| Access Denied | IAM permissions | Check role permissions |
| Module not found | Source path wrong | Re-scaffold and init |
| Dependency not found | Dependency not deployed | Deploy dependency first |
| Invalid variable | Missing required var | Check values.yaml |
| Resource already exists | State mismatch | Import or refresh |
| Timeout | Operation too slow | Check AWS service status |
13.4 Recovery Procedures
Corrupted State
Symptoms:
- Terraform can't read state
- Resources exist but not in state
- Plan shows unexpected changes
Recovery:
1. List state versions:
- Download previous version:
- Verify recovered state:
- Upload recovered state:
Failed Deployments
Symptoms:
- Partial resource creation
- Resources in inconsistent state
- Error during apply
Recovery:
1. Check current state:
- Review failed resources:
- Fix issues and re-apply:
- Clean up orphaned resources (if needed):
Orphaned Resources
Symptoms:
- Resources exist in AWS but not in state
- Plan shows resources to be created (already exist)
Recovery:
1. Import resources:
- Or remove resources manually:
13.5 Chapter Summary
Executive Summary
-
★Common Issues
- State locking errors
- Permission denied
- Module not found
- Dependency failures
- Terraform crashes
-
★Debugging Techniques
- Verbose logging
- State inspection
- Dependency graphs
- Plan analysis
-
★Recovery Procedures
- Corrupted state recovery
- Failed deployment recovery
- Orphaned resource cleanup
-
★Best Practices
- Always plan before apply
- Backup state files
- Monitor for stuck locks
- Document recovery procedures