Terraform Drift Detector
A Python CLI tool to detect and report drift between your Terraform configuration and the actual infrastructure state.
Overview
Infrastructure drift occurs when your actual cloud resources deviate from what's defined in your Terraform files. This tool helps you detect such drift by running terraform plan across your infrastructure and analyzing the results.
Features
- Scan single or multiple Terraform directories for drift
- Parallel processing for faster scanning of large infrastructure
- Detailed reporting of resources that will be added, changed, or destroyed
- Color-coded output for better readability
- Option to export results to JSON for further processing or integration
Installation
- Clone this repository or download the files
- Install the required dependencies:
pip install -r requirements.txt
- Make the script executable:
chmod +x terraform_drift_detector.py
Usage
Basic usage:
tfdd
This will scan the current directory for Terraform files and check for drift.
Command Line Options
usage: terraform_drift_detector.py [-h] [-d DIRECTORY] [--details] [-w WORKERS] [-o OUTPUT]
Detect drift in Terraform-managed infrastructure
options:
-h, --help show this help message and exit
-d DIRECTORY, --directory DIRECTORY
Directory containing Terraform files (default: current directory)
--details Show detailed information about detected drift
-w WORKERS, --workers WORKERS
Maximum number of parallel workers (default: 5)
-o OUTPUT, --output OUTPUT
Save results to a JSON file
Examples
Scan a specific directory with detailed output:
./terraform_drift_detector.py -d /path/to/terraform --details
Scan with more parallel workers and save results to a file:
./terraform_drift_detector.py -w 10 -o drift_results.json
Exit Codes
- 0: No drift detected
- 1: Error during execution
- 2: Drift detected
You can use these exit codes in CI/CD pipelines to fail builds or trigger alerts when drift is detected.
How It Works
The tool:
- Finds all directories containing Terraform files
- Runs
terraform initif needed - Executes
terraform planwith the-detailed-exitcodeoption - Parses the results to determine if drift exists
- For detailed reports, uses
terraform show -jsonto extract specific resource changes
Requirements
- Python 3.6+
- Terraform CLI installed and in PATH
- Valid Terraform configurations with proper authentication to your infrastructure provider
Metadata
Release files for terraform-drift-detector 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| terraform_drift_detector-0.1.0.tar.gz | 8.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| terraform_drift_detector-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.4 kB
Release files / terraform_drift_detector-0.1.0.tar.gz
| Download URL | terraform_drift_detector-0.1.0.tar.gz |
|---|---|
| Size | 8.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
afce2cf0b5c4aa7369965cd115da3329b8b2227f963712f971c459e6268365ed
|
|
BLAKE2b-256 checksum How to use checksums |
5f618bafc40f099cb0aaa4771c7735a99c0967a168fa5ca1f2a82d49685a92a4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.0
|
Release files / terraform_drift_detector-0.1.0-py3-none-any.whl
| Download URL | terraform_drift_detector-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c02a90643c6fa45e675cdc38285f65c090464a693cc7b2862cad4cea565de9d8
|
|
BLAKE2b-256 checksum How to use checksums |
c6c5d2c3f629eb3d52696026641aa187517173d48492c4907640ae9c4d2825e4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.0
|