I am new to python
and boto3
, I want to get the latest snapshot ID.
I am not sure if I wrote the sort with lambda correctly and how to access the last snapshot, or maybe I can do it with the first part of the script when I print the snapshot_id
and snapshot_date
?
Thanks.
Here is my script
import boto3
ec2client = mysession.client('ec2', region_name=region)
ec2resource = mysession.resource('ec2', region_name=region)
def find_snapshots():
for snapshot in ec2client_describe_snapshots['Snapshots']:
snapshot_volume = snapshot['VolumeId']
mnt_vol = "vol-xxxxx"
if mnt_vol == snapshot_volume:
snapshot_date = snapshot['StartTime']
snapshot_id = snapshot['SnapshotId']
print(snapshot_id)
print(snapshot_date)
find_snapshots()
snapshots = ec2resource.snapshots.filter(Filters=[{'Name': 'volume-id', 'Values': [mnt_vol]}]).all()
print(snapshots)
snapshots = sorted(snapshots, key=lambda ss:ss.start_time)
print(snapshots)
snapshot_ids = map(lambda ss:ss.id, snapshots)
print(snapshot_ids)
last_snap_id = ?
output:
snap-05a8e27b15161d3d5
2016-12-25 05:00:17+00:00
snap-0b87285592e21f0
2016-12-25 03:00:17+00:00
snap-06fa39b86961ffa89
2016-12-24 03:00:17+00:00
ec2.snapshotsCollection(ec2.ServiceResource(), ec2.Snapshot)
[]
<map object at 0x7f8d91ea9cc0>
*update question to @roshan answer:
def find_snapshots():
list_of_snaps = []
for snapshot in ec2client_describe_snapshots['Snapshots']:
snapshot_volume = snapshot['VolumeId']
if mnt_vol == snapshot_volume:
snapshot_date = snapshot['StartTime']
snapshot_id = snapshot['SnapshotId']
list_of_snaps.append({'date':snapshot['StartTime'], 'snap_id': snapshot['SnapshotId']})
return(list_of_snaps)
find_snapshots()
print(find_snapshots())
#sort snapshots order by date
newlist = sorted(list_to_be_sorted, key=lambda k: k['name'])
print(newlist)
output:
[{'date': datetime.datetime(2016, 12, 25, 14, 23, 37, tzinfo=tzutc()), 'snap_id': 'snap-0de26a40c1d1e53'}, {'date': datetime.datetime(2016, 12, 24, 22, 9, 34, tzinfo=tzutc()), 'snap_id': 'snap-0f0341c53f47a08'}]
Traceback (most recent call last):
File "test.py", line 115, in <module>
newlist = sorted(list_to_be_sorted, key=lambda k: k['name'])
NameError: name 'list_to_be_sorted' is not defined
If I do:
list_to_be_sorted = (find_snapshots())
print(list_to_be_sorted)
#sort snapshots order by date
newlist = sorted(list_to_be_sorted, key=lambda k: k['snap_id'])
print(newlist[0])
the latest snapshot does not appear in the output:
{'date': datetime.datetime(2016, 12, 23, 3, 0, 18, tzinfo=tzutc()), 'snap_id': 'snap-0225cff1675c369'}
this one is the latest:
[{'date': datetime.datetime(2016, 12, 25, 5, 0, 17, tzinfo=tzutc()), 'snap_id': 'snap-05a8e27b15161d5'}
How do I get the latest snapshot (snap_id
) ?
Thanks
You should it sort in
reverse
order.sorts the list by the date (in ascending order - oldest to the latest) If you add
reverse=True
, then it sorts in descending order (latest to the oldest).[0]
returns the first element in the list which is the latest snapshot.If you want the
snap_id
of the latest snapshot, just access that key.