Need Advice To Add Exponenital Back-off Logic In Aws Api In Python
I have a few lambda functions which is making mutliple AWS Elastic beanstalk API Call written in python. It was working fine. but since last few days we are getting Throttling erro
Solution 1:
You can use a proxy object to wrap around any AWS client object and add some retrying logic to the proxy object:
from botocore.exceptions import ClientError
import retrying
import wrapt
classRetriedClient(wrapt.ObjectProxy):
"""Add retry logic to a boto3 client.
Wait 2^x * 1000 milliseconds between each retry, up to 10 seconds,
then 10 seconds afterwards.
"""defis_throttling_error(exception):
"""Botocore throws just a generic ClientError exception."""returnisinstance(exception, ClientError) \
and"RequestLimitExceeded"in exception.response
@retrying.retry(
wait_exponential_multiplier=1000,
wait_exponential_max=10000,
retry_on_exception=is_throttling_error)def__getattr__(self, name):
returngetattr(self.__wrapped__, name)
# Create a boto3 client to Cloudformation
cf_client = boto3.client('cloudformation')
# Add exponential backoff retries to all client methods
wrapped_cf_client = RetriedClient(cf_client)
Then you can just use wrapped_cf_client
as you would normally use boto3's built-in client:
resp = wrapped_cf_client.describe_stacks()
DEPRECATION NOTE:
In newer versions of botocore
there is a better way of configuring the retry logic of the boto3 SDKs. This works starting from version 1.6.0
of botocore
:
from botocore.config import Config
config = Config(
retries = dict(
max_attempts = 10
)
)
ec2 = boto3.client('ec2', config=config)
Post a Comment for "Need Advice To Add Exponenital Back-off Logic In Aws Api In Python"