使用python3和boto3获取最新的aws卷snapshot-id

时间:2016-12-25 09:40:39

标签: python python-3.x amazon-web-services aws-sdk boto3

我是for(var i = 0; i < array.length; i++){ if(array[i].getAttribute("data") > 1){ // your code here } } python的新手,我想获取最新的快照ID。

我不确定我是否正确编写了lambda的排序以及如何访问最后一个快照,或者当我打印boto3和{{1}时,我可以使用脚本的第一部分来完成}?

感谢。

这是我的剧本

snapshot_id

输出:

snapshot_date

*更新问题到@roshan回答:

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 = ?

输出:

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>

如果我这样做:

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)

最新快照未出现在输出中:

[{'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

这是最新的:

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])

如何获取最新快照({'date': datetime.datetime(2016, 12, 23, 3, 0, 18, tzinfo=tzutc()), 'snap_id': 'snap-0225cff1675c369'} )?

由于

4 个答案:

答案 0 :(得分:2)

您应该按reverse顺序排序。

sorted(list_to_be_sorted, key=lambda k: k['date'], reverse=True)[0]

按日期对列表进行排序(按升序排列 - 从最旧到最新) 如果添加reverse=True,则按降序排序(最新到最旧)。 [0]返回列表中的第一个元素,即最新的快照。

如果您想要最新快照的snap_id,只需访问该密钥。

sorted(list_to_be_sorted, key=lambda k: k['date'], reverse=True)[0]['snap_id']

答案 1 :(得分:1)

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)

答案 2 :(得分:1)

最后一行必须是这样的:

",".join(my_list).split(",")

答案 3 :(得分:0)

您可以将快照附加到列表,然后根据日期对列表进行排序

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'])  

希望有所帮助!!