Search code examples
pulumi

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


How to correctly use 'apply' in order to build 'ip_configurations' list using subnet id known after virtual network created ? Below gives 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" }]

Solution

  • First of all, I recommend you using a separate Subnet resource, e.g

    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
    

    You can find an example here.

    If you define subnets inside VirtualNetwork, you can access ID by looking at the id property of the first item in subnets output (virtual_network.subnets[0].id).