Using Amazon EC2 IDs Instead of Names

When working with cloud resources, it’s often useful to know the Amazon EC2 ID of the resource instead of using a name. This is especially true when tying data together into multiple systems and connect between them. Using Amazon EC2 IDs can be useful when tying data into into multiple systems and connecting between them. This guide will show you how to determine them. Luckily, instances in both Amazon EC2 and Rackspace Cloud have an associated instance id that you can acccess from within the instance without needing any special API access.

Getting this information in EC2 is pretty straight-forward. EC2 has a metadata service that listens as an HTTP server on a link-local IP address that provides you with a variety of information about the instance. You can explore it simply with the curl command from your instance. Run

curl http://169.254.169.254/latest/meta-data/

to see a list of some of the information which is available. And thus, to get the instance id, you can just use

curl http://169.254.169.254/latest/meta-data/instance-id

and the instance id will be returned.

Although OpenStack provides a similar metadata service, it is not enabled in the deploy used by Rackspace’s public cloud offering. Luckily, the information *is* available, it’s just not as easily accessible as you have to use Xenstore to read it out ( note: this means it’s accessible to only the root user). Xenstore is a generic feature provided by the Xen hypervisor for sharing config information between the host system and the guest. Rackspace puts some information about your instance there. So from your Rackspace Cloud instance, you can run

/usr/bin/xenstore-read name

and you’ll get back a string ‘instance-INSTANCEID’. So if you want to just get the instance id, you can do this with

/usr/bin/xenstore-read name |sed -e 's/^instance-//'

Other information can be found by looking at

/usr/bin/xenstore-ls vm-data

You can then use this information in config files or anywhere else you want on the system.  If you’re using puppet or chef, then check out facter or ohai as they already provide easy access to this information for EC2 instances and will soon also provide the Rackspace Cloud instance information.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>