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.
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.
Recommended Posts