I work in many AWS environments on a regular basis so I need to know which one I am signed into and in which region at a glance. I also need to be able to switch quickly and easily between environments as well as login to accounts.

Here are some of MY tips and tools to accomplish these needs. This is by no means a complete list.

I rely heavily on Oh My Zsh for much of this functionality.

Add the AWS plugin to the oh my zsh configuration by opening the ~/.zshrc file in your favorite editor

Find the plugins line and add aws to the list of plugins

plugins=(aws {other plugins})

save and exit the file

you will need to restart or reload you shell to add the plugin to the running environment

running source ~/.zshrc will do that, or exit and restart the shell.

The following image demonstrates two immediate benefits:

  1. My current AWS profile and region are displayed in my command prompt

  2. The Oh My Zsh plugin provides the asp command that will assign the profile I pass as a parameter.
    The command asp on its own will clear the current environment.

The Oh My Zsh plugin provides the asp command that will assign the profile

But wait there is more: if you have an SSO login configured in

~/.aws/config

then drop this script into your custom directory:

~/.oh-my-zsh/custom/sso.zsh
sso() {
aws --profile "$1" sso login --no-browser
export AWS_PROFILE="$1"
export AWS_DEFAULT_PROFILE="$1"
export AWS_DEFAULT_REGION=us-east-2
}

Now to login to any profile use sso profile for example sso castlerock
this will print the sso login url out, which you can capture with xclip into a browser of your choice for that environment to authenticate.

For a short cut for changing to commonly used AWS regions I add aws.zsh to my custom directory as follows (of course you will want to pick your most commonly used regions and the short cut names you want to use):

# REGIONS
ca(){
echo "setting AWS Canada Central 1"
export AWS_DEFAULT_REGION=ca-central-1
export AWS_REGION=ca-central-1
}
va(){
echo "setting AWS Virginia us-east-1"
export AWS_DEFAULT_REGION=us-east-1
export AWS_REGION=us-east-1
}

Again, save this file and reload your shell by exiting or running source ~/.zshrc
This enables you to simply type ca or va to set your local environment to that chosen region.

More Shortcuts

Add these to your own *.zsh file in the custom directory, or append them to the existing aws.zsh, up to you

listec2() {
aws ec2 describe-instances --output json --query Reservations |
jq '.[] |.Instances[] | {InstanceId: .InstanceId,
Name: (.Tags[] | select(.Key=="Name").Value), InstanceState: .State}'
}
listrds() {
aws rds describe-db-clusters
--query 'DBClusters[].[DBClusterIdentifier, Endpoint]'
--output text
}
ssm(){
aws ssm start-session --target $1
}

These list ec2 instances and RDS clusters in the current environment. Obviously, there is a lot more you can do with these other than these examples.

The last one is useful for starting an SSM session on an ec2 instance. If I know the instance-id (from listec2) then use ssm i-123456 to start a console session.

Remember to reload your environment when adding functions and values to oh my zsh.