If you’re not ponying up an extra few bucks per month for an EC2 Elastic IP it can be a pain in the ass to reach your EC2 instances, particularly after they’ve rebooted and gotten a new IP and hostname. Fortunately it is possible, though not obvious, to retrieve the public DNS name of an EC2 instance based on its Instance ID and Availability Zone.
To this end I’ve created two scripts, a Python script [ec2-get-dns] that uses boto to retrieve the hostname, and a bash script [ec2-ssh] that gets the hostname and connects via SSH.
Oh wait, you say it’s a pain in the ass to remember instance IDs? Really? I always know that i-35213355 is the web server and i-54632856 is the database server, but OK. This also reads named shortcuts stored in a file [.ec2list] in your home directory.
#shortcut-name:region:instance-id[:username] web-server:us-west-2:i-35213355 db-server:us-west-2:i-54632856:db-user
As you can see above the username is optional. Connecting to the ‘web-server’ entry will default to ‘ec2-user’ while ‘db-server’ will use ‘db-user’. Continue reading…