Reputation: 432
I'm using AWS Lambda python functions for copying EBS/RDS snapshots to another region for disaster recovery. The issue that I encountered is a copy limit of 5 snapshots at the time. If I tried to copy more then 5 at the time I will get an error:
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.
To avoid this, I add a waiter function that is checking the status of the snapshot in the destination region and after a snapshot has completed status it continues a loop. It works well, but in this case, it's coping only one snapshot at the time. The question is, how to implement concurrent.futures module for a parallel task that will copy 5 snapshots at the time?
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
Upvotes: 1
Views: 7432
Reputation: 1904
You can use concurrent executor and max_workers
parameter to limit how many jobs are run simutaneously. Like this:
import concurrent.futures
def copy_snapshot(snapshot_id):
waiter = client_ec2_dst.get_waiter('snapshot_completed')
response = client_ec2_dst.copy_snapshot(
Description='[Disaster Recovery] copied from us-east-1',
SourceRegion=region_src,
SourceSnapshotId=snapshot_id,
DryRun=False
)
new_snapshot_id = response["SnapshotId"]
waiter.wait(
SnapshotIds=[new_snapshot_id],
WaiterConfig={'Delay': 5, 'MaxAttempts': 120}
)
# Copy snapshots in parallel, but no more than 5 at a time:
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
futures = [
executor.submit(copy_snapshot, s['SnapshotId'])
for s in ec2_snapshots_src]
for future in futures:
future.result()
Upvotes: 4