Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save cauealvesbraz/1121c0a0375648db13b137b31ef8955d to your computer and use it in GitHub Desktop.
Save cauealvesbraz/1121c0a0375648db13b137b31ef8955d 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