Created
October 1, 2019 05:24
-
-
Save anuragmathur1/96a35bf44e6351f7a02f501247fa64cb to your computer and use it in GitHub Desktop.
Delete EBS snapshots that are not attached to any AMI and are at least 90 days old
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
## The script deletes the snapshots that are : | |
## - not attached to an AMI | |
## - are atleast 90 days old | |
## Output : | |
## - soem csv files if a report is required | |
## - the return values ( return_code && request_id from the delete command ) | |
## Usage | |
## ./delete-snapshots.py <ami-tag-name> <ami-tag-value> | |
import boto3 | |
import csv | |
import sys | |
import os | |
import shutil | |
import pytz | |
from datetime import datetime, timedelta | |
from pprint import pprint | |
account_id = boto3.client('sts').get_caller_identity()['Account'] | |
oldest_date = datetime.now(pytz.utc) - timedelta(days=90) | |
tag_value = sys.argv[1] | |
#shutil.rmtree(account_id) | |
if not os.path.exists(account_id): | |
os.makedirs(account_id) | |
map_file = account_id+'/map_file' | |
ami_snapshots = account_id+'/ami_snapshots' | |
all_snapshots = account_id+'/all_snapshots' | |
client = boto3.client('ec2') | |
csvdata = [] | |
ami_snaps = [] | |
response = client.describe_images(Owners=[account_id]) | |
for i in response['Images']: | |
curr_data = [i['ImageId']] | |
for j in i['BlockDeviceMappings']: | |
if 'Ebs' in j: | |
snapshots = [] | |
curr_data.append(j['Ebs']['SnapshotId']) | |
snapshots.append(j['Ebs']['SnapshotId']) | |
ami_snaps.append(snapshots) | |
csvdata.append(curr_data) | |
with open(map_file,'w') as csvFile: | |
writer = csv.writer(csvFile) | |
writer.writerows(csvdata) | |
csvFile.close() | |
with open(ami_snapshots,'w') as csvFile: | |
writer = csv.writer(csvFile) | |
writer.writerows(ami_snaps) | |
csvFile.close() | |
## Get All Snaphots | |
response = client.describe_snapshots(OwnerIds=[account_id]) | |
snap_list = [] | |
for i in response['Snapshots']: | |
snapshot_id = i['SnapshotId'] | |
temp_arr = [snapshot_id] | |
if temp_arr not in ami_snaps: | |
snap_list.append(temp_arr) | |
with open(all_snapshots,'w') as csvFile: | |
writer = csv.writer(csvFile) | |
writer.writerows(snap_list) | |
csvFile.close() | |
old_snapshot = [] | |
for s in snap_list: | |
print "====================================================" | |
snapshot = client.describe_snapshots(OwnerIds=[account_id],SnapshotIds=[s[0]],Filters=[{'Name':'tag:'+sys.argv[1],'Values':[sys.argv[2]]}]) | |
#pprint(snapshot) | |
if snapshot['Snapshots'] == []: | |
#pprint(s[0]+" does not have the tag key") | |
continue | |
if snapshot['Snapshots'][0]['StartTime'] < oldest_date: | |
pprint(s[0] + " to be DELETED !!") | |
#pprint(s[0]) | |
response = client.delete_snapshot(SnapshotId=s[0]) | |
pprint(response['ResponseMetadata']['HTTPStatusCode']) | |
pprint(response['ResponseMetadata']['RequestId']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment