Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aws cognito list-users function only returns 60 users

I need to list all users of the cognito user-pool. Is there any way to return all users of the user-pool?

The list_users-function of boto3 - client like in the following code only returns 60 users instead of all of them.

client = boto3.client('cognito-idp',
                         region_name=aws_region,
                         aws_access_key_id=aws_access_key,
                         aws_secret_access_key=aws_secret_key,
                         config=config)

print('Setup client')

response = client.list_users(
UserPoolId=userpool_id,
AttributesToGet=[
    'email','sub'
] 
)

The expected result is a list of json-objects that includes all users of the cognito user-group

like image 292
C.Tomas Avatar asked Jan 26 '19 23:01

C.Tomas


2 Answers

You are seeing the expected result. You can request 60 or less users at a time. You will need to use pagination token to be able to go through all the users exist in the user pool. See the API documentation specifically Limit and PaginationToken parameters (emphasis mine):

Limit

Maximum number of users to be returned.

Type: Integer
Valid Range: Minimum value of 0. Maximum value of 60.
Required: No

and

PaginationToken

An identifier that was returned from the previous call to this operation, which can be used to return the next set of items in the list.

Type: String
Length Constraints: Minimum length of 1.
Pattern: [\S]+
Required: No

like image 67
A.Khan Avatar answered Sep 27 '22 22:09

A.Khan


@A.Khan's answer is correct. Below I've coded up 1 way to do this:

def get_all_users():
    cognito = boto3.client('cognito-idp')
    
    users = []
    next_page = None
    kwargs = {
        'UserPoolId': "whatever_your_user_pool_id_is"
    }

    users_remain = True
    while(users_remain):
        if next_page:
            kwargs['PaginationToken'] = next_page
        response = cognito.list_users(**kwargs)
        users.extend(response['Users'])
        next_page = response.get('PaginationToken', None)
        users_remain = next_page is not None

   return users
like image 29
Eric Avatar answered Sep 27 '22 22:09

Eric