Skip to content

Instantly share code, notes, and snippets.

@xxxVxxx
Last active November 25, 2022 06:52
Show Gist options
  • Save xxxVxxx/9648264fd6f41bbb1f65 to your computer and use it in GitHub Desktop.
Save xxxVxxx/9648264fd6f41bbb1f65 to your computer and use it in GitHub Desktop.
boto3 aws find all IAM accesskeys details for the account
import boto3
boto3.setup_default_session(profile_name='IAM')
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
@siva-sph
Copy link

AttributeError: 'IAM' object has no attribute 'resource'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment