Boto ec2创建磁盘需要很长时间才能创建 - Python

时间:2015-04-22 17:46:23

标签: python amazon-web-services amazon-ec2 boto

我正在撰写关于使用boto在EC2中创建,附加,分离和销毁EBS卷的概念证明:

import time
import ec2.boto

conn = boto.ec2.connect_to_region("us-west-2",
            aws_access_key_id=AWS_ACCESS_KEY,
            aws_secret_access_key=AWS_SECRET_KEY)

reservation = conn.run_instances(
                'ami-1fa78f2f',
                instance_type='m3.medium')

instance = reservation.instances[0]
instance.add_tag('Name', 'volume_test')


while instance.state != 'running':
        time.sleep(10)
        instance.update() # Updates Instance metadata
        print 'Instance state: ', instance.state

print "attempting to attach volume"
vol = conn.create_volume(10, instance.placement)
while vol.status != 'available':
        print 'Vol state: ', vol.status
        time.sleep(20)

vol.attach(instance.id, '/dev/sdh')

它的容量为10GBv,但它永远不会超出“创造”的范围。状态:

# python test.py 
Instance state:  pending
Instance state:  pending
Instance state:  running
attempting to attach volume
Vol state:  creating
Vol state:  creating
Vol state:  creating
Vol state:  creating
Vol state:  creating
Vol state:  creating
Vol state:  creating
Vol state:  creating
Vol state:  creating

我做错了什么,或者EC2创建音量太慢了?

1 个答案:

答案 0 :(得分:2)

您没有更新循环内的音量。试试这个。

print "attempting to attach volume"
vol = conn.create_volume(10, instance.placement)
while vol.status != 'available':
    print 'Vol state: ', vol.status
    time.sleep(20)
    vol.update()