C.Tomas
C.Tomas

Reputation: 461

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

Upvotes: 9

Views: 13301

Answers (2)

Eric
Eric

Reputation: 4301

@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

Upvotes: 11

A.Khan
A.Khan

Reputation: 3992

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

Upvotes: 10

Related Questions