How to handle EC2 RequestLimitExceeded in boto3?
ec2 = boto3.resource('ec2')
instances = ec2.instances.all()
for instance in instances:
for volume in instance.volumes.all():
for snapshot in volume.snapshots.all():
The fifth line is throwing the error "botocore.exceptions.ClientError: an Error occurred (RequestLimitExceeded) when calling the DescribeSnapshots operation: Request limit exceeded."
- Does boto3 implement exponential backoff? Is boto3 configurable for number of retries?
- If not, what's proper way to handle this error? Is the exponential backoff the answer? Is there a sample on how to do it? I'm guessing its been done many times before so I rather not start from scratch.
- What's the suggested way to test for rate limit errors? Running the code in my account causes no problems since I only have a few instances/volumes/snapshots. This error occurs when this code is run in another account (that I don't have access to) that has 1000s of instances, volumes, snapshots.
- Is there a more efficient way to write the above code? The script is iterating through all instances to find any volumes that hasn't had a snapshot in 24 hours.
- Yes, boto3 implements exponential backoff. This request should be retried as shown here. In that file there's a
__defaults__section, which shows we retry 5 times. If you're seeing these errors it's because you're exceeding the 5 attempts we make. You can also see more info by addingboto3.set_stream_logger('')at the top of your file. - Should be handled, but we don't have a way to increase the number of attempts. There is a feature request tracking this work.
- Ideally boto3 should be handling this for you. For some reason the level of throttling being applied is exceeding our default values. Once we support configurable retries, you should be able to just increase the retry attempts and not have to worry about this.
- I'd ask on the EC2 forums just for sure, but for as I can tell the best you can do is: 1) describe_volumes paginated call to get all of your volumes 2) for each volume make a describe-snapshots call with a filter of
volume-idandstart-time. So if N is your total volumes you'd have N + (N / 1000) as the number of API calls needed. But again, I'd ask on the EC2 forums.
Found the issue: https://github.com/boto/botocore/issues/882
I believe we can move the discussion over to that issue. Essentially what you'll need is the ability to increase the number of retries in the case where our default number of exponential backoff attempts is insufficient.
Closing issue. Let's track this in the issue that @jamesls linked.
One quick question, as the changes are done to botocore will the retry logic be present in boto 2.45?