Sean Whitesell

Cloud Architect, Microsoft MVP, ASP Insider, and User Group Leader

Using Pulumi to create Azure Virtual Network

This code segment is to create a virtual network in Azure. It's assumed you already have Pulumi installed and connected to Azure.

To start, you do need to know what Resource Group you want to house the virtual network (vnet). And, you need to know what address space you want to use and any subnets. You need at least one subnet for resources that need an IP address.

We'll start with a blank MyStack class. I create other classes for the resources so I don't have every network piece, VMs, storage accounts, etc all in the case class. I like the segregation.

class MyStack : Stack

{

    private readonly ResourceGroup _resourceGroup;

    private readonly Dictionary<string, Subnet> _subnets;


    private const string ResourceGroupName = ""seanrsgsetuptest"";

    private const string Location = ""southcentralus"";

    private const string VnetAddressSpace = ""10.20.0.0/17"";


    public MyStack()

    {

        _resourceGroup = new ResourceGroup(ResourceGroupName,

                new ResourceGroupArgs

                {

                    Name = ResourceGroupName,

                    Location = Location

                });

  

       var vnet = new VirtualNetworkBuilder(Location, _resourceGroup, VnetAddressSpace);

       vnet.BuildVnetAndSubnets();

       _subnets = vnet.Subnets;

    }

}

I have a class VirtualNetworkBuilder because there can easily be a lot of code for making the vnet and subnets. I have the Subnets dictionary available after creation because there are other resources that need a subnet by name and/or the ID.

internal class VirtualNetworkBuilder

{

        private readonly string _location;

        private readonly ResourceGroup _resourceGroup;

        private readonly string _vnetAddressSpace;

        private readonly string _dmzSubnetAddress;

        private readonly string _webSubnetAddress;

        private readonly string _dataSubnetAddress;

        private readonly string _gatewaySubnetAddress;


        public VirtualNetwork Vnet { get; private set; }

        public Dictionary<string, Subnet> Subnets { get; private set; }


        public VirtualNetworkBuilder(string location, ResourceGroup resourceGroup, string vnetAddressSpace)

        {

            _location = location;

            _resourceGroup = resourceGroup;

            _vnetAddressSpace = vnetAddressSpace;

            Subnets = new Dictionary<string, Subnet>();


            Vnet = new VirtualNetwork(""vnet"", new VirtualNetworkArgs()

            {

                ResourceGroupName = resourceGroup.Name,

                AddressSpaces = new[] { vnetAddressSpace }

            });


            var octets = vnetAddressSpace.Split('.');

            const string subnetSize = ""24"";

            var firstTwoOctets = octets[0] + ""."" + octets[1];

            _dmzSubnetAddress = firstTwoOctets + "".0.0/"" + subnetSize;

            _webSubnetAddress = firstTwoOctets + "".1.0/"" + subnetSize;

            _dataSubnetAddress = firstTwoOctets + "".2.0/"" + subnetSize;

            _gatewaySubnetAddress = firstTwoOctets + "".3.0/"" + subnetSize;

        }


        public void BuildVnetAndSubnets()

        {

            var dmzSubnet = new Subnet(""DMZ"", new SubnetArgs()

            {

                ResourceGroupName = _resourceGroup.Name,

                VirtualNetworkName = Vnet.Name,

                AddressPrefixes = _dmzSubnetAddress

            });

            Subnets.Add(""DMZ"", dmzSubnet);


            var webSubnet = new Subnet(""Web"", new SubnetArgs()

            {

                ResourceGroupName = _resourceGroup.Name,

                VirtualNetworkName = Vnet.Name,

                AddressPrefixes = _webSubnetAddress

            });

            Subnets.Add(""Web"", webSubnet);


            var dataSubnet = new Subnet(""Data"", new SubnetArgs()

            {

                ResourceGroupName = _resourceGroup.Name,

                VirtualNetworkName = Vnet.Name,

                AddressPrefixes = _dataSubnetAddress

            });

            Subnets.Add(""Data"", dataSubnet);


            var gatewaySubnet = new Subnet(""GatewaySubnet"", new SubnetArgs()

            {

                ResourceGroupName = _resourceGroup.Name,

                VirtualNetworkName = Vnet.Name,

                Name = ""GatewaySubnet"",

                AddressPrefixes = _gatewaySubnetAddress

            });

            Subnets.Add(""GatewaySubnet"", gatewaySubnet);

         }

}