Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kshailen/16385df959b0875046e30139db2f0939 to your computer and use it in GitHub Desktop.
Save kshailen/16385df959b0875046e30139db2f0939 to your computer and use it in GitHub Desktop.
AWS IAM Get UserName by Access Key Id
#!/bin/bash
# exit when the command fails
set -o errexit;
# exit when try to use undeclared var
set -o nounset;
accessKeyToSearch=${1?"Usage: bash $0 AccessKeyId"}
for username in $(aws iam list-users --query 'Users[*].UserName' --output text); do
for accessKeyId in $(aws iam list-access-keys --user-name $username --query 'AccessKeyMetadata[*].AccessKeyId' --output text); do
if [ "$accessKeyToSearch" = "$accessKeyId" ]; then
echo $username;
break;
fi;
done;
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment