Skip to content

Instantly share code, notes, and snippets.

@hmdmph
Last active January 23, 2022 08:34
Show Gist options
  • Save hmdmph/dbabfaa6a6ce0f6dd20dc4ce0cf7bf88 to your computer and use it in GitHub Desktop.
Save hmdmph/dbabfaa6a6ce0f6dd20dc4ce0cf7bf88 to your computer and use it in GitHub Desktop.
Simple bash function to add AWS profile
#!/bin/bash
#
# DESCRIPTION:
# * simple aws function that help you to add aws profile by just passing arguments
#
# USAGE:
# 1. Save this function in your ~/.bashrc or ~/.bash_profile or ~/.profile
# 2. Edit the places where required
# 2.1 replace <REGION> with your default region. Well region can be parametrised if u wish :D.
# e.g. ap-southeast-1
# 2.2 replace <SOURCE-PROFILE> with your source profile in config.
# 2.3 replace <MFA-ARN> with your MFA ARN. If no MFA used then remove the whole line from the function.
# e.g. arn:aws:iam::123123123:mfa:/user@example.com
# 3. Save and use the source the file, so you can use it easily.
# 4. aws_add_profile <profile-name> <aws-account-id> <aws-role>
#
# This is generic function, you have to modify it before use.
#
function aws_add_profile(){
local config_file=~/.aws/config # change this path if yours different.
if [ "$#" -ne 3 ] ; then
echo -e "Usage: aws_add_profile <profile-name> <aws-account-id> <aws-role> \n\t Example: aws_add_profile admin-myawsp-rofile-dev 123654809 Admin"
else
profile=${1}
aws_acc_id=${2}
role=${3}
echo "
[profile ${profile}]
role_arn = arn:aws:iam::${aws_acc_id}:role/${role}
source_profile = <SOURCE-PROFILE>
mfa_serial = <MFA-ARN>
region = <REGION>
" >> $config_file
echo -e "AWS profile added successfully!"
fi
}
# Sample function modified to use with your AWS config.
function aws_add_profile(){
local config_file=~/.aws/config
if [ "$#" -ne 3 ] ; then
echo -e "Usage: aws_add_profile <profile-name> <aws-account-id> <aws-role> \n\t Example: aws_add_profile admin-myawsp-rofile-dev 123654809 Admin"
else
profile=${1}
aws_acc_id=${2}
role=${3}
echo "
[profile ${profile}]
role_arn = arn:aws:iam::${aws_acc_id}:role/${role}
source_profile = default
mfa_serial = arn:aws:iam::8767868768:mfa/user@example.com
region = ap-southeast-1
" >> $config_file
echo -e "AWS profile added successfully!"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment