2

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?

I Z
  • 5,719
  • 19
  • 53
  • 100
  • Never done any python. But it seems like you are running `volume status check` **ONLY** once and then checking it forever with `if` loop. You should run `volume status check` every time to get latest update before you run the `if` loop. Else the `if` loop will keep comparing against the stale value which does not exists. – slayedbylucifer Dec 31 '13 at 05:22
  • Yes, somewhere in your loop you should be calling ``vol.update()`` to get the latest status about your volume. – garnaat Jan 02 '14 at 15:54
  • Garnaat, thanks, yes I indeed needed an update... – I Z Jan 02 '14 at 19:07
  • possible duplicate of [How to launch EC2 instance with Boto, specifying size of EBS?](http://stackoverflow.com/questions/13585857/how-to-launch-ec2-instance-with-boto-specifying-size-of-ebs) – trss Apr 29 '15 at 06:10

2 Answers2

2

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)
I Z
  • 5,719
  • 19
  • 53
  • 100
0

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:

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!

Community
  • 1
  • 1
trss
  • 915
  • 1
  • 19
  • 35