AWS boto:如何在创建子网后刷新subnet.state?它一直处于“待处理”状态且没有update()

0 投票
1 回答
526 浏览
提问于 2025-04-17 21:22

我有一个虚拟私有云(VPC)。在这个VPC里,我创建了一个子网。我想尽量小心,不想在子网真正准备好之前就继续操作。但是如果我查看subnet.state,它总是显示'pending',即使它已经活跃了一段时间。

>>> 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 个回答

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

撰写回答