«

»

Jun 24

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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);
         }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

hublot replica | replica watches | cartier replica sale | breitling replica sale