Skip to content

Instantly share code, notes, and snippets.

@bashtoni
Created June 26, 2017 07:39
Show Gist options
  • Save bashtoni/bfd6a5dc51aac7039ebd21581ea71e43 to your computer and use it in GitHub Desktop.
Save bashtoni/bfd6a5dc51aac7039ebd21581ea71e43 to your computer and use it in GitHub Desktop.
Delete AWS IAM User with MFA
#!/bin/bash
if [ -n $1 ]; then
echo "Usage: $0 <username>"
fi
user=$1
# First, find which groups the user is in and remove them from there
for group in $(aws iam list-groups-for-user --user-name $user --query Groups[].GroupName --output text); do
aws iam remove-user-from-group --user-name $user --group-name $group
done
# Now delete all their access keys
for accesskey in $(aws iam list-access-keys --user-name $user --query AccessKeyMetadata[].AccessKeyId --output text); do
aws iam delete-access-key --access-key-id $accesskey --user-name $user
done
# And now their MFA devices
for mfaserial in $(aws iam list-mfa-devices --user-name $user --query MFADevices[].SerialNumber --output text); do
aws iam deactivate-mfa-device --serial-number $mfaserial --user-name $user
aws iam delete-virtual-mfa-device --serial-number $mfaserial
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment