AWS Now Allows Deleting EBS Snapshots When Deregistering AMIs

Listen to this Post

Featured Image
Amazon Web Services (AWS) has introduced a long-awaited feature that enables users to delete underlying Elastic Block Store (EBS) snapshots when deregistering Amazon Machine Images (AMIs). This simplifies storage management and reduces unnecessary costs by eliminating orphaned snapshots.

🔗 Source: AWS Announcement

You Should Know:

  1. How to Deregister an AMI and Delete Its Snapshots
    To automate the cleanup of AMIs and their associated snapshots, use the following AWS CLI commands:

List AMIs and Their Snapshots

aws ec2 describe-images --image-ids ami-1234567890abcdef0 

Deregister AMI & Delete Snapshots (Manual Method)

aws ec2 deregister-image --image-id ami-1234567890abcdef0 
aws ec2 delete-snapshot --snapshot-id snap-1234567890abcdef0 

Automated Cleanup Script (Bash)

!/bin/bash 
AMI_ID="ami-1234567890abcdef0" 
SNAPSHOTS=$(aws ec2 describe-images --image-ids $AMI_ID --query 'Images[bash].BlockDeviceMappings[].Ebs.SnapshotId' --output text)

aws ec2 deregister-image --image-id $AMI_ID

for SNAP in $SNAPSHOTS; do 
aws ec2 delete-snapshot --snapshot-id $SNAP 
echo "Deleted snapshot: $SNAP" 
done 

2. Verify Snapshots Are Deleted

aws ec2 describe-snapshots --snapshot-ids snap-1234567890abcdef0 

3. Using AWS Lambda for Automated Cleanup

Deploy an AWS Lambda function triggered by CloudWatch Events to auto-handle stale AMIs and snapshots.

import boto3

def lambda_handler(event, context): 
ec2 = boto3.client('ec2') 
old_amis = ec2.describe_images(Filters=[{'Name': 'state', 'Values': ['available']}])

for ami in old_amis['Images']: 
ami_id = ami['ImageId'] 
snapshots = [bdm['Ebs']['SnapshotId'] for bdm in ami['BlockDeviceMappings'] if 'Ebs' in bdm]

ec2.deregister_image(ImageId=ami_id) 
for snap in snapshots: 
ec2.delete_snapshot(SnapshotId=snap) 

What Undercode Say

AWS’s new feature eliminates manual snapshot cleanup, reducing storage costs and operational overhead. Automating this process via CLI or Lambda ensures compliance and cost efficiency.

Additional Useful Commands

  • List All Unused Snapshots:
    aws ec2 describe-snapshots --owner-ids self --query 'Snapshots[?StartTime<=<code>2025-01-01</code>].SnapshotId' 
    
  • Cross-Region Cleanup:
    aws ec2 copy-snapshot --source-region us-east-1 --source-snapshot-id snap-1234567890abcdef0 --region eu-west-1 
    
  • Windows (PowerShell) Alternative:
    Remove-EC2Image -ImageId ami-1234567890abcdef0 -Force 
    

Expected Output:

A streamlined AMI and snapshot management process, reducing AWS costs and manual cleanup efforts.

Prediction

AWS will likely introduce more automated lifecycle policies for AMIs and snapshots, further simplifying cloud resource management.

IT/Security Reporter URL:

Reported By: Marko Bevc – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 Telegram