7

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.

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
frank.meng
  • 83
  • 1
  • 1
  • 3

3 Answers3

11

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.

Bruce Chen
  • 18,207
  • 2
  • 21
  • 35
  • yes, I use reference function. "outputs": { "storageAccountInfo": { "value": "[reference(concat('Microsoft.Storage/storageAccounts/', parameters('storageAccountname')),providers('Microsoft.Storage', 'storageAccounts').apiVersions[0])]", "type": "object" } } – frank.meng Dec 19 '16 at 03:35
3

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.

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • yes, I use reference function. "outputs": { "storageAccountInfo": { "value": "[reference(concat('Microsoft.Storage/storageAccounts/', parameters('storageAccountname')),providers('Microsoft.Storage', 'storageAccounts').apiVersions[0])]", "type": "object" } } – frank.meng Dec 19 '16 at 03:35
  • There is at least one case where this could actually make sense, as it seems that deployment is eventually consistent in ARM. Say I deploy resource B, dependent on resource A, both with a linked template, type `Microsoft.Resources/deployments`. A `dependsOn` is set from B to A. The point is that when the deployment of A is over it might be the case that the resources in B don't see those in A yet, causing the B deployment to fail. Having a look around quite a few people are complaining about this, I was not able to find a solution yet. – reim Nov 11 '20 at 14:48
  • That's exactly what I am doing, and I have an example where it does not work - we might be filing an issue. – reim Nov 11 '20 at 19:11
  • mmm, that shouldn't be possible. do you have a repro? – 4c74356b41 Nov 12 '20 at 08:13
  • not true, there are many cases where a resource already exits outside the template but is required by microsoft documentation .e.g. https://learn.microsoft.com/en-us/azure/azure-functions/functions-infrastructure-as-code?tabs=linux#app-service-plan says we need a dependent storage account. this might already exist outside the function deployement – Tiju John Jul 26 '22 at 09:50
  • but you don't need to dependsOn the resource, its not part of the template, its already there, please, I've been doing this for 7 years. – 4c74356b41 Jul 27 '22 at 06:13
0

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'))]"
                        }
                    ]
                }
            }
  • yes, I use reference function. "outputs": { "storageAccountInfo": { "value": "[reference(concat('Microsoft.Storage/storageAccounts/', parameters('storageAccountname')),providers('Microsoft.Storage', 'storageAccounts').apiVersions[0])]", "type": "object" } } – frank.meng Dec 19 '16 at 03:35