Automounting Amazon EBS volumes on EC2 instances

I've been using S3 to store semi-transient information like log files from EC2 nodes in the past but recently decided to give Amazon's Elastic Block Store (EBS) a try instead. I quickly realized a downside to using EBS in that there is no mechanism for auto-attach and mounting volumes when an AMI is launched. This is probably something Amazon will fix at some point and allow you to launch a given AMI with an attached EBS volume but until then you need some way of doing it yourself. The following is a simple way of using ruby to make it happen.

I'm going to assume you have already created your EBS volume, if you haven't you can learn more about that from the docs. You will need to make sure ruby is installed on the AMI you are going to use and that the RightScale AWS gem is installed as well.

The following script grabs the instance id from the EC2 metadata URL. It then uses the RightScale EC2 calls to attach the volume to the current EC2 instance. After the attach call it may take a few seconds for the volume to become ready so the script sleeps for a few seconds before calling out to the system to mount the device. One enhancement that is obvious here would be to have the script make a RightScale EC2 call to determine when the volume is really ready and then continue after that.


require 'rubygems'
require 'right_aws'
require 'net/http'

url = ''
instance_id = Net::HTTP.get_response(URI.parse(url)).body

AMAZON_PUBLIC_KEY='your public key'
AMAZON_PRIVATE_KEY='your private key'
EC2_LOG_VOL='the volume id'


vol = ec2.attach_volume(EC2_LOG_VOL, instance_id, '/dev/sdh')
puts vol

# It can take a few seconds for the volume to become ready. 
# This is just to make sure it is ready before mounting it.
sleep 20

system('mount /dev/sdh /mymountpoint')

I called the script mountlogs.rb and call it out of the local startup script /etc/rc.local so it mounts the disk on startup. This seems to work pretty well as a stopgap until Amazon puts in place a way to auto-attach EBS volumes to instance creation.

Leave a Reply

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