SSH to an EC2 instance by name

Now my company has around 35 different EC2 instances (and growing), I needed a way to easily SSH to them by their 'Name' tag instead of randomly assigned DNS name as we still have pets not cattle (tut tut!). This simple script does the job quite nicely:

#!/usr/bin/env bash
#
# SSH to an EC2 instance by name
#
# You need the AWS cli (pip install awscli)
#

if [[ -z $1 ]]; then
  echo 'You must specify an instance name!'
  exit 1
fi

name=$1

instance=$(aws ec2 describe-instances \
  --output=text \
  --query 'Reservations[*].Instances[*].[PrivateIpAddress]' \
  --filters "Name=tag:Name,Values=$name")

if [[ -z $instance ]]; then
  echo "Instance $name not found!"
  exit 1
fi

ssh $instance