«

»

Jun 24

Using Pulumi to Create a Azure Linux Virtual Machine

This code relies on the constants you can find in other posts. This will create a storage account used for boot diagnostics. Then it create a network interface, then a VM using an Ubuntu image.

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
class ExampleVm
{
    private readonly string _location;
    private readonly ResourceGroup _resourceGroup;
    private readonly string _vmSize = "Standard_DS3_v2";
    private readonly string _ipAddress = "10.20.1.10";
    private readonly string _adminUsername = "adminAccount";
    private readonly string _adminPassword = "SuperSecretP@ssw3rd";
    private string _computerName = ""; //replaced on init
 
    public ExampleVm(string location, ResourceGroup resourceGroup)
    {
        _location = location;
        _resourceGroup = resourceGroup;
    }
 
    //Example that using attribute will output designated properties
    [Output] public Output<string> PrimaryBlobEndpointConnectionString { get; set; }
 
    public Account BuildBootDiagnosticStorageAccount(string storageAccountName)
    {
        // Create an Azure Storage Account
        var storageAccount = new Account(storageAccountName, new AccountArgs
        {
            Name = storageAccountName,
            Location = _location,
            ResourceGroupName = _resourceGroup.Name,
            AccountReplicationType = Constants.LRS,
            AccountTier = Constants.SKUSTANDARD,
            AccountKind = Constants.STORAGEACCOUNT_DEFAULT
        });
 
        PrimaryBlobEndpointConnectionString = storageAccount.PrimaryBlobEndpoint;
        return storageAccount;
    }
 
    public void BuildVM(string vmName, Account bootDiagStorageAccount, Dictionary<string, Subnet> subnets)
    {
        _computerName = vmName;
 
        var nic = new NetworkInterface(vmName + "-nic", new NetworkInterfaceArgs()
        {
            Location = _location,
            ResourceGroupName = _resourceGroup.Name,
            EnableAcceleratedNetworking = true,
            IpConfigurations = new NetworkInterfaceIpConfigurationArgs
            {
                Name = "Ipconfig1",
                Primary = true,
                SubnetId = subnets["Web"].Id,
                PrivateIpAddressVersion = Constants.IPVERSION4,
                PrivateIpAddress = _ipAddress,
                PrivateIpAddressAllocation = Constants.STATIC
            }
        });
 
        var vm = new VirtualMachine(vmName, new VirtualMachineArgs()
        {
            Location = _location,
            ResourceGroupName = _resourceGroup.Name,
            Name = vmName,
            VmSize = _vmSize,
            Zones = "1",
            BootDiagnostics = new VirtualMachineBootDiagnosticsArgs()
            {
                Enabled = true,
                StorageUri = bootDiagStorageAccount.PrimaryBlobEndpoint
            },
            NetworkInterfaceIds = nic.Id,
            OsProfile = new VirtualMachineOsProfileArgs
            {
                AdminUsername = _adminUsername,
                AdminPassword = _adminPassword,
                ComputerName = _computerName
            },
            OsProfileLinuxConfig = new VirtualMachineOsProfileLinuxConfigArgs
            {
                DisablePasswordAuthentication = false
            },
            StorageImageReference = new VirtualMachineStorageImageReferenceArgs
            {
                Sku = "18.04-LTS",
                Offer = "UbuntuServer",
                Publisher = "Canonical",
                Version = "Latest"
            },
            StorageOsDisk = new VirtualMachineStorageOsDiskArgs
            {
                Name = vmName + "-OSDisk",
                Caching = "ReadWrite",
                CreateOption = "FromImage",
                DiskSizeGb = 30,
                OsType = "Linux",
                ManagedDiskType = Constants.PREMIUM_LRS
            }
        });
    }
}