SSH to Auto Scaled EC2 instances

If you use Auto Scaling with AWS, the following script may come in handy.

Sometimes you just want to connect to a random auto scaled server or servers. Using this script you can simply run it once to get a random server or run it repeatedly to connect to all the servers in your auto scaling group.

I place the script at ~/bin/appserver and then run chmod +x ~/bin/appserver to make it executable.

It requires PHP and the AWS CLI┬áto be installed – you will also need to have permission to run the aws ec2 describe-instances command.

Setup is simple. Just set the path to your private key, your SSH username and change the autoscaling group or groups you wish to connect to.