«

Jun 26

Using Pulumi to Create an Azure Load Balancer

This code will create an Azure load balancer with a frontend IP configuration, health probe, and a backend pool. There is also a method for associating a VM’s network interface (NIC) to a backend pool. Since VM can have NICs, and each NIC can have multiple IP configurations.

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
public class Constants
{
        public const string SKUSTANDARD = "Standard";
        public const string SKUBASIC = "Basic";
        public const string IPVERSION4 = "IPv4";
        public const string STATIC = "Static";
        public const string DYNAMIC = "Dynamic";
 
        public const string ALL = "*";
        public const string TCP = "TCP";
        public const string UDP = "UDP";
        public const string ALLOW = "Allow";
 
        public const string VIRTUALAPPLIANCE = "VirtualAppliance";
        public const string VIRTUALNETWORKGATEWAY = "VirtualNetworkGateway";
 
        public const string PREMIUM_LRS = "Premium_LRS";
        public const string LRS = "LRS";
 
        public const string STORAGEACCOUNT_BLOB = "BlobStorage";
        public const string STORAGEACCOUNT_BLOCKBLOB = "BlockBlobStorage";
        public const string STORAGEACCOUNT_FILE = "FileStorage";
        public const string STORAGEACCOUNT_STORAGE = "Storage";
        public const string STORAGEACCOUNT_STORAGEV2 = "StorageV2";
        public const string STORAGEACCOUNT_DEFAULT = "StorageV2";
 
}
 
class LoadBalancerBuilder
{
        private readonly string _namePrefix;
        private readonly string _location;
        private readonly ResourceGroup _resourceGroup;
        private readonly Dictionary<string, Subnet> _subnets;
 
        public LoadBalancerBuilder(string namePrefix, string location, ResourceGroup resourceGroup, Dictionary<string, Subnet> subnets)
        {
            _namePrefix = namePrefix;
            _location = location;
            _resourceGroup = resourceGroup;
            _subnets = subnets;
        }
 
        public LoadBalancer BuildWebAppsLoadBalancer()
        {
            var frontEndConfig = new LoadBalancerFrontendIpConfigurationArgs()
            {
                Name = "LoadBalancerFrontEnd",
                PrivateIpAddress = "11.0.1.10",
                PrivateIpAddressVersion = Constants.IPVERSION4,
                PrivateIpAddressAllocation = Constants.STATIC,
                SubnetId = _subnets["DMZ"].Id,
 
            };
 
            var lb = new LoadBalancer(_namePrefix + "-lb-webapps", new LoadBalancerArgs()
            {
                Location = _location,
                ResourceGroupName = _resourceGroup.Name,
                Sku = Constants.SKUSTANDARD,
                FrontendIpConfigurations = frontEndConfig
            });
 
            var healthProbe = new Probe("Probe1", new ProbeArgs
            {
                Name = "Probe1",
                LoadbalancerId = lb.Id,
                ResourceGroupName = _resourceGroup.Name,
                IntervalInSeconds = 5,
                NumberOfProbes = 2,
                Port = 22,
                Protocol = Constants.TCP
            });
 
            var backendPool = new BackendAddressPool("Pool1", new BackendAddressPoolArgs
            {
                ResourceGroupName = _resourceGroup.Name,
                LoadbalancerId = lb.Id,
                Name = "Pool1"
            });
 
            return lb;
        }
 
        public void AttachVmToLoadBalancerBackendPool(BackendAddressPool backendAddressPool, LoadBalancer lb, NetworkInterface nic, string ipConfigName)
        {
            var rnd = new Random();
            var rndNumber = rnd.Next(100, 999);
 
            new NetworkInterfaceBackendAddressPoolAssociation("assoc"+ rndNumber,
                new NetworkInterfaceBackendAddressPoolAssociationArgs
                {
                    BackendAddressPoolId = backendAddressPool.Id,
                    NetworkInterfaceId = nic.Id,
                    IpConfigurationName = ipConfigName
                });
        }
}

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