使用boto3复制AWS快照

时间:2018-08-13 08:45:21

标签: python amazon-web-services boto3 snapshot

我有一段针对源和目标区域的代码。我设法对所有快照数据进行了响应,但是我无法仅对“ SnapshotId”进行过滤并复制响应。

import boto3

REGIONS = ['eu-central-1', 'eu-west-3']

SOURCEREG = boto3.client('ec2', region_name='eu-central-1')
DISTREG = boto3.client('ec2', region_name='eu-west-3')

response = SOURCEREG.describe_snapshots()
print(response)

在这种情况下,我收到一个类似于{'OwnerId':'xxxxxxx','StartTime':datetime.xxxxxxxx,'SnapshotId':'snap-xxxxxxxxxx“等.....}的json。

如何过滤此输出并复制快照?

1 个答案:

答案 0 :(得分:2)

参考:describe_snapshotscopy_snapshot

import boto3

conn = boto3.client('ec2', region_name='eu-central-1')
response = conn.describe_snapshots()

for snapshots in response['Snapshots']:
    print('Copying Snapshot -> ' + snapshots['SnapshotId'])
    copy_response = conn.copy_snapshot(
        Description='Snapshot copied from' + snapshots['SnapshotId'],
        DestinationRegion='eu-central-1',
        SourceRegion='eu-west-3',
        SourceSnapshotId=snapshots['SnapshotId'],
    )