DEV Community

Ace7-coder
Ace7-coder

Posted 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

As cloud engineer Aman Maheshwari noted, in environments with hundreds of instances, automated scripts that blindly delete unattached volumes create 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

Enterprise Multi-Account Governance

Running isolated scripts per region or single account does not scale across enterprise AWS Organizations. Real-world FinOps requires role assumption across organizational units, cost anomaly correlation, and automated scheduled auditing.

Top comments (0)