Author : MD TAREQ HASSAN | Updated : 2021/10/29

Create VNet Without Subnet

First, Create Resource Group

//
// Create VNet
//
var virtualNetwork = new VirtualNetwork("DemoVirtualNetwork", new VirtualNetworkArgs
{
    ResourceGroupName = resourceGroup.Name,
    VirtualNetworkName = "DemoVnet",
    Location = "japaneast",
    AddressSpace = new AddressSpaceArgs
    {
        AddressPrefixes =
        {
            "10.10.0.0/16",
        },
    }
});

Create VNet With Subnet

First, Create Resource Group

//
// Create VNet
//
var virtualNetwork = new VirtualNetwork("DemoVirtualNetwork", new VirtualNetworkArgs
{
    ResourceGroupName = resourceGroup.Name,
    VirtualNetworkName = "DemoVnet",
    Location = "japaneast",
    AddressSpace = new AddressSpaceArgs
    {
        AddressPrefixes =
        {
            "10.10.0.0/16",
        },
    },
    Subnets =
    {
        new AzureNative.Network.Inputs.SubnetArgs
        {
           Name = "AKSApplicationSubnet",
           AddressPrefix = "10.10.0.0/18"
        },
        new AzureNative.Network.Inputs.SubnetArgs
        {
           Name = "AzureSQLManagedInstanceSubnet",
           AddressPrefix = "10.10.72.0/24"
        },
        new AzureNative.Network.Inputs.SubnetArgs
        {
           Name = "PrivateLinkSubnet",
           AddressPrefix = "10.10.73.0/24"
        },
        new Pulumi.AzureNative.Network.Inputs.SubnetArgs
        {
           Name = "UtilitySubnet",
           AddressPrefix = "10.10.80.0/21"
        }
    }
});

Create VNet With Values from Stack Settings File

Pulumi.dev.yaml

config:
  azure-native:location: japaneast

  xxx:SpokeVirtualNetworkArgs:
    VirtualNetworkName: demo-vnet
    AddressPrefixes:
    - 10.10.0.0/16
    Subnets:
    - AddressPrefix: 10.10.0.0/18
      SubnetName: AKSApplicationSubnet
    - AddressPrefix: 10.10.64.0/21
      SubnetName: AKSKafkaSubnet
    - AddressPrefix: 10.10.72.0/24
      SubnetName: AzureSQLManagedInstanceSubnet
    - AddressPrefix: 10.10.80.0/21
      SubnetName: UtilitySubnet
      
# ... ... ...

First, Create Resource Group

MyStack.cs

using System.Threading.Tasks;
using Pulumi;
using Pulumi.AzureNative.Resources;
using System.Text.Json;
using System.Collections.Generic;
using Pulumi.AzureNative.Network;
using Pulumi.AzureNative.Network.Inputs;
using System;
using System.Text;
using AzureNative = Pulumi.AzureNative;


namespace PulumiDemo
{
    class MyStack : Stack
    {

        public MyStack()
        {
            var config = new Pulumi.Config("xxx");
            
            // ... ... ...

            var spokeVNetArgs = config.RequireObject<JsonElement>("SpokeVirtualNetworkArgs");
            var spokeVNetName = spokeVNetArgs.GetProperty("VirtualNetworkName").GetString()!;
            var spokeVNetAddressPrefixes = spokeVNetArgs.GetProperty("AddressPrefixes").Deserialize<List<string>>()!;
            
            //
            // Create spoke VNet
            //
            var spokeVNet = new VirtualNetwork("SpokeVirtualNetwork", new VirtualNetworkArgs
            {
                ResourceGroupName = mainResourceGroup.Name,
                VirtualNetworkName = spokeVNetName,
                AddressSpace = new AddressSpaceArgs
                {
                    AddressPrefixes = spokeVNetAddressPrefixes
                }
            });

            //
            // Add subnets to spoke VNet
            //
            var spokeVNetSubnets = spokeVNetArgs.GetProperty("Subnets").Deserialize<List<Dictionary<string, string>>>()!;

            var _ = spokeVNet.Name.Apply(spokeVNetName =>
            {
                foreach (var subnet in spokeVNetSubnets)
                {
                    var subnetName = subnet![SubnetName];
                    var subnetAddressPrefix = subnet![AddressPrefix];

                    var _ = new Subnet($"{spokeVNetName}-{subnetName}", new AzureNative.Network.SubnetArgs
                    {
                        SubnetName = subnetName,
                        AddressPrefix = subnetAddressPrefix,
                        VirtualNetworkName = spokeVNet.Name,
                        ResourceGroupName = mainResourceGroup.Name
                    });
                }

                return spokeVNetName;
            });
            
            // ... ... ...
        }
    }
}