python
boto3
aws
5 years, 7 months ago
import sys
import boto3
import datetime
import time
from boto.ec2 import connect_to_region
from datetime import datetime, timedelta
try:
days = int(sys.argv[1])
except IndexError:
days = 0
delete_time = datetime.utcnow() - timedelta(days=days)
filters = {
'tag-value': 'value',
'tag-key': 'key'
}
print ('Deleting any snapshots older than {days} days'.format(days=days))
ec2 = connect_to_region('us-east-1',aws_access_key_id='ACCESS_KEY',aws_secret_access_key='SECRET_KEY')
snapshots = ec2.get_all_snapshots(filters=filters)
deletion_counter = 0
size_counter = 0
for snapshot in snapshots:
start_time = datetime.strptime(
snapshot.start_time[:10],
'%Y-%m-%d'
)
if start_time < delete_time:
print ('Deleting {id}'.format(id=snapshot.id))
deletion_counter = deletion_counter + 1
size_counter = size_counter + snapshot.volume_size
snapshot.delete()
print ('Deleted {number} snapshots total {size} GB'.format(
number=deletion_counter,
size=size_counter
))
0 Comments
Please Login to Comment Here