AWSRetry is a Python Decorator that can be used to wrap boto3 function calls. This function was built out of the need to get around a couple of common issues when working with AWS API's.
This list can be extended. (http://docs.aws.amazon.com/AWSEC2/latest/APIReference/errors-overview.html)
Install awsretry.
.. code-block:: sh
$ pip install awsretry
I will assume you know about setting up Boto3 Credentials, if not you can read the instructions here http://boto3.readthedocs.io/en/latest/guide/configuration.html
Write a quick function that implements AWSRetry.backoff()
.. code-block:: python
#!/usr/bin/env python
import botocore
import boto3
from awsretry import AWSRetry
@AWSRetry.backoff()
def get_instances():
client = boto3.client('ec2')
try:
instances = client.describe_instances()
return instances
except botocore.exceptions.ClientError as e:
raise e
instances = get_instances()
Write a quick function that will overwrite the default arguments.
.. code-block:: python
import botocore import boto3 from awsretry import AWSRetry
@AWSRetry.backoff(tries=20, delay=2, backoff=1.5, added_exceptions=['ConcurrentTagAccess']) def create_tags(): client = boto3.client('ec2') try: resources = ['1-12345678891234'] tags = [{'Key': 'service', 'Value': 'web-app'}] instances = client.create_tags(Resources=resources, Tags=tags) except botocore.exceptions.ClientError as e: raise e
create_tags()
Assuming that you have Python and virtualenv
installed, set up your
environment and install the required dependencies like this instead of
the pip install awsretry
defined above:
.. code-block:: sh
$ git clone https://github.com/linuxdynasty/awsretry.git
$ cd awsretry
$ virtualenv venv
...
$ . venv/bin/activate
$ pip install -r requirements.txt
$ pip install -e .
You can run the tests by using tox which implements nosetest or run them directly using nosetest.
.. code-block:: sh
$ tox
$ tox tests/test_awsretry.py
$ tox -e py27,py36 tests/
$ nosetest