如何在创建'VirtualNetwork'对象后知道的pulumi中异步使用子网ID?

How to use asynchronously subnet id in pulumi which is know after 'VirtualNetwork' object created?

如何正确使用 'apply' 以便使用创建虚拟网络后已知的子网 ID 构建 'ip_configurations' 列表?下面给出 AttributeError: 'VirtualNetwork' object has no attribute 'subnet_id'

virtual_network = azure.network.VirtualNetwork(vnet_name, name=vnet_name, resource_group_name=resource_group.name,
    address_spaces=test_vnet['addr'], subnets=test_vnet['subnets'], location=resource_group.location)
ip_configurations = [{ "name": "ip-cfg", "subnet_id": virtual_network.subnet_id.apply(lambda subnet_id: virtual_network.subnets[0].id), 
    "privateIpAddressAllocation": "Dynamic" }]

首先,我建议您使用单独的 Subnet 资源,例如

vnet = network.VirtualNetwork(
    vnet_name,
    resource_group_name=resource_group.name,
    address_spaces=["10.0.0.0/16"])

subnet = network.Subnet(
    "subnet",
    resource_group_name=resource_group.name,
    address_prefix="10.0.2.0/24",
    virtual_network_name=vnet.name,
    enforce_private_link_endpoint_network_policies="false")

# subnet.id

你可以找到一个例子here

如果您在 VirtualNetwork 中定义 subnets,您可以通过查看 subnets 输出中第一项的 id 属性 来访问 ID( virtual_network.subnets[0].id).