简体   繁体   中英

Using Tenacity With an AWS (boto3) Call

I am running into intermittent RateLimitExceeded errors in a call to AWS using boto3. In the example below, either call, instance.all() or i.vpc.tags could fail due to rate limiting:

 for i in instance.all():
     tags = i.vpc.tags

Normally, I use tenacity as a decorator on my own functions, but obviously that cannot be done with this call since it's from an imported library. If it weren't in a for loop I could use the retrying function, like this:

r = tenacity.Retrying(
    reraise=True, 
    wait=tenacity.wait_random_exponential(multiplier=1, max=60), 
    stop=tenacity.stop_after_delay(130))

r.call(call_wrapped_in_tenacity())

So, is there a way to wrap these two calls to AWS without building out a new function for each while keeping the retry ability?

If creating your own functions is out of scope, then here is the only way that I found to fix this issue. Basically, when you create your boto3 Client, you pass it a reference to your own setting in the boto configuration, like this:

from botocore.config import Config
config = Config(retries=dict(max_attempts=20))
ec2_client = boto3.client('ec2', config=config)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM