Yuta NakataのBlog

Python / AWS / ITについて役立つ情報を発信します

AWS/boto3でRate Exceeded の回避方法

結論

import boto3
from botocore.config import Config
 
config = Config(retries = { 'max_attempts': 10, 'mode': 'standard' } )
 
client = boto3.client('s3', config=config)

でOKです。

巷では、

pip install retrying

https://github.com/rholder/retrying

をインストールして、回避する方法もありますが、lambdaであればlayer追加等の手順が必要なこともあるので、基本的には、aws SDKであるboto3による回避がオススメです。

解説

config = Config(retries = { 'max_attempts': 10, 'mode': 'standard' } )

の引数について、解説です。

max_attemptsは、試行回数を指定します。

modeは、

  • standard
  • legacy
  • adaptive

の三つがあります。

  • legaryは、標準のmodeです。最初期にリリースされたこともあり、 再試行に対応するerrors/exceptions の数が限られています。

  • adaptiveは、最もリッチなmodeですが、それ故に将来的な仕様変更が発生する可能性が一番高いです。

  • 上記を踏まえて、オススメがStandardです。 legacy モードよりも多くのエラーやlimitに対応しています。