I'm trying to write a python script to delete ebs snapshots which are 14 days old. problem is i could not able to display 2 weeks old date and compare that with current date. Below is my code
import boto import dateutil.relativedelta from boto.exception import EC2ResponseError from datetime import timedelta, datetime conn = boto.ec2.connect_to_region( 'us-east-1', aws_access_key_id = 'xxxxxxxxxxxxx', aws_secret_access_key = 'yyyyyyyyyyyyyyyyyy', ) snaps = conn.get_all_snapshots() for list in snaps: old_date = list.start_time - timedelta(days=14) if list.start_time (is less than or equal to) old_date: print conn.delete_snapshots(list.id)
Error:
Traceback (most recent call last): File "/home/swaroop/Documents/My_python/display_snapshots.py", line 28, in old_date = list.start_time - timedelta(days=14) TypeError: unsupported operand type(s) for -: 'unicode' and 'datetime.timedelta'
Note: Snapshots display date format this way: 2013-12-19T11:11:43.000Z
Swaroop.
In your code list.start_time
returns start datetime as unicode string.
You need to convert it to datetime.timedelta
like
from dateutil import parser
and
for snap in snaps:
limit = datetime.now() - timedelta(days=14)
if parser.parse(snap.start_time).date() <= limit.date():
#do your filter stuff here
print conn.delete_snapshot(snap.id)
Note: First you can try with print snap.id
, if it will return a valid result then you can do print conn.delete_snapshot(snap.id)