Define a virtual network
This skill is focused on learning how to deploy Windows and Linux virtual machines. A pre- requisite of deploying a virtual machine is having a virtual network. Listing 3-1 shows how to define the structure of the virtual network using several variables that describe the address space and subnet allocation.
LISTING 3-1 Variables for virtual network creation
“ExamRefRGPrefix”: “10.0.0.0/16”, “ExamRefRGSubnet1Name”: “FrontEndSubnet”, “ExamRefRGSubnet1Prefix”: “10.0.0.0/24”, “ExamRefRGSubnet2Name”: “BackEndSubnet”, “ExamRefRGSubnet2Prefix”: “10.0.1.0/24”,
“ExamRefRGSubnet1Ref”: “[concat(variables(‘vnetId’), ‘/subnets/’, variables(‘ExamRefRGSubnet1Name’))]”,
“VNetId”: “[resourceId(‘Microsoft.Network/virtualNetworks’, variables(‘VirtualNetworkN ame’))]”,
“VirtualNetworkName”: “ExamRefVNET”,
After the variables are defined, you can add the virtual network resource to the resource’s element in your template. Listing 3-2 shows a portion of an ARM template for a virtual network named ExamRefVNET, with an address space of 10.0.0.0/16 and two subnets: FrontEndSubnet 10.0.0.0/24 and BackEndSubnet 10.0.1.0/24. Note that this is only an example and would
not validate as a complete template, and that the syntax to read the value of variables— [variables(‘variablename’)]—is used heavily when authoring templates. The virtual net- work’s location is set based on the return value of the built-in resourceGroup() function, which returns information about the resource group the resource is being created or updated in.
LISTING 3-2 Template structure for creating a virtual network
{
“name”: “[variables(‘VirtualNetworkName’)]”, “type”: “Microsoft.Network/virtualNetworks”, “location”: “[resourceGroup().location]”, “apiVersion”: “2023-06-01”,
“dependsOn”: [], “properties”: {
“addressSpace”: { “addressPrefixes”: [
“[variables(‘ExamRefRGPrefix’)]”
]
},
“subnets”: [
{
“name”: “[variables(‘ExamRefRGSubnet1Name’)]”, “properties”: {
“addressPrefix”: “[variables(‘ExamRefRGSubnet1Prefix’)]”
}
},
{
“name”: “[variables(‘ExamRefRGSubnet2Name’)]”, “properties”: {
“addressPrefix”: “[variables(‘ExamRefRGSubnet2Prefix’)]”
}
]
}
}
Define a network interface
Every virtual machine has one or more network interfaces. To create one with a template, add a variable to the variables section to store the network interface resource name as the following snippet demonstrates:
“VMNicName”: “VMNic”
Listing 3-3 defines a network interface named WindowsVMNic. This resource has a depen- dency on the ExamRefVNET virtual network. This dependency will ensure that the virtual network is created prior to the network interface creation when the template is deployed and is a critical feature of orchestration of resources in the correct order. The network interface is associated to the subnet by referencing the ExamRefRGSubnet1Ref variable.
LISTING 3-3 Creating a network interface
{
“name”: “[variables(‘VMNicName’)]”,
“type”: “Microsoft.Network/networkInterfaces”, “location”: “[resourceGroup().location]”, “apiVersion”: “2023-06-01”,
“dependsOn”: [
“[resourceId(‘Microsoft.Network/virtualNetworks’, ‘ExamRefVNET’)]”
],
“properties”: { “ipConfigurations”: [
{
“name”: “ipconfig1”, “properties”: {
“privateIPAllocationMethod”: “Dynamic”, “subnet”: {
“id”: “[variables(‘ExamRefRGSubnet1Ref’)]”
}
}
}
]
}
}
EXAM TIP
To specify a static private IP address in template syntax, specify an address from the assigned subnet using the privateIpAddress property and set the privateIpAllocation method to Static.
“privateIpAddress”: “10.0.0.10”, “privateIpAllocationMethod”: “Static,