简体   繁体   中英

Handling dependencies in Azure Resource Manager Linked Templates

Azure Resource Manager (ARM) Templates have the ability to use Linked Templates . These linked templates can define additional resources to create during an ARM template deployment.

ARM templates support dependencies which ensure some resources are created before others are.

I would like to specify a dependency in a linked template for a resource created in the master template. If I include the dependency in the Linked Template, it looks like this:

"resources": [
    {
        "apiVersion": "2015-08-01",
        "type": "Microsoft.Web/sites/hostNameBindings",
        "name": "[concat(parameters('siteName'),'/', parameters('fqdn'))]",
        "dependsOn": [
            "[concat('Microsoft.Web/sites/', parameters('siteName'))]"
        ],
        "properties": {
            "siteName": "[parameters('siteName')]"
        }
    }
]

While the dependsOn appears correct, the a resource is created at Microsoft.Web/sites/{siteNameParameter} , deploying the ARM template outputs the following error message:

InvalidTemplate : Deployment template validation failed: 'The resource 'Microsoft.Web/sites/blahblahblahblah' is not defined in the template. Please see https://aka.ms/arm-template for usage details.'.

I am currently defining this dependency in the master template when I define the linked template call. This seems brittle and easy to break. Is there a better way than defining dependencies in the master ARM template?

{
    "apiVersion": "2015-01-01",
    "name": "SomeName",
    "type": "Microsoft.Resources/deployments",
    "dependsOn": [
        "[concat('Microsoft.Web/sites/', parameters('siteName'))]"
    ],
    "properties": {
        "mode": "Incremental",
        "templateLink": {
            "uri": "https://tempuri.org/supersecrettemplatepath/azuredeploy.json",
            "contentVersion": "1.0.0.0"
        },
        "parameters":
        {
            "fqdn": {
                "value": "www.tempuri.org"
            },
            "siteName": {
                "value": "[parameters('siteName')]"
            }
        }
    }
}

You can define the dependency either way - both are valid. Putting the dependency on the deployment resource (your second approach) will mean that the entire nested deployment is not started until the web site is provisioned. If you wanted to kick some things off in parallel, then you would put the dependency in the nested template (your first approach). That may or may not matter for your scenario, but that's a key difference.

dependsOn requires a resourceId - and as the error is trying to say, if the resource is not defined in the template you need more detail in the resourceId , in this case, you need the resourceGroup (maybe the subscription, but I doubt it). So for example you can use:

"dependsOn": [
    "[resourceId(resourceGroup().name, 'Microsoft.Web/sites', parameters('siteName'))]"
],

The dependsOn needs the name of the linked deployment, not one of the resources in it.

eg:

dependsOn: "Microsoft.Resources/deployments/myExternalTemplate"

"dependsOn": [
    "[resourceId(subscription().subscriptionId, resourceGroup().name, 'Microsoft.Web/sites', parameters('siteName'))]"
],

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM