A nifty little CLI tool to SSH to EC2 instances using their names
Have you ever done this?
- Login to EC2 Console.
- Search for an instance.
- Copy its IP address.
- Switch to a terminal.
- Type
ssh
and paste in the IP. - Repeat 2-5 for other instances.
I've been in that situation more times than I care to admit. Enter AWSer: A simple Python script that will make your life 95% easier... at least when it comes to SSH'ing into EC2s!
- Download the
awser.py
script and place it in a folder in your path. - [optional] Create a short symlink or alias such as
a
that points to the script. - Install boto3 if you don't already have it installed:
pip3 install boto3 --user
- Make sure you've configured your AWS credentials via
aws configure
. - Start using AWSer!
usage: awser.py [-h] [-r REGION] [-u USER] [-i IDENTITY]
keywords [keywords ...]
Quickly SSH to EC2 instances by name
positional arguments:
keywords Keyword(s) to filter list of servers, i.e. 'core 21a'
optional arguments:
-h, --help show this help message and exit
-r REGION, --region REGION
Overrides the default AWS region.
-u USER, --user USER Specifies a user for SSH.
-i IDENTITY, --identity IDENTITY
Selects a file from which the identity (private key)
is read.
-d, --public-dns Use public DNS name instead of IP address to connect.
Don't want to use Python? Try this little Bash alternative (requires fzf
and jq
to be installed):
function awser() {
if [[ ! $1 ]]; then
echo "Must provide a search term, i.e. 'trading'."
return 1
fi
local instances=$(aws ec2 describe-instances --filter Name=tag:Name,Values="*$1*" --query 'Reservations[].Instances[].{Name:Tags[?Key==`Name`].Value,IP:PrivateIpAddress}')
local instance=$(echo "$instances" | jq '.[] | (.Name | .[]) + ": " + .IP' | sed 's/"//g' | fzf -1 -0 --header "Select an instance" | awk -F": " '{print $2}')
if [[ $instance ]]; then
ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 $instance
else
echo "No match found! Please check the term you provided."
fi
}