Is it possible to create a EBS volume from and attach it to the same instance? will boto be of some help?

link|improve this question
feedback

2 Answers

up vote 3 down vote accepted

Yes it is. Set your credentials as environment variables (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY), install boto and fire up Python:

 >>> import boto
 >>> ec2 = boto.connect_ec2()
 >>> reservations = ec2.get_all_instances()
 >>> ins = reservations.instances[0]        # say ins is *this* instance
 >>> vol = ec2.create_volume(10, ins.placement)
 >>> vol.attach(inst.id, '/dev/sdh')
 u'attaching'

The above assumes you have only one instance started. If you know your instance id (you should!) you can simply match on the instance ids over all instances for all reservations.

Eventually you can simply format your volume and mount it:

 # mkfs -t ext3 /dev/sdh
 # mkdir /volume
 # mount -t ext3 /dev/sdh /volume
link|improve this answer
feedback

I think getting the instance ID dynamically is a better option. You can improve the above script with a GET request to the following URL within the instance.

http://169.254.169.254/2009-04-04/meta-data/instance-id

Then you can write one script that'll work for all instances. Even if you just have one at a time, you can include this script in a custom AMI so each new instance can give itself a new volume if needed.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.