data-bite
data-bite

Reputation: 417

Python boto3 - iam userlist limits to 100 users

I was working on a script to filter some users out of all iam user in the same account. It worked fine on one aws account, but when i used same script on another aws account with abuot 400+ users, it just skipped all users after first 100.

I assumed that something is limiting this, so i created below script which counts the number of users in given aws account. it stops at 100 and skips the rest of the users.

#!/usr/bin/env python3
import boto3

def main():
    Usercount = 0

    iam = boto3.client('iam')
    client = boto3.client('iam',aws_access_key_id=XXXXXXXXXXXXXX, aws_secret_access_key=XXXXXXXXXXXXXXXXXXXXXX)

    try:
        for user in client.list_users()['Users']:
            Usercount +=1
        print(Usercount)
    except:
        print("Error getting user count")

This prints right number if iam users are less than 100 but prints only 100 if users are more than 100. Is there a limit set via aws console? Is there a way to get rid of this limit?

Upvotes: 0

Views: 1241

Answers (2)

John Rotenstein
John Rotenstein

Reputation: 269666

When calling list_users(), if IsTruncated is True, then you should make a subsequent call with Marker equal to the value that was returned in the call.

Or, you can simply use a paginator that will do it for you:

paginator = client.get_paginator('list_users')

response_iterator = paginator.paginate()

for user in response_iterator:
  Usercount += 1

Upvotes: 2

Tobias Bruckert
Tobias Bruckert

Reputation: 397

You have to use a while loop and validate the response for the existence of the marker.

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.list_users

Marker (string) -- Use this parameter only when paginating results and only after you receive a response indicating that the results are truncated. Set it to the value of the Marker element in the response that you received to indicate where the next call should start.

Upvotes: 2

Related Questions