Copying AWS Snapshots using boto3

2019-08-20 08:10发布

问题:

I have piece of code for source and destination regions. I managed to have a reponse with all snapshot data but I can't manage to filter the response just to "SnapshotId" and copying it.

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)

In this case I receive a json response looking like {'OwnerId': 'xxxxxxx', 'StartTime': datetime.xxxxxxxx, 'SnapshotId': 'snap-xxxxxxxxxx", etc .....}.

How can i filter this output and copy the snapshots?

回答1:

Reference: describe_snapshots and copy_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'],
    )