boto3 icon indicating copy to clipboard operation
boto3 copied to clipboard

How to handle EC2 RequestLimitExceeded in boto3?

Open sajee opened this issue 10 years ago • 4 comments

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."

  1. Does boto3 implement exponential backoff? Is boto3 configurable for number of retries?
  2. 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.
  3. 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.
  4. 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.

sajee avatar Aug 13 '16 18:08 sajee

  1. 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 adding boto3.set_stream_logger('') at the top of your file.
  2. Should be handled, but we don't have a way to increase the number of attempts. There is a feature request tracking this work.
  3. 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.
  4. 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-id and start-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.

jamesls avatar Aug 16 '16 20:08 jamesls

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.

jamesls avatar Aug 16 '16 20:08 jamesls

Closing issue. Let's track this in the issue that @jamesls linked.

kyleknap avatar Aug 30 '16 19:08 kyleknap

One quick question, as the changes are done to botocore will the retry logic be present in boto 2.45?

maaziz avatar Apr 22 '17 07:04 maaziz