Aaron

Add new file

import csv
import boto3
import argparse
###
# To run this script you need to be authenticated in AWS already.
#
# Export your user access key and password to ~/.aws/credentials
#
# - or -
#
# If you're running this on an EC2 instance, and the instance has
# role with permissions to read IAM users, you export the profile:
#
# export AWS_DEFAULT_PROFILE=env-name
#
# - or -
#
# export AWS_ACCESS_KEY_ID=AKIAIO5FODNN7EXAMPLE
# export AWS_SECRET_ACCESS_KEY=ABCDEF+c2L7yXeGvUyrPgYsDnWRRC1AYEXAMPLE
#
# - or -
#
# Finally, if you just want to hard code your creds, initialize
# your client like this:
#
# client = boto3.client(
# 'iam',
# aws_access_key_id='AKIAIO5FODNN7EXAMPLE',
# aws_secret_access_key='ABCDEF+c2L7yXeGvUyrPgYsDnWRRC1AYEXAMPLE'
# )
###
def get_all_headers(data):
keys = []
for item in data:
for key in item.keys():
if key not in keys:
keys.append(key)
return keys
if __name__ == '__main__':
# Parse CLI args
argp = argparse.ArgumentParser(description='Export AWS users to CSV')
argp.add_argument('--file', '-f', required=True, dest='outfile',
action='store', help='CSV file to export data into')
args = argp.parse_args()
# Initialize client
client = boto3.client('iam')
user_data = client.list_users()
header = get_all_headers(user_data['Users'])
# Write data to file
with open(args.outfile, 'w', newline='') as f:
writer = csv.DictWriter(f, fieldnames=header)
writer.writeheader()
for user in user_data['Users']:
writer.writerow(user)
... ...