1

I have a resource group that contains separately created DevTest lab and Virtual Network (VNet) using Bicep.

I can manually by using the Azure portal attach the VNet to the Devtest lab by:

  1. Go to Devtest lab
  2. Select "Configuration and policies"
  3. From the External resources menu, select "Virtual networks"
  4. Click "Add"
  5. Select the VNet

Is it possible to automate this process using Azure CLI? or any other alternative?

As I am adopting Azure DevOps pipelines to run (Bicep code) and adjust (Azure CLI) the resources automatically.

I am using ARM/Bicep template (Microsoft.Network/virtualNetworks@2021-02-01) for VNET, and (Microsoft.DevTestLab/labs@2018-09-15) for DevTest lab

A7med
  • 23
  • 6

1 Answers1

1

If you are creating Vnet and Devtest lab separately then you can use the below Azure Quickstart Template from Github for creating a Devtestlab with existing vnet :

JSON ARM:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
      "newLabName": {
        "type": "string",
        "metadata": {
          "description": "The name of the new lab instance to be created."
        }
      },
      "newLabVirtualNetworkName": {
        "type": "string",
        "metadata": {
          "description": "The name of the new lab virtual network instance to be created with the new lab instance being created."
        }
      },
      "existingVirtualNetworkId": {
        "type": "string",
        "metadata": {
          "description": "The resource ID pointing to an existing (compute) virtual network to be referenced by the new lab virtual network instance being created."
        }
      },
      "existingSubnetName": {
        "type": "string",
        "defaultValue": "default",
        "metadata": {
          "description": "The name of an existing (compute) subnet instance to be configured for Lab VM creation."
        }
      }
    },
    "variables": {
      "existingSubnetId": "[concat(parameters('existingVirtualNetworkId'), '/subnets/', parameters('existingSubnetName'))]"
    },
    "resources": [
      {
        "apiVersion": "2018-10-15-preview",
        "type": "Microsoft.DevTestLab/labs",
        "name": "[parameters('newLabName')]",
        "location": "[resourceGroup().location]",
        "resources": [
          {
            "apiVersion": "2018-10-15-preview",
            "name": "[parameters('newLabVirtualNetworkName')]",
            "type": "virtualNetworks",
            "dependsOn": [
              "[resourceId('Microsoft.DevTestLab/labs', parameters('newLabName'))]"
            ],
            "properties": {
              "description": "Existing Compute virtual network associated as part of the lab creation process.",
              "externalProviderResourceId": "[parameters('existingVirtualNetworkId')]",
              "subnetOverrides": [
                {
                  "name": "[parameters('existingSubnetName')]",
                  "resourceId": "[variables('existingSubnetId')]",
                  "useInVmCreationPermission": "Allow",
                  "usePublicIpAddressPermission": "Allow"
                }
              ]
            }
          }
        ]
      }
    ],
    "outputs": {
      "labId": {
        "type": "string",
        "value": "[resourceId('Microsoft.DevTestLab/labs', parameters('newLabName'))]"
      }
    }
  }

Outputs: I tested it on my environment and the results are as below:

enter image description here

enter image description here

enter image description here

enter image description here

Note: Unfortuantely, there are no commands to connect vnet and Devtestlabs using Azure CLI or Azure-Powershell.

BICEP ARM:

@description('The name of the new lab instance to be created.')
param newLabName string

@description('The name of the new lab virtual network instance to be created with the new lab instance being created.')
param newLabVirtualNetworkName string

@description('The resource ID pointing to an existing (compute) virtual network to be referenced by the new lab virtual network instance being created.')
param existingVirtualNetworkId string

@description('The name of an existing (compute) subnet instance to be configured for Lab VM creation.')
param existingSubnetName string = 'default'

var existingSubnetId = '${existingVirtualNetworkId}/subnets/${existingSubnetName}'

resource newLabName_resource 'Microsoft.DevTestLab/labs@2018-10-15-preview' = {
  name: newLabName
  location: resourceGroup().location
}

resource newLabName_newLabVirtualNetworkName 'Microsoft.DevTestLab/labs/virtualNetworks@2018-10-15-preview' = {
  parent: newLabName_resource
  name: newLabVirtualNetworkName
  properties: {
    description: 'Existing Compute virtual network associated as part of the lab creation process.'
    externalProviderResourceId: existingVirtualNetworkId
    subnetOverrides: [
      {
        name: existingSubnetName
        resourceId: existingSubnetId
        useInVmCreationPermission: 'Allow'
        usePublicIpAddressPermission: 'Allow'
      }
    ]
  }
}

output labId string = newLabName_resource.id

Outputs:

enter image description here

enter image description here

Ansuman Bal
  • 9,705
  • 2
  • 10
  • 27
  • Thanks for your answer, but I cannot see that this template is included in Bicep. However, I see that I can use https://learn.microsoft.com/en-us/azure/templates/microsoft.devtestlab/2018-09-15/labs/virtualnetworks?tabs=bicep – A7med Sep 27 '21 at 10:54
  • hello @A7med, i have provided the above for only json arm template , yes we can use bicep as well . let me convert the above json to bicep and add it to the answer. – Ansuman Bal Sep 27 '21 at 11:54
  • 1
    This is a correct way of solving the problem, thanks. Here is the same solution suggested in MS community https://learn.microsoft.com/en-us/answers/questions/566490/how-to-connect-an-existing-virtual-netwrok-to-devt.html – A7med Oct 14 '21 at 11:16