Skip to content

Instantly share code, notes, and snippets.

@mindfullsilence
Created April 13, 2023 15:21
Show Gist options
  • Save mindfullsilence/245b8f0bbc53136621ed37707ad31ed2 to your computer and use it in GitHub Desktop.
Save mindfullsilence/245b8f0bbc53136621ed37707ad31ed2 to your computer and use it in GitHub Desktop.
Add multiple accounts to your AWS cli, and activate them when needed

Add the contents of aws-profile.sh to your bash profile.

Use like the following:

Create a user in AWS IAM with CLI capabilities Get the users' access key and secret key

Add the profile to your CLI

aws-add-profile {the account name} {your access key} {your secret key}

# E.g.
aws-add-profile myaccount 123AKSDJLHSLKD KJHK-12321-12OIE-OI12E

Activate the profile:

aws-profile

> 1) default
> 2) myaccount
> 3) Quit
> Select a profile: 2|

> you chose myaccount

Your profile is now active, and will be used for aws-sdk cli commands.

```sh
export AWS_PROFILE=default
function aws-profile() {
PS3='Select a profile: '
unset AWS_PROFILES
while IFS= read -r LINE; do
AWS_PROFILES+=("${LINE}")
done < <(aws configure list-profiles)
select CHOSEN_PROFILE in "${AWS_PROFILES[@]}" "Quit"; do
case ${CHOSEN_PROFILE} in
"Quit")
break
;;
*)
echo "you chose ${CHOSEN_PROFILE}"
export AWS_PROFILE=$CHOSEN_PROFILE
break
;;
esac
done
}
function aws-add-profile() {
ACCOUNTNAME=$1
ACCESSKEY=$2
SECRETKEY=$3
echo "[$ACCOUNTNAME]" >> ~/.aws/credentials
echo "aws_access_key_id=$ACCESSKEY" >> ~/.aws/credentials
echo "aws_secret_access_key=$SECRETKEY" >> ~/.aws/credentials
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment