Skip navigation

While looking for a way to name instances and update the /etc/hosts file, I came across Tim Dysinger’s blog entry.  It’s a ruby script that parses AWS metadata to get the internal IP address of the running instances in your AWS account and its respective keypair – perfect for /etc/hosts entries.  Obviously, the caveat here is that you need to generate 1 unique ssh private key for every instance you are running because key == internal dns name.  You’ll be managing a lot of ssh keys in the end.

This is perfect for situations where your instances interact with each other (load balancers in front of app servers with database servers at the back?).  It’s very handy calling your instances lb0, app23  or db3 instead of something like  Automatic, too.  If you’re running the script as a cron job, your /etc/hosts file gets updated if you run new instances.

#!/usr/bin/env ruby
%w(optparse rubygems EC2 resolv pp).each { l require l }
options = {}
parser = do p
  p.banner = "Usage: hosts [options]"
  p.on("-a", "--access-key USER", "The user's AWS access key ID.") do aki
    options[:access_key_id] = aki
       "--secret-key PASSWORD",
       "The user's AWS secret access key.") do sak
    options[:secret_access_key] = sak
  p.on_tail("-h", "--help", "Show this message") {
  p.parse!(ARGV) rescue puts(p)
if options.key?(:access_key_id) and options.key?(:secret_access_key)
  puts " localhost" do r
    r.instancesSet.item.each do i
      if =~ /running/
        puts( +
             " #{i.keyName}.ec2 #{i.keyName}")

Just pass on your AWS access key and secret key as parameters, pipe it to /etc/hosts and you’re good to go.

One Trackback/Pingback

  1. […] that came to my mind is to use them for maintaining internal DNS. The two blog posts here and here describe how to do this using the name of the ssh key that was used to create the instance. However […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: