[英]get the latest aws volume snapshot-id using python3 and boto3
I am new to python
and boto3
, I want to get the latest snapshot ID.我是
python
和boto3
,我想获取最新的快照 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
?我不确定我是否使用 lambda 正确编写了排序以及如何访问最后一个快照,或者我可以在打印
snapshot_id
和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: *将问题更新为@roshan 答案:
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
) ?如何获取最新快照 (
snap_id
)?
Thanks谢谢
You should it sort in reverse
order.你应该以
reverse
顺序排序。
sorted(list_to_be_sorted, key=lambda k: k['date'], reverse=True)[0]
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).按日期对列表进行排序(按升序 - 从最旧到最新)如果添加
reverse=True
,则按降序排序(从最新到最旧)。 [0]
returns the first element in the list which is the latest snapshot. [0]
返回列表中的第一个元素,即最新的快照。
If you want the snap_id
of the latest snapshot, just access that key.如果您想要最新快照的
snap_id
,只需访问该密钥。
sorted(list_to_be_sorted, key=lambda k: k['date'], reverse=True)[0]['snap_id']
def find_snapshots():
list_of_snaps = []
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']
list_of_snaps.append({'date':snapshot['StartTime'], 'snap_id': snapshot['SnapshotId']})
print(snapshot_id)
print(snapshot_date)
#sort snapshots order by date
newlist = sorted(list_of_snaps, key=lambda k: k['date'], reverse= True)
latest_snap_id = newlist[0]['snap_id']
#The latest_snap_id provides the exact output snapshot ID
print(latest_snap_id)
最后一行需要是这样的:
newlist = sorted(list_of_snaps, key=lambda k: k['snap_id'])
You can append the snapshots to list, then sort the list based on date您可以将快照附加到列表中,然后根据日期对列表进行排序
def find_snapshots():
list_of_snaps = []
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']
list_of_snaps.append({'date':snapshot['StartTime'], 'snap_id': snapshot['SnapshotId']})
print(snapshot_id)
print(snapshot_date)
#sort snapshots order by date
newlist = sorted(list_to_be_sorted, key=lambda k: k['name'])
Hope it helps !!希望能帮助到你 !!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.