Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akosveres/f2bd41bfc17e8edfc3edec0eeae747b7 to your computer and use it in GitHub Desktop.
Save akosveres/f2bd41bfc17e8edfc3edec0eeae747b7 to your computer and use it in GitHub Desktop.
boto3 aws find all IAM accesskeys details for the account
import boto3
resource = boto3.resource('iam')
client = boto3.client("iam")
KEY = 'LastUsedDate'
for user in resource.users.all():
Metadata = client.list_access_keys(UserName=user.user_name)
if Metadata['AccessKeyMetadata'] :
for key in user.access_keys.all():
AccessId = key.access_key_id
Status = key.status
LastUsed = client.get_access_key_last_used(AccessKeyId=AccessId)
if (Status == "Active"):
if KEY in LastUsed['AccessKeyLastUsed']:
print("User: " , user.user_name , "Key: " , AccessId , "AK Last Used: " , LastUsed['AccessKeyLastUsed'][KEY])
else:
print("User: ", user.user_name , "Key: ", AccessId , "Key is Active but NEVER USED")
else:
print("User: ", user.user_name , "Key: ", AccessId , "Keys is InActive")
else:
print ("User: ", user.user_name , "No KEYS for this USER") #".. proof: " , Metadata
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment