awsp
is a tiny shell function to quickly switch AWS profiles based on AWS SSO account names. It uses your existing SSO session cache and sets the AWS_PROFILE
environment variable in your shell.
Given an account name (e.g., media-prod
), it:
- Locates your active AWS SSO session token.
- Lists all available accounts via
aws sso list-accounts
. - Finds the account ID associated with your input.
- Constructs the profile name as
SharedAdministratorAccess-ACCOUNT_ID
. - Sets the
AWS_PROFILE
accordingly.
- Save the function into a file:
~/.oh-my-zsh/custom/aws.zsh
- Reload your shell:
source ~/.zshrc
β Tip: Oh My Zsh automatically loads any .zsh file in the ~/.oh-my-zsh/custom/ directory on startup.
- Add the function to your ~/.bashrc:
nano ~/.bashrc
-
Paste the function and save the file.
-
Reload your shell:
source ~/.bashrc
awsp <account-name>
Example:
awsp media-prod
Output:
β
AWS_PROFILE set to 'SharedAdministratorAccess-123456789012'
π Optional: Check Current AWS_PROFILE
If you run awsp without arguments, it will:
Show usage instructions
Display your current AWS_PROFILE (broken into role and account ID)
Example:
awsp
Usage: awsp <account-name>
π Currently set AWS_PROFILE: SharedAdministratorAccess-123456789012
ββ Role: SharedAdministratorAccess
ββ Account ID: 123456789012
- An active AWS SSO session (run aws sso login if needed)
- jq must be installed (brew install jq on macOS)
- AWS CLI v2 with SSO configured
- Fuzzy matching for account names
- Override default IAM role name
- Caching for performance
- Auto login if session is expired
MIT β use, share, and modify freely !