2

I am trying to create a custom image in my Azure DevTestLab, from a vhd image stored in the default storage account.

I started modifying the file at the GitHub repo for ARM templates for creating a custom image from a vhd.

I have already created the DevTest lab, and I can run the following Powershell command to get its data:

PS C:\git> $rid = Get-AzureRmResource -ResourceGroupName jgTl9-rg -ResourceType Microsoft.DevTestLab/labs -ResourceName jgTl9 -ApiVersion 2016-05-15

This yields a bunch of information. If I want to extract the default storage account name, I do the following:

PS C:\git> $rid.properties.defaultStorageAccount

So I know it's possible to get it. Now I have modified the variables section of the Azure build template (a JSON file) to look as follows:

"variables": {
  "resourceName": "[concat(variables('existingLabName'), '/', variables('imageName'))]",
  "resourceType": "Microsoft.DevTestLab/labs/customimages", 
  "existingLabName": "[replace(resourceGroup().name,'-rg','')]",
  "storageAccountID": "/subscriptions/xxxxxxxxxxxx/resourceGroups/jgTl9-rg/providers/Microsoft.Storage/storageAccounts/djgtl91795",
  "saidFrags": "[skip(split(variables('storageAccountID'), '/' ), add(length(split(variables('storageAccountID'), '/' )),-1) ) ]",  
  "storageAccountSuffix": "[take(variables('saidFrags'),1)[0]]",
  "existingVhdUri": "[concat('https://',variables('storageAccountSuffix'),'.blob.core.windows.net/uploads/',parameters('imageFileName'))]",
  "imageName": "JaysImage"

},

This code actually works when I call the New-AzureRmResourceGroupDeployment command with the azuredeploy.json and azuredeploy.parameters.json.

However, instead of hardcoding the storageAccountID variable, I'd like to extract the defaultStorageAccount from the environment. I tried something like this:

   "storageAccountID": "[string( resourceId(resourceGroup().name,'MicrosoftDevTestLab/labs','jgTl9').properties.defaultStorageAccount  ))",

I tried a few variations on this but nothing worked. What is the syntax I need to use to extract the same information as I have hardcoded for the storageAccountID?

(I could not find anything in Microsoft's description of template functions).

Jay Godse
  • 15,163
  • 16
  • 84
  • 131

1 Answers1

0

Couple of issues with your following expression

"storageAccountID": "[string( resourceId(resourceGroup().name,'MicrosoftDevTestLab/labs','jgTl9').properties.defaultStorageAccount  ))",
  1. There is syntax issue ie. an extra ) at the end.
  2. The resource provider MicrosoftDevTestlab/labs should be Microsoft.DevTestlab/labs
  3. When resourceId is used it will not give you the entire object. So when you do properties.defaultStorageAccount it will not work. Because it gives you just the string of your dev test lab and nothing else and there is no property on a string of defaultStorageAccount.

Now what you need is reference function like below

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {    
  },
  "variables": {
    "vDefaultStorageAccount": "[string(resourceId('yourResourceGroupName','Microsoft.DevTestLab/labs','yourTestLabName'))]"
  },
  "resources": [],
  "outputs": {        
    "defaultStorageAccount":{
      "value": "[reference(variables('vDefaultStorageAccount'),'2016-05-15').defaultStorageAccount]",
      "type":"string"
    }        
  }
}

In the variable, vDefaultStorageAccount we get the ID of the DevTestLab and then we use reference function to get the object. Then you can do . to get properties. You don't need to do properties.defaultStorageAccount for example.

Mitul
  • 9,734
  • 4
  • 43
  • 60