Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dreamingbinary/ec0fc855b580dc6011c73832aa73cb73 to your computer and use it in GitHub Desktop.
Save dreamingbinary/ec0fc855b580dc6011c73832aa73cb73 to your computer and use it in GitHub Desktop.
Find an AWS IAM user corresponding to an AWS Access Key (boto3)
# Find the IAM username belonging to the TARGET_ACCESS_KEY
import boto3
from botocore.exceptions import ClientError
iam = boto3.client('iam')
def find_user(key):
try:
key_info = iam.get_access_key_last_used(AccessKeyId=key)
return key_info['UserName']
except ClientError as e:
print "Received error: %s", e
if e.response['Error']['Code'] == 'AccessDenied':
return "Key does not exist in target account"
try:
print find_user("AKIAXXXXXXXXXXXXXXXX")
except ClientError as e:
print "Received error: %s", e
if e.response['Error']['Code'] == 'ExpiredToken':
print "Please login to the target AWS account"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment