【发布时间】:2019-09-30 23:29:34
【问题描述】:
我正在使用 AWS Lambda python 函数将 EBS/RDS 快照复制到另一个区域以进行灾难恢复。 我遇到的问题是当时 5 个快照的复制限制。 如果我当时尝试复制超过 5 个,我会收到错误:
botocore.exceptions.ClientError: An error occurred (ResourceLimitExceeded) when calling the CopySnapshot operation: Too many snapshot copies in progress. The limit is 5 for this destination region.
为了避免这种情况,我添加了一个等待函数,它检查目标区域中快照的状态,并在快照完成状态后继续循环。 它运行良好,但在这种情况下,它当时只处理一个快照。 问题是,如何为一个同时复制 5 个快照的并行任务实现 concurrent.futures 模块?
waiter = client_ec2_dst.get_waiter('snapshot_completed')
message = ""
for i in ec2_snapshots_src:
# snapshot_tags_filtered = ([item for item in i["Tags"] if item['Key'] != 'aws:backup:source-resource']
# snapshot_tags_filtered.append({'Key': 'delete_On', 'Value': delete_on})
# snapshot_tags_filtered.append({'Key': 'src_Id', 'Value': i["SnapshotId"]})
try:
response = client_ec2_dst.copy_snapshot(
Description='[Disaster Recovery] copied from us-east-1',
SourceRegion=region_src,
SourceSnapshotId=i["SnapshotId"],
DryRun=False,
# Encrypted=True,
# KmsKeyId='1e287363-89f6-4837-a619-b550ff28c211',
)
new_snapshot_id = response["SnapshotId"]
waiter.wait(
SnapshotIds=[new_snapshot_id],
WaiterConfig={'Delay': 5, 'MaxAttempts': 120}
)
snapshot_src_name = ([dic['Value'] for dic in snapshot_tags_filtered if dic['Key'] == 'Name'])
message += ("Started copying latest EBS snapshot: " + i["SnapshotId"] + " for EC2 instance: " + str(snapshot_src_name) + " from: " + region_src + " to: " + region_dst + " with new id: " + new_snapshot_id + ".\n")
# Adding tags to snapshots in destination region
tag_src = [new_snapshot_id]
tag = client_ec2_dst.create_tags(
DryRun=False,
Resources=tag_src,
Tags=snapshot_tags_filtered
)
except Exception as e:
raise e
【问题讨论】:
标签: python amazon-web-services aws-lambda boto3 concurrent.futures