Created
January 30, 2019 08:55
-
-
Save davyngugi/7f533a50e78fa0e69134098356f12c2f to your computer and use it in GitHub Desktop.
Iteratively retrieve all IAM users with the groups, roles and date created then save to csv file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
iam = boto3.client('iam',aws_access_key_id="XXXX",aws_secret_access_key="XXXX") | |
user_list = [] | |
users = (iam.get_account_authorization_details(Filter=['User'], MaxItems=1000)) | |
while users['IsTruncated']: | |
marker = users['Marker'] | |
for user_detail in users['UserDetailList']: | |
policyname = [] | |
# find each policy attached to the user | |
for policy in user_detail['AttachedManagedPolicies']: | |
policyname.append(policy['PolicyName']) | |
user_details = { | |
"user": user_detail['UserName'], | |
"groups": ", ".join(user_detail['GroupList']), | |
"policyname": ", ".join(policyname), | |
"created": user_detail['CreateDate'] | |
} | |
user_list.append(user_details) | |
users = iam.get_account_authorization_details(Filter=['User'], MaxItems=1000, Marker=marker) | |
import csv | |
keys = user_list[0].keys() | |
with open('aws_users.csv', 'wb') as output_file: | |
dict_writer = csv.DictWriter(output_file, keys) | |
dict_writer.writeheader() | |
dict_writer.writerows(user_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment