Reputation: 5927
In the past, I have created an instance with attached EBS storage through the AWS web console. At the "Step4. Add storage" step I would add EBS storage as device="/dev/sdf"
, Standard
as Volume type
and no Snapshot
. Once the instance got launched, I would issue the following set of commands to mount the extra drive as a separate directory and make it accessible to everybody:
sudo mkfs.ext4 /dev/xvdf
sudo mkdir /home/foo/extra_storage_directory
sudo mount -t ext4 /dev/xvdf /home/foo/extra_storage_directory
cd /home/foo
sudo chmod a+w extra_storage_directory
aI was given a piece of python code that creates instances without any extra storage programmatically. It calls boto.ec2.connection.run_instances
. I need to modify this code to be able to create instances with extra storage. I need to essentially emulate the manual steps I used doing it via console, to make sure that the above sudo
commands work after I launch the new instance.
Which boto
function(s) do I need to use and how to add the storage?
UPDATE: I did some digging and wrote some code that I thought was supposed to do what I wanted. However, the behavior is a bit strange. Here's what I have:
res = state.connection.run_instances(state.ami,key_name=state.key,instance_type=instance_type,security_groups=sg)
inst = res.instances[0]
pmt = inst.placement
time.sleep(60)
try:
vol = state.connection.create_volume(GB, pmt)
tsleep = 60
time.sleep(tsleep)
while True:
vstate = vol.status
if not vstate == 'available':
print "volume state is %s, trying again after %d secs" % (vstate,tsleep)
time.sleep(tsleep)
else:
break
print "Attaching vol %s to inst %s" % (str(vol.id),str(inst.id))
state.connection.attach_volume(vol.id, inst.id, "/dev/sdf")
print "attach_volume OK"
except Exception as e:
print "Exception: %s" % str(e)
The call to run_instances
came from the original code that I need to modify. After the volume get created, when I looked at its status in the AWS console, I see available
. However, I get an endless sequence of
volume state is creating, trying again after 60 secs
Why the difference?
Upvotes: 2
Views: 1920
Reputation: 943
I tripped on the same problem and the answer at How to launch EC2 instance with Boto, specifying size of EBS? had the solution.
Here are the relevant links:
block_device_map
BlockDeviceMapping.N
-b, --block-device-mapping mapping
--block-device-mappings (list)
Important note: While in the Web Console the "Delete on Termination" check box is checked, in the Boto API, it's the opposite, delete_on_termination=False
by default!
Upvotes: 0
Reputation: 5927
As garnaat pointed out, I did have to use vol.update()
to update the volume status. So the code below does what I need:
res = state.connection.run_instances(state.ami,key_name=state.key,instance_type=instance_type,security_groups=sg)
inst = res.instances[0]
pmt = inst.placement
time.sleep(60)
try:
vol = state.connection.create_volume(GB, pmt)
tsleep = 60
time.sleep(tsleep)
while True:
vol.update()
vstate = vol.status
if not vstate == 'available':
print "volume state is %s, trying again after %d secs" % (vstate,tsleep)
time.sleep(tsleep)
else:
break
print "Attaching vol %s to inst %s" % (str(vol.id),str(inst.id))
state.connection.attach_volume(vol.id, inst.id, "/dev/sdf")
print "attach_volume OK"
except Exception as e:
print "Exception: %s" % str(e)
Upvotes: 2