7
votes

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.

3

3 Answers

11
votes

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.

3
votes

No, this makes no sense, the dependsOn property is meant to track dependencies inside the ARM template, so it can provision resources in specific order. If a resource is there, there's no sense to track it. It's already there. You just reference it when you use it.

0
votes

Yes. DependsOn is used when you are creating a resources which depends on another resource which you are creating via the same template. If the resource is already created then you just add a reference to it. In your case, you can add properties key for the VM like this:

"properties": {
                "hardwareProfile": {
                    "vmSize": "Standard_DS1"
                },
                "storageProfile": {
                    "imageReference": {
                        "publisher": "MicrosoftWindowsServerHPCPack",
                        "offer": "WindowsServerHPCPack",
                        "sku": "2012R2",
                        "version": "latest"
                    },
                    "osDisk": {
                        "name": "[parameters('virtualMachines_APP01_name')]",
                        "createOption": "FromImage",
                        "vhd": {
                            "uri": "[concat('https', '://', parameters('storageAccounts_vmdkstorageacct_name'), '.blob.core.windows.net', concat('/vhds/', parameters('virtualMachines_APP01_name'),'APP01.vhd'))]"
                        },
                        "caching": "ReadWrite"
                    },
                    "dataDisks": []
                },
                "osProfile": {
                    "computerName": "[parameters('virtualMachines_APP01_name')]",
                    "adminUsername": "vmadmin",
                    "windowsConfiguration": {
                        "provisionVMAgent": true,
                        "enableAutomaticUpdates": true
                    },
                    "secrets": [],
                    "adminPassword": "[parameters('virtualMachines_APP01_adminPassword')]"
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaces_app01_name'))]"
                        }
                    ]
                }
            }