Azure ARM Template dependsOn: Resource Not Defined in Template

frank.meng picture frank.meng · Dec 15, 2016 · Viewed 10.4k times · Source

I want to create a new vm to resource group in azure with visual studio 2015. the new vm depends on an existing resource in the same resource group , that isn’t declared in the template. but I got "The resource 'Microsoft.Storage/storageAccounts/***' is 02:21:10 - not defined in the template"

"resources": [
{
  "apiVersion": "2015-06-15",
  "type": "Microsoft.Compute/virtualMachines",
  "name": "[variables('vmName')]",
  "location": "[resourceGroup().location]",
  "tags": {
    "displayName": "VirtualMachine"
  },
  "dependsOn": [
    "[resourceId('0abb7c58-93b4-45f4-b1be-61a98ac347a3','securitydata','Microsoft.Storage/storageAccounts', parameters('storageAccounts_simscitestrg6892_name'))]"
  ],

DependsOn can only refer to resources in the same ARM template ?

Any help appreciated.

Regards, Frank.

Answer

Bruce Chen picture Bruce Chen · Dec 15, 2016

DependsOn can only refer to resources in the same ARM template ?

From this official document about defining dependencies in Azure Resource Manager templates, we could find as follows:

Resource Manager evaluates the dependencies between resources, and deploys them in their dependent order. When resources are not dependent on each other, Resource Manager deploys them in parallel. You only need to define dependencies for resources that are deployed in the same template.

Based on my test, I could reproduce this issue. You need to add the Storage resource within your template as follows:

{
    "name": "[parameters('storageAccounts_simscitestrg6892_name')]",
    "type": "Microsoft.Storage/storageAccounts",
    "location": "[resourceGroup().location]",
    "apiVersion": "2015-06-15",
    "dependsOn": [],
    "tags": {
      "displayName": "StorageAccountResourceName"
    },
    "properties": {
      "accountType": "[parameters('StorageAccountType')]"
    }
}

For your VM resource, you could configure the osDisk under the "properties > storageProfile" section as follows:

"osDisk": {
  "name": "Your-VMOSDisk",
  "vhd": {
    "uri": "[concat('https://', parameters('storageAccounts_simscitestrg6892_name'), '.blob.core.windows.net/', variables('Your-VMStorageAccountContainerName'), '/', variables('Your-VMOSDiskName'), '.vhd')]"
  },
  "caching": "ReadWrite",
  "createOption": "FromImage"
}

The storage resource would be created under the same location as your VM, if not exists.