Viv*_*ble 3 amazon-web-services python-3.x boto3 aws-lambda
我从 boto3 组织的主 AWS 账户中获取所有子账户。代码运行良好。我可以获得儿童帐户列表。但是,如果您再次运行我的 AWS Lambda 函数,则它无法获取子帐户。
出现以下错误:
Error while getting AWS Accounts : An error occurred (TooManyRequestsException) when calling the ListAccounts operation: AWS Organizations can't complete your request because another request is already in progress. Try again later
Run Code Online (Sandbox Code Playgroud)
20 到 30 分钟后,我可以看到我的代码一次又一次地引发上述异常。
我通过 AWS Gateway + AWS Lambda 运行此代码。
任何想法?
代码:
import boto3
class Organizations(object):
"""AWS Organization"""
def __init__(self, access_key, secret_access_key, session_token=None):
self.client = boto3.client('organizations',
aws_access_key_id=access_key,
aws_secret_access_key=secret_access_key,
aws_session_token=session_token
)
def get_accounts(self, next_token=None, max_results=None):
"""Get Accounts List"""
if next_token and max_results:
result = self.client.list_accounts(NextToken=next_token,
MaxResults=max_results)
elif next_token:
result = self.client.list_accounts(NextToken=next_token)
elif max_results:
result = self.client.list_accounts(MaxResults=max_results)
else:
result = self.client.list_accounts()
return result
class AWSAccounts(object):
""" Return AWS Accounts information. """
def get_aws_accounts(self, access_key, secret_access_key, session_token):
""" Return List of AWS account Details."""
org_obj = Organizations(access_key=access_key,
secret_access_key=secret_access_key,
session_token=session_token)
aws_accounts = []
next_token = None
next_result = None
while True:
response = org_obj.get_accounts(next_token, next_result)
for account in response['Accounts']:
account_details = {"name": account["Name"],
"id": account["Id"],
"admin_role_name": self.account_role_name
}
aws_accounts.append(account_details)
if "NextToken" not in response:
break
next_token = response["NextToken"]
return aws_accounts
Run Code Online (Sandbox Code Playgroud)
小智 7
配置您的boto3客户端以使用内置的标准重试模式:
import boto3
from botocore.config import Config
config = Config(
retries = {
'max_attempts': 10,
'mode': 'standard'
}
)
ec2 = boto3.client('ec2', config=config)
Run Code Online (Sandbox Code Playgroud)
根据文档,默认模式是“legacy”,它不处理TooManyRequestsException.
请参阅有关重试配置的 boto3 文档: https: //boto3.amazonaws.com/v1/documentation/api/latest/guide/retries.html
| 归档时间: |
|
| 查看次数: |
9804 次 |
| 最近记录: |