CLI to switch aws profiles
I do this for fun on my free time, so if you have any issues or suggestions, please add an issue to the repository, and if I have time I'll try to do it, but keep in mind I can be months without checking the repository again! So feel free to fork the repository if you want and do all the changes you want/need!
The name is a tribute to a friend. It's probably not the best name for an aws profile switcher but... well, it means something to me 😊
The first thing you need to know is: don't use go install github.com/belitre/canu, that would add canu as an executable to your $GOPATH/bin with name canu, and we don't want that!
Also, canu will only read profiles from the aws profiles config file that match the pattern [profile profile-name] with sso_start_url, sso_account_id and sso_role_name defined. That means it will not show the default profile, or any other profile that is not using SSO. For example, if your config file looks like this:
[default]
region = us-east-1
[profile test]
region = us-east-2
[profile admin]
sso_start_url = https://d-93456h53g77.awsapps.com/start
sso_region = us-east-1
sso_account_id = 987654321
sso_role_name = MyRoleName
region = us-east-1
canu will only show the profile admin in the menu.
- Download the binary for your OS/ARCH from the release page and unpack it in a folder available in your
$PATH(I'll use for the example$GOPATH/bin):I recommend to download thecurl -sL https://github.com/belitre/canu/releases/download/1.1.0/canu-darwin-arm64.tar.gz | tar -zxf - -C $GOPATH/bin_canubinary in a folder where your user has permissions to write. Of course, as a user, you can download the binary to a folder that requires root permissions, but then you will need to run theinstallcommand with root permissions as well, and unless you are usingsudo, which should keep the correct value for your user in the$HOMEenv var, you may need to set the path where you wantcanuto write the alias, to be sure it writes it to the user shell config file, not the root one. More information about this in the next section. - Run
_canu install. Running this command,canuwill install with the default settings. That means it will use as default config path for your aws config:$HOME/.aws/config, and it will use$HOME/.bash_profileto add the alias. If you have different settings, please check the next section to learn how to customise yourcanuinstallation. - Restart your shell, or run the alias returned in the
_canu installoutput. - Run
canuand enjoy!
What happens during the installation is:
canuwill create a shell script in the same folder the binary is downloaded with the name of the alias (by default this name iscanu, but you can set a different name, check the next section to learn how). This is the reason you will need to run_canu installwith root permissions if you download the_canubinary to a folder where your user doesn't have write permissions!canuwill add an alias to your$HOME/.bash_profilethat will source the generated shell script, this is howcanusets the selected profile in theAWS_PROFILEenvironment variable.
Once you downloaded the binary, you can check the available flags running _canu install -h:
installs canu aliases
Usage:
_canu install [flags]
Flags:
--alias-name string name of the alias (default "canu")
-c, --config-path string Path with the AWS config file with the defined profiles (default "/Users/miguel/.aws/config")
-e, --exclude stringArray Profiles containing any of the excludes will be ignored. Exclude takes preference over include (ignores upper/lower case)
-h, --help help for install
-i, --include stringArray Profiles containing any of the includes will be available to select (ignores upper/lower case)
--shell-config string path to the shell config script (default "/Users/miguel/.bash_profile")
--skip-alias if provided it won't add an alias to the shell config script
-s, --sort If provided it will sort the profiles, by default the profiles will use the same order you have in your aws config file
There are two different kind of flags here:
- Flags related with the installation:
--alias-name string name of the alias (default "canu") --shell-config string path to the shell config script (default "/Users/miguel/.bash_profile") --skip-alias if provided it won't add an alias to the shell config script--alias-name: This is the name of the aliascanuwill create, also, it will be the name of the shell script generated to set the correct value for theAWS_PROFILEenvironment variable.--shell-config: The path to your shell profile config file. By defaultcanuwill use$HOME/.bash_profile. If you usezshyou can run the install command with--shell-config $HOME/.zshrc--skip-alias: If this flag is providedcanuwon't add the alias to your shell profile config file.
- The other flags will change the behaviour when running the generated alias:
-c, --config-path string Path with the AWS config file with the defined profiles (default "/Users/miguel/.aws/config") -e, --exclude stringArray Profiles containing any of the excludes will be ignored. Exclude takes preference over include (ignores upper/lower case) -i, --include stringArray Profiles containing any of the includes will be available to select (ignores upper/lower case) -s, --sort If provided it will sort the profiles, by default the profiles will use the same order you have in your aws config file-c, --config-path: The path where the aws config for your profiles is located, by default it will use$HOME/.aws/config-e, --exclude: Flag to exclude profiles, multiple values can be provided.canuwill exclude the profiles containing any of the provided values with this flag. This flag takes preference over--include-i, --include: Flag to include profiles, multiple values can be provided.canuwill include only the profiles containing any of provided values with this flag.-s, --sort: If this flag is providedcanuwill sort the profiles alphabetically before showing the menu. By default the order of the profiles will be the same one you have in your aws profiles config file.
- Install
canuwith aliasprofile-switcher, using as shellzsh:_canu install --shell-config /Users/my-user/.zshrc --alias-name profile-switcher - Install
canuwith default values for installation, excluding profiles with the stringtestanddev, sorting the list and using a different path for the aws profiles config file while running:_canu install -e test -e dev -s -c /Users/my-user/.aws/custom-config - Install
canuwith all custom settings:This will install_canu install --alias-name profile-switcher --shell-config /Users/my-user/.zshrc -c /Users/my-user/.aws/custom-config -e test -e dev -i account1 -i account2 -scanuwith the aliasprofile-switcherusing thezshshell config file to create the alias, excluding profiles containingtestanddev, and including profiles containingaccount1andaccount2, and sorting the profiles before showing the menu. - Install multiple aliases for different profiles:
This will create two aliases, one called
_canu install --alias-name canu-dev -i dev _canu install --alias-name canu-prod -i prodcanu-devthat will include only profiles containingdev, and another one calledcanu-prodthat will include only profiles containingprod.
The uninstall process will delete the script created by _canu install and remove the alias added if it's found in the file.
The available flags for uninstall are:
./bin/_canu uninstall -h
uninstalls canu aliases
Usage:
_canu uninstall [flags]
Flags:
--alias-name string name of the alias (default "canu")
-h, --help help for uninstall
--shell-config string path to the shell config script (default "/Users/miguel/.bash_profile")
If you used the default installation, you can just run:
_canu uninstall
In case you used a different alias or a different shell config file to add the alias you can run:
_canu uninstall --alias-name profile-switcher --shell-config /Users/my-user/.zshrc