Skip to content

Instantly share code, notes, and snippets.

@gjyoung1974
Last active December 21, 2017 19:19
Show Gist options
  • Save gjyoung1974/b00fbbc7fa357e815bc29ec2511455b8 to your computer and use it in GitHub Desktop.
Save gjyoung1974/b00fbbc7fa357e815bc29ec2511455b8 to your computer and use it in GitHub Desktop.
aws_iam_access_key_age.py
import boto3
import datetime
import time
# AWS Client>>
profile = 'some-profile'
boto3.setup_default_session(profile_name=profile)
client = boto3.client('iam')
# Array of users
response = client.list_users() # List all the users
for user in response["Users"]:
username = user['UserName']
# Array of access keys for 1 user
res = client.list_access_keys(UserName=username) # for each user get the each access key
for key in res['AccessKeyMetadata']:
# Format the dates
accesskeydate = key['CreateDate']
accesskeydate = accesskeydate.strftime("%Y-%m-%d %H:%M:%S")
currentdate = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
accesskeyd = time.mktime(datetime.datetime.strptime(accesskeydate, "%Y-%m-%d %H:%M:%S").timetuple())
currentd = time.mktime(datetime.datetime.strptime(currentdate, "%Y-%m-%d %H:%M:%S").timetuple())
active_days = (currentd - accesskeyd) / 60 / 60 / 24 # We get the data in seconds and convert it to days
if active_days >= 90:
if key['Status'] == 'Active':
print key['UserName'] + ', ' + key['AccessKeyId'] + ", " + str((int(round(active_days))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment