Skip to main content

A CLI tool to detect drift in Terraform-managed infrastructure

Project description

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

  1. Clone this repository or download the files
  2. Install the required dependencies:
pip install -r requirements.txt
  1. 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:

  1. Finds all directories containing Terraform files
  2. Runs terraform init if needed
  3. Executes terraform plan with the -detailed-exitcode option
  4. Parses the results to determine if drift exists
  5. For detailed reports, uses terraform show -json to extract specific resource changes

Requirements

  • Python 3.6+
  • Terraform CLI installed and in PATH
  • Valid Terraform configurations with proper authentication to your infrastructure provider

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

terraform_drift_detector-0.1.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

terraform_drift_detector-0.1.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file terraform_drift_detector-0.1.0.tar.gz.

File metadata

File hashes

Hashes for terraform_drift_detector-0.1.0.tar.gz
Algorithm Hash digest
SHA256 afce2cf0b5c4aa7369965cd115da3329b8b2227f963712f971c459e6268365ed
MD5 953ad53d8b3907d7874d820106ba1ab9
BLAKE2b-256 5f618bafc40f099cb0aaa4771c7735a99c0967a168fa5ca1f2a82d49685a92a4

See more details on using hashes here.

File details

Details for the file terraform_drift_detector-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for terraform_drift_detector-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c02a90643c6fa45e675cdc38285f65c090464a693cc7b2862cad4cea565de9d8
MD5 41a48841cf0280ad3b801d91807fcf18
BLAKE2b-256 c6c5d2c3f629eb3d52696026641aa187517173d48492c4907640ae9c4d2825e4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page