Forked from cauealvesbraz/aws-iam-get-username-by-access-key.bash
Created
April 29, 2020 07:03
-
-
Save kshailen/16385df959b0875046e30139db2f0939 to your computer and use it in GitHub Desktop.
AWS IAM Get UserName by Access Key Id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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