过去,我通过AWS Web控制台创建了一个附加了EBS存储的实例。在“Step4。添加存储”步骤中,我将EBS存储添加为device="/dev/sdf"
,将Standard
添加为Volume type
,不添加Snapshot
。实例启动后,我会发出以下命令集,将额外的驱动器作为一个单独的目录挂载,并让所有人都可以访问:
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
给了我一段python代码,它以编程方式创建没有任何额外存储的实例。它调用boto.ec2.connection.run_instances
。我需要修改此代码才能创建具有额外存储空间的实例。我需要基本上模拟我通过控制台使用的手动步骤,以确保在启动新实例后上述sudo
命令有效。
我需要使用哪些boto
功能以及如何添加存储空间?
更新:我做了一些挖掘并编写了一些我认为应该做我想要的代码。但是,这种行为有点奇怪。这就是我所拥有的:
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)
对run_instances
的调用来自我需要修改的原始代码。创建卷后,当我在AWS控制台中查看其状态时,我看到available
。但是,我得到了无穷无尽的序列
volume state is creating, trying again after 60 secs
为什么会有差异?
答案 0 :(得分:2)
正如garnaat指出的那样,我确实必须使用vol.update()
来更新音量状态。所以下面的代码可以满足我的需求:
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)
答案 1 :(得分:0)
我绊倒了同样的问题,How to launch EC2 instance with Boto, specifying size of EBS?的答案有了解决方案。
以下是相关链接:
block_device_map
BlockDeviceMapping.N
-b, --block-device-mapping mapping
--block-device-mappings (list)
重要提示:在Web控制台中,"删除终止"选中复选框,在Boto API中,相反,默认为delete_on_termination=False
!