TooManyRequestsException: Rate exceeded. while calling codnito-idp admin_get_user function
I’m using AWS sdk in my Python Lambda function to get Cognito User’s attributes. Like this code:
client = boto3.client('cognito-idp') try: response = client.admin_get_user( UserPoolId=pool_id, Username=email ) if response is not None and 'Username' in response and 'UserAttributes' in response: res = response['UserAttributes'] return res else: return None except ClientError as e: if e.response['Error']['Code'] == 'UserNotFoundException': print("USER NOT FOUND") return None raise e return None
When i am trying to get multiple users (around 50 users) using the AdminGetUser API, I am getting TooManyRequestsException error.
What is the default Limit to get users Info from Cognito per second according to the AWS Doc? I see this page Quotas in Amazon Cognito but I can not see what is this default Limit.
What is the best way to handle this?
Right now I’m planning to wait for 1 second then I will try to get user’s attributes from the same user while i get the same exception.
Thanks!
According to that Quotas page you mentioned, the limit is 5/second.
Soft Limits in Amazon Cognito User Pools APIs
…
Admin APIs not listed above. 5
You need to throttle your requests, but the best approach depends on other aspects of your solution.
A few examples:
If your lambda function is being invoked 50 times, one invocation per user, you can set the maximum concurrency for your lambda. (you can find more info here and here ).
If a single lambda call iterates all 50 items at once, then you can add a time.sleep
call on each iteration, or use some rate limiting library (this one seems to do the task: https://pypi.org/project/ratelimit/). In this case, in addition to this approach, you would probably want to limit the concurrency of your lambda to just 1 .