Terraform State Delta and Infrastructure Drift Detector
This infrastructure script parses generated Terraform plan outputs serialized as JSON structures to evaluate proposed resource modifications and identify destructive resource updates. Internally, it traverses resource_changes arrays inside the parsed plan object, evaluating attribute deltas between before and after resource states to flag unexpected resource deletions or unauthorized security group changes prior to execution.
Integrating this script into deployment workflows establishes automated GitOps governance controls. It functions as a pipeline safety check that halts deployment automation whenever execution plans contain unapproved modifications, uncommitted state drifts, or hazardous infrastructure destructions.
Large scale state plan files containing thousands of resources require significant memory resources during deep tree iterations. Because serialized plan files can expose sensitive values such as plain text database passwords or API keys, process execution must occur inside ephemeral pipeline environments with secure memory management.
> System.Execute()
import json, sys
def analyze_terraform_plan(plan_json_path):
with open(plan_json_path) as f:
plan = json.load(f)
destructive_changes = []
for change in plan.get("resource_changes", []):
actions = change.get("change", {}).get("actions", [])
if "delete" in actions:
destructive_changes.append(f"Resource: {change['address']} | Actions: {actions}")
if destructive_changes:
print(f"[CRITICAL] Destructive Changes Detected: {len(destructive_changes)}")
for item in destructive_changes:
print(f" - {item}")
sys.exit(1)
else:
print("[+] Infrastructure plan verified. No unexpected destructions detected.")
analyze_terraform_plan("tfplan.json")
> Architecture & Security Context
Verified for Valters IT Hub. When deploying this configuration in a production homelab or enterprise environment, ensure that you monitor system resources and implement strict access controls. Administrator privileges are required.
ID: VALTERSIT-F2DDF12E2A2E