Skip to content

Instantly share code, notes, and snippets.

@tomislacker
Created August 25, 2016 18:27
Show Gist options
  • Select an option

  • Save tomislacker/a6d6f181a6df7d7a4606a905d017ad1e to your computer and use it in GitHub Desktop.

Select an option

Save tomislacker/a6d6f181a6df7d7a4606a905d017ad1e to your computer and use it in GitHub Desktop.
Find AWS IAM Account a Key Belongs To
#!/bin/bash
get_keys_for_user ()
{
aws iam list-access-keys \
--user-name $@ \
| jq -r '.AccessKeyMetadata[].AccessKeyId'
}
get_all_users ()
{
aws iam list-users \
| jq -r '.Users[].UserName'
}
get_username_for_key ()
{
local this_key=$1
local found_user=
while read username
do
if get_keys_for_user $username | grep -q $this_key
then
found_user=$username
break
fi
done
if [ -n "$found_user" ]
then
echo $found_user
else
return 1
fi
}
get_username_for_key $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment