Connecting Azure DevTest Lab environments to the Lab Virtual Network


Azure DevTest Labs makes creating VMs easier with the built-in networking for the lab, and has a great deal of flexibility with the ability to create multi-VM and/or PaaS environments.  We have added a new feature that will allow lab users to create environments that are connected to a lab subnet.  This is accomplished by using the string $(LabSubnetId) in place of the Subnet resource Id in the environment template.  This allows the environment templates to have fewer resources and focus the network management to the lab.  Having this capability allows for other PaaS resources to be connected to the lab subnet.  One scenario is testing an N-tier app with a SQL Server data tier that is connected to the lab VNet allowing test VMs within the lab to access it.  This post will walk through how to use this token to connect a VMs in an environment to the lab virtual network.  As there is an excellent post on how to create multi-VM environments and PaaS resources with Azure Resource Manager templates, this post will focus on the use of the token within an ARM template.

How and where can I use the token $(LabSubnetId)

The string token can be used any place where the actual Azure Resource Id for the subnet would be used.

  • In the example below it has been used as the default value for a parameter which allows the end user to leave it as is.
  • If users have a different subnet then they can enter that value instead.
  • If user input isn't needed, then the token can be set as the value of a variable that will look like "DTLSubnetVariable": "$(LabSubnetId)" . This variable can be used wherever necessary in the resources section.
  • The token can be used directly in the resources section, however this isn't recommended as the practice is prone to being difficult to debug.
  • If externally linked templates are being used the token replacement will only occur in the main template. The Lab Subnet resource information can be passed to the externally linked file via inline parameters which is demonstrated in the post on linked and nested templates.

Simple VM Environment Template

Below is a simple one VM DTL Environment template that will connect the VM Nic to the lab's subnet.  The following json is the data for the azuredeploy.json that will be stored in the lab private repo.  The token is the default value for a parameter so the end user can change it with an actual Azure subnet ResourceId if available.

Sample JSON File

{

"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",

"parameters": {

"DTLEnvironVmStoretype": {

"type": "string",

"defaultValue": "Standard_LRS",

"allowedValues": [

"Standard_LRS",

"Standard_ZRS",

"Standard_GRS",

"Standard_RAGRS",

"Premium_LRS"

]

},

"DTLEnvironVmName": {

"type": "string",

"minLength": 1

},

"VmAdminUserName": {

"type": "string",

"minLength": 1

},

"VmAdminUserPassword": {

"type": "securestring"

},

"DTLEnvironVmOsVersion": {

"type": "string",

"defaultValue": "2012-R2-Datacenter",

"allowedValues": [

"2008-R2-SP1",

"2012-Datacenter",

"2012-R2-Datacenter",

"Windows-Server-Technical-Preview"

]

},

"DTLSubnetId": {

"type": "string",

"defaultValue": "$(LabSubnetId)"

}},

"variables": {

"DTLEnvironStoreName": "[toLower([concat(parameters('DTLEnvironVmName'), 'storename')])]",

"DTLEnvironVmImagePublisher": "MicrosoftWindowsServer",

"DTLEnvironVmImageOffer": "WindowsServer",

"DTLEnvironVmOSDiskName": "[concat(parameters('DTLEnvironVmName'), 'OSDisk')]",

"DTLEnvironVmSize": "Standard_D2_v2",

"DTLEnvironVmStorageAccountContainerName": "vhds",

"DTLEnvironVmNicName": "[concat(parameters('DTLEnvironVmName'), 'NetworkInterface')]"

},

"resources": [

{

"name": "[variables('DTLEnvironStoreName')]",

"type": "Microsoft.Storage/storageAccounts",

"location": "[resourceGroup().location]",

"apiVersion": "2016-01-01",

"sku": {

"name": "[parameters('DTLEnvironVmStoretype')]"

},

"dependsOn": [ ],

"tags": {

"displayName": "[variables('DTLEnvironStoreName')]"

},

"kind": "Storage"

},

{

"name": "[variables('DTLEnvironVmNicName')]",

"type": "Microsoft.Network/networkInterfaces",

"location": "southeastasia",

"apiVersion": "2016-03-30",

"dependsOn": [ ],

"tags": {

"displayName": "[variables('DTLEnvironVmNicName')]"

},

"properties": {

"ipConfigurations": [

{

"name": "ipconfig1",

"properties": {

"privateIPAllocationMethod": "Dynamic",

"subnet": {

"id": "[parameters('DTLSubnetId')]"

}
}
}
]
}
},

{

"name": "[parameters('DTLEnvironVmName')]",

"type": "Microsoft.Compute/virtualMachines",

"location": "[resourceGroup().location]",

"apiVersion": "2015-06-15",

"dependsOn": [

"[resourceId('Microsoft.Storage/storageAccounts', variables('DTLEnvironStoreName'))]",

"[resourceId('Microsoft.Network/networkInterfaces', variables('DTLEnvironVmNicName'))]"

],

"tags": {

"displayName": "[parameters('DTLEnvironVmName')]"

},

"properties": {

"hardwareProfile": {

"vmSize": "[variables('DTLEnvironVmSize')]"

},

"osProfile": {

"computerName": "[parameters('DTLEnvironVmName')]",

"adminUsername": "[parameters('VmAdminUserName')]",

"adminPassword": "[parameters('VmAdminUserPassword')]"

},

"storageProfile": {

"imageReference": {

"publisher": "[variables('DTLEnvironVmImagePublisher')]",

"offer": "[variables('DTLEnvironVmImageOffer')]",

"sku": "[parameters('DTLEnvironVmOsVersion')]",

"version": "latest"

},

"osDisk": {

"name": "[variables('DTLEnvironVmOSDiskName')]",

"vhd": {

"uri": "[concat(reference(resourceId('Microsoft.Storage/storageAccounts', variables('DTLEnvironStoreName')), '2016-01-01').primaryEndpoints.blob, variables('DTLEnvironVmStorageAccountContainerName'), '/', variables('DTLEnvironVmOSDiskName'), '.vhd')]"

},

"caching": "ReadWrite",

"createOption": "FromImage"

}

},

"networkProfile": {

"networkInterfaces": [

{

"id": "[resourceId('Microsoft.Network/networkInterfaces', variables('DTLEnvironVmNicName'))]"

}
]
}
}
}],

"outputs": {}

}

The DevTest Lab Subnet token is another capability that allows DevTest Labs to be better, more customizable, and help you the customer more.

We hope you find this new feature useful!

Got an idea to make it work better for you? Submit your feedback/ideas or vote for others at Azure DevTest Labs UserVoice forum.

Have a question? Check out the answers or ask a new one at MSDN forum.

Roger Best, Senior Software Engineer

Roger is part of the Visual Studio and .NET engineering team focused on Visual Studio and Azure customers.  He has been at Microsoft for 20 years, focusing on developer technologies for the past decade or so.  In his spare time, he watches too many movies, and tries to survive triathlons


Comments (0)

Skip to main content