[Python] Retry process (Exponential Backoff) memo in AWS Lambda

What should I do with the retry process in the Lambda function? It is a memo that I thought about.

The following slide was helpful for the retry process in Lambda.

In addition, the following articles are detailed about Exponential Backoff.

I will actually write

Lambda functions are written in AWS Chalice. (Retry processing can be done without any problem even if it is not Chalice.) For retries, try using the following libraries.

$ pip install retrying

There is also retry, which seems to be easy to retry as well.

app.py


from chalice import Chalice                                                                                                                    
from datetime import datetime
from retrying import retry
import boto3
from botocore.exceptions import ClientError

app = Chalice(app_name='exponential-backoff')

def retryIfClientError(exception):
    print(datetime.now().strftime("%Y/%m/%d %H:%M:%S"))
    print('retrying...')
    #Retry until False is returned
    return isinstance(exception, ClientError)

# retry_on_exception:To pick up a specific Exception
# stop_max_attempt_number:Number of trials
# wait_exponential_multiplier:multiplier
@retry(retry_on_exception=retryIfClientError, stop_max_attempt_number=3, wait_exponential_multiplier=1000)
def retryS3Test():
    s3 = boto3.resource('s3')
    client = s3.meta.client
    response = client.get_object(Bucket='mybucket', Key='Files that may not exist')

@app.route('/')
def index():
    retryS3Test()

requirements.txt


retrying==1.3.3

If you deploy the chalice project and hit Endpoint, you should see the following log in CloudWatch Log and it is being retried.

スクリーンショット 2017-07-29 18.40.38.png

Recommended Posts

[Python] Retry process (Exponential Backoff) memo in AWS Lambda
[Python] Scraping in AWS Lambda
[AWS Lambda] Use any container Image in Python very simply
Summary if using AWS Lambda (Python)
Effective Python Memo Item 11 Use zip to process iterators in parallel
Run Python on Schedule on AWS Lambda
Best practice for logging in JSON format on AWS Lambda / Python
[Python / AWS Lambda layers] I want to reuse only module in AWS Lambda Layers
I compared Node.js and Python in creating thumbnails using AWS Lambda
Install pip in Serverless Framework and AWS Lambda with Python environment
Notify HipChat with AWS Lambda (Python)
Easy server monitoring with AWS Lambda (Python) and result notification in Slack
Modules cannot be imported in Python on EC2 run from AWS Lambda
[AWS] Problem avoidance memo when installing awscli and boto3 in Python 2.7 environment
Upload what you got in request to S3 with AWS Lambda Python
[AWS] Using ini files with Lambda [Python]
Use print in a Python2 lambda expression
Obtaining temporary AWS credentials in PHP, Python
Process multiple lists with for in Python
[Python] Run Headless Chrome on AWS Lambda
Connect to s3 with AWS Lambda Python
Image sending / receiving memo in Python (Flask)
CG image quality evaluation memo in Python
Python + Selenium + Headless Chromium with aws lambda
Python memo
python memo
Python memo
Python memo
Create a setting in terraform to send a message from AWS Lambda Python3.8 to Slack
Python memo
Python memo
Periodically run a python program on AWS Lambda
A memo that I wrote a quicksort in Python
Differences between queryStringParameters and multiValueQueryStringParameters in AWS Lambda
Creating numbering process using python in DynamoDB Local Numbering process
I was able to recurse in Python: lambda
A memo about writing merge sort in Python
LINE BOT with Python + AWS Lambda + API Gateway
Serverless application with AWS SAM! (APIGATEWAY + Lambda (Python))
[AWS / Lambda] How to load Python external library
Getting started with AWS IoT easily in Python
Summary of studying Python to use AWS Lambda
Convert exponential notation float to str in Python
Amazon API Gateway and AWS Lambda Python version