DEV Community

Cover image for Preventing Post-Termination EBS Waste: Automated Ownership Reports with Python & AWS Lambda
Ace7-coder
Ace7-coder

Posted on Edited on

Preventing Post-Termination EBS Waste: Automated Ownership Reports with Python & AWS Lambda

When EC2 instances are terminated across large AWS fleets, associated EBS volumes often survive in an unattached available state if DeleteOnTermination was not explicitly configured on every block device. Left unmonitored, hundreds of orphaned gigabytes continue accruing charges month after month.

The Governance Problem: Why Scripts Fail Without Ownership

In production environments with hundreds of instances, automated scripts that blindly delete unattached volumes create significant operational risk:

  • Missing Context: A detached volume might contain critical database state, debugging dumps, or compliance archives waiting on sign-off.
  • The Solution: A two-tier governance model—generate automated reporting with resource owner tags, send notification digests (via SNS/Slack), and enforce a grace period before snapshotting and purging.

Auditing & Reporting Detached Volumes with Boto3

Here is an automated audit pattern using boto3 that queries unattached volumes, resolves owner tags, and formats the findings for reporting:

import boto3

def audit_orphaned_ebs_with_tags(region='us-east-1'):
    ec2 = boto3.client('ec2', region_name=region)

    # Query unattached volumes
    volumes = ec2.describe_volumes(
        Filters=[{'Name': 'status', 'Values': ['available']}]
    ).get('Volumes', [])

    report = []
    for vol in volumes:
        vol_id = vol['VolumeId']
        size = vol['Size']
        tags = {t['Key']: t['Value'] for t in vol.get('Tags', [])}
        owner = tags.get('Owner', tags.get('CreatedBy', 'Unassigned'))

        report.append({
            'VolumeId': vol_id,
            'SizeGB': size,
            'Owner': owner,
            'CreateTime': vol['CreateTime'].strftime('%Y-%m-%d')
        })

    return report
Enter fullscreen mode Exit fullscreen mode

Running ad-hoc scripts in a single account or region does not scale across enterprise AWS Organizations. Sustained cloud cost governance requires automated anomaly detection, cross-account STS role assumption, and scheduled auditing.

  • 📦 Install Free Single-Account CLI: pip install aws-finops-auditor (PyPI | GitHub)
  • 💼 Managing Multiple Accounts or Client Infrastructure?
    • Practitioner License ($149/yr): Single-account CLI scanner, compliance exports, and dynamic bash remediation.
    • Consultancy & Agency Pack ($999/yr): Automated multi-account STS role assumption, CloudFormation StackSets, and branded executive PDF showback reports.

👉 Get the AWS Cloud Cost Auditor on Gumroad

Which AWS service generates the most unexpected waste in your environment? Let me know in the comments.

Top comments (0)