Skip to content

Instantly share code, notes, and snippets.

@ashwiniag
Last active April 20, 2020 05:17
Show Gist options
  • Select an option

  • Save ashwiniag/3739b71a096aff83c3b7cd53805279d9 to your computer and use it in GitHub Desktop.

Select an option

Save ashwiniag/3739b71a096aff83c3b7cd53805279d9 to your computer and use it in GitHub Desktop.
To create/ delete AWS IAM user.
#aws_access_key_id and aws_secret_access_key are displayed just once hence note down it safely
#Just sharing the AWS-GK :P
#For each user only Two times aws_secret_access_key can be created.
# /bin/bash createuser.sh create_user/delete_user
#!/bin/bash
set -euo pipefail
declare -r aws_profile="<value>"
declare -r user_name="<value>"
declare -r service_family="<value>"
declare -r command=${1:?"$(echo "FATAL YOU WANT TO "add" OR "delete" PARAMETERS ?")"}
read -p "Ehhh!! Have you set the variables right Monsieur :P ? y/n " input
if [ $input != 'y' ];
then
exit 1
fi
function create_user {
#create user
aws iam create-user --user-name ${user_name} --path /${service_family}/ --profile ${aws_profile}
## create credentials for user and storing in credentials environment variables in the form of array.
credentials=($(aws iam create-access-key --user-name ${user_name} --profile ${aws_profile} | jq -r ".AccessKey.AccessKeyId, .AccessKey.SecretAccessKey " ) )
## Displaying credentials
echo "Displaying credentials of user:: ${user_name}. These Credentials are displayed just once, therefore store it safely."
echo "aws_access_key_id = ${credentials[0]}"
echo "aws_secret_access_key = ${credentials[1]}"
}
function delete_user {
# Get access key id of application user
ID=($(aws iam list-access-keys --user ${user_name} --profile ${aws_profile} | grep "AccessKeyId" | sed 's/\"/ /g' | awk '{print $3}'))
#Delete the access keys of application user using id
aws iam delete-access-key --access-key-id ${ID[0]} --user-name ${user_name} --profile ${aws_profile}
#Now delete the user
aws iam delete-user --user-name ${user_name} --profile ${aws_profile}
}
case $command in
"create_user") create_user
;;
"delete_user") delete_user
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment