Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 5 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
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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