Add a Public IP address
To add a public IP address to the virtual machine, you must make several modifications. The first is to define a parameter that the user will use to specify a unique DNS name for the public IP. The following code goes in the parameters block of a template:
“VMPublicIPDnsName”: { “type”: “string”, “minLength”: 1
}
The second modification is to add the public IP resource itself. Before adding the resource,add a new variable in the variables section to store the name of the public IP resource.
“VMPublicIPName”: “VMPublicIP”
Listing 3-4 shows a public IP address resource with the public IP allocation method set to Dynamic (it can also be set to Static). The domainNameLabel property of the IP address dnsSet- tings element is populated by the parameter. This makes it easy to specify a unique value for the address at deployment time.
LISTING 3-4 Creating a network interface
{
“name”: “[variables(‘VMPublicIPName’)]”, “type”: “Microsoft.Network/publicIPAddresses”, “location”: “[resourceGroup().location]”, “apiVersion”: “2023-06-01”,
“dependsOn”: [ ], “properties”: {
“publicIPAllocationMethod”: “Dynamic”, “dnsSettings”: {
“domainNameLabel”: “[parameters(‘VMPublicIPDnsName’)]”
}
}
}
The next modification is to update the network interface resource that the public IP address is associated with. The network interface must now have a dependency on the public IP address to ensure it is created before the network interface. The following example shows the addition to the dependsOn array:
“dependsOn”: [
“[resourceId(‘Microsoft.Network/virtualNetworks’, ‘ExamRefVNET’)]”, “[resourceId(‘Microsoft.Network/publicIPAddresses’,
variables(‘VMPublicIPName’))]”
],
The ipConfigurations -> properties element must also be modified to reference the publicIPAddress resource. See Listing 3-5.
LISTING 3-5 IP configurations
“ipConfigurations”: [
{
“name”: “ipconfig1”, “properties”: {
“privateIPAllocationMethod”: “Dynamic”, “subnet”: {
“id”: “[variables(‘ExamRefRGSubnet1Name’)]”
},
“publicIPAddress”: {
“id”: “[resourceId(‘Microsoft.Network/publicIPAddresses’, variables(‘VMPublicIPName’))]”
},
}
}
]
Define a virtual machine resource
Before creating the virtual machine resource, you will add several parameters and variables to define. Each virtual machine requires administrative credentials. To enable a user to specify the credentials at deployment time, add two additional parameters for the administrator account and the password.
“VMAdminUserName”: { “type”: “string”, “minLength”: 1
},
“VMAdminPassword”: { “type”: “string”, “minLength”: 1
}
Several variables are needed to define the configuration of the virtual machine resource. The following variables define the VM name, operating system image, and the VM size. These should be inserted into the variables section of the template.
“VMName”: “MyVM”,
“VMImagePublisher”: “MicrosoftWindowsServer”, “VMImageOffer”: “WindowsServer”, “VMOSVersion”: “WS2019-Datacenter”, “VMOSDiskName”: “VM2OSDisk”,
“VMSize”: “Standard_D2_v2”, “VM2ImagePublisher”: “MicrosoftWindowsServer”, “VM2ImageOffer”: “WindowsServer”, “VM2OSDiskName”: “VM2OSDisk”,
“VMSize”: “Standard_D2_v2”
The VM has a dependency on the network interface. It doesn’t have to have a dependency on the virtual network because the network interface itself does. This VM is using managed disks, so there are no references to storage accounts for the VHD file. Listing 3-6 shows a sample virtual machine resource.
LISTING 3-6 Virtual machine resource
{
“name”: “[parameters(‘VMName’)]”,
“type”: “Microsoft.Compute/virtualMachines”, “location”: “[resourceGroup().location]”,
“apiVersion”: “2023-06-01”, “dependsOn”: [
“[resourceId(‘Microsoft.Network/networkInterfaces’, variables(‘VMNicName’))]”
],
“properties”: { “hardwareProfile”: {
“vmSize”: “[variables(‘vmSize’)]”
},
“osProfile”: {
“computerName”: “[variables(‘VMName’)]”, “adminUsername”: “[parameters(‘VMAdminUsername’)]”, “adminPassword”: “[parameters(‘VMAdminPassword’)]”
},
“storageProfile”: { “imageReference”: {
“publisher”: “[variables(‘VMImagePublisher’)]”, “offer”: “[variables(‘VMImageOffer’)]”,
“sku”: “[variables(‘VMOSVersion’)]”, “version”: “latest”
},
“osDisk”: {
“createOption”: “FromImage”
}
},
“networkProfile”: { “networkInterfaces”: [
{
“id”: “[resourceId(‘Microsoft.Network/networkInterfaces’, variables(‘VMNicName’))]”
}
]
}
}
}
There are several properties of a virtual machine resource that are critical to its configuration:
- hardwareProfile This element is where you set the size of the virtual machine. Set the vmSize property to the desired size, such as Standard_D2_v2.
- osProfile This element at a basic level is where you set the computerName and adminUsername properties. The adminPassword property is required if you do not specify an SSH key. This element also supports other properties, including windowsConfigura- tion, linuxConfiguration, and secrets.
- osProfile, windowsConfiguration While the example doesn’t use this configuration,this element provides the ability to set advanced properties on Windows VMs:
- provisionVMAgent This is enabled by default, but you can disable it. Specify whether extensions can be added.
- enableAutomaticUpdates Specify whether Windows updates are enabled.
- timeZone Specify the time zone for the virtual machine.
- additionalUnattendContent Pass unattended install configuration for additional configuration options.
- winRM Configure Windows PowerShell remoting.
- provisionVMAgent Enabled by default, but you can disable. Specify whether extensions can be added.
- disablePasswordAuthentication If set to true, you must specify an SSH key.
- Ssh, publicKeys Specify the public key to use for authentication with the VM.
- osProfile, secrets This element secrets is used for deploying certificates that are in Azure Key Vault.
- storageProfile This element is where the operating system image is specified, and the operating system and data disk configuration are set.
- networkProfile This element is where the network interfaces for the virtual machine are specified.