I’ve been a longtime user of the excellent Elastics utility for the Mac for managing connections to Amazon Web Services EC2 instances: it provides a handy menu bar item with a drop-down list of running instances, with a link to connect to each via SSH.
Unfortunately Elastics has been slowly dying on the vine with each new version of Mac OS, and with recent updates to High Sierra it’s stopped working completely for me so I needed a replacement. While there have been various attempts to build alternative “manage your AWS instances in a GUI” utilities, none has the elegance of Elastics, and many of the features are overkill for me: all I really need is a quick way of SSHing to an instance by instance name.
Here’s what I built.
First, I made a shell script call aws-connect.sh that looks like this:
#!/bin/sh
aws ec2 describe-instances --filters "Name=tag:Name,Values=$1" --query "Reservations[0].Instances[0].PublicDnsName"
It uses the AWS command line utility aws to return the public DNS name of the instance I pass it on the command line. So if I do:
./aws-connect.sh slave
it will return the public DNS of an instance I’ve named “slave,” like this:
"ec2-164-71-182-18.compute-1.amazonaws.com"
I then created a workflow, another shell script, in Alfred that looks like this:
export EC2HOST=`~/bin/aws-connect.sh {query} | tr -d '"'`
ssh -t -p 22 -i 'keypair.pem' ec2-user@$EC2HOST
This script puts the public DNS of the named instance (passed from Alfred as {query}), with the quotes stripped out, into the EC2HOST shell variable, and then SSHes to that instance using my AWS keypair.
When this is all glued together, connecting to an AWS instance, assuming I know the name, is as simple as invoking Alfred (with Control + Space in my setup) and typing ec2 [instance name]: a Terminal window opens, and I’m connected to the instance. It doesn’t replace everything that I used Elastics for, but it does replace the most important thing, and that’s a big help.
Add new comment