AWS boto:创建后如何刷新subnet.state?它永远陷入'待定'状态,并且没有更新()

时间:2014-03-08 00:30:06

标签: python amazon-web-services boto

我是一名VPC。在该VPC中,我创建了一个子网。我希望尽可能小心,在子网真正准备好之前不要再继续了。但是,如果我做了subnet.state,它总是说'等待',即使它已经活了一段时间。

>>> subnet = {}
>>> subnet['public'] = conn.create_subnet(vpcid, '10.2.0.0/24')
>>> subnet['public'].state
u'pending'

我尝试做了subnet.update()但是没有用。

>>> subnet['public'].update()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Subnet' object has no attribute 'update'

更新子网对象状态的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

我刚刚在几分钟前遇到过这个问题。我希望有一个类似于VPC对象的子网对象的update()方法。这是我的解决方案:

#generic subnet creation method
def create_subnet(connection, vpc, cidr, name, test_mode=True):
    print("Creating subnet with CIDR block", cidr)
    subnet = connection.create_subnet(vpc.id, cidr_block=cidr, dry_run=test_mode)

    #wait for subnet to become live
    while subnet.state == 'pending':
        subnets = connection.get_all_subnets()
        for item in subnets:
            if item.id == subnet.id:
                subnet.state = item.state
        time.sleep(5)

    #tag the subnet
    subnet.add_tag("Name", name)
    print("Done")
    return subnet