From 1438a229ce666d006fd345a8fb1d9f67a12ea392 Mon Sep 17 00:00:00 2001 From: Deepak Date: Sat, 13 Apr 2024 17:28:27 +0530 Subject: [PATCH] Create EBS_Stale_Delete.py --- EBS_Stale_delete.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 EBS_Stale_delete.py diff --git a/EBS_Stale_delete.py b/EBS_Stale_delete.py new file mode 100644 index 0000000..d697037 --- /dev/null +++ b/EBS_Stale_delete.py @@ -0,0 +1,38 @@ + +import boto3 + +def lambda_handler(event, context): + ec2 = boto3.client('ec2') + + # Get all EBS snapshots + response = ec2.describe_snapshots(OwnerIds=['self']) + + # Get all active EC2 instance IDs + instances_response = ec2.describe_instances(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]) + active_instance_ids = set() + + for reservation in instances_response['Reservations']: + for instance in reservation['Instances']: + active_instance_ids.add(instance['InstanceId']) + + # Iterate through each snapshot and delete if it's not attached to any volume or the volume is not attached to a running instance + for snapshot in response['Snapshots']: + snapshot_id = snapshot['SnapshotId'] + volume_id = snapshot.get('VolumeId') + + if not volume_id: + # Delete the snapshot if it's not attached to any volume + ec2.delete_snapshot(SnapshotId=snapshot_id) + print(f"Deleted EBS snapshot {snapshot_id} as it was not attached to any volume.") + else: + # Check if the volume still exists + try: + volume_response = ec2.describe_volumes(VolumeIds=[volume_id]) + if not volume_response['Volumes'][0]['Attachments']: + ec2.delete_snapshot(SnapshotId=snapshot_id) + print(f"Deleted EBS snapshot {snapshot_id} as it was taken from a volume not attached to any running instance.") + except ec2.exceptions.ClientError as e: + if e.response['Error']['Code'] == 'InvalidVolume.NotFound': + # The volume associated with the snapshot is not found (it might have been deleted) + ec2.delete_snapshot(SnapshotId=snapshot_id) + print(f"Deleted EBS snapshot {snapshot_id} as its associated volume was not found.")