Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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')]"
            }
        }
    }
}
like image 967
Dustin Venegas Avatar asked Sep 01 '16 20:09

Dustin Venegas


People also ask

How do you use depends on in ARM template?

dependsOn. Within your Azure Resource Manager template (ARM template), the dependsOn element enables you to define one resource as a dependent on one or more resources. Its value is a JavaScript Object Notation (JSON) array of strings, each of which is a resource name or ID.

What are the 5 key pieces of a resource manager template?

It will contain five main sections: schema, parameters, variables, resources, and outputs.

What is a difference between using Azure blueprints and Azure Resource Manager templates?

Summary. A Template is the basic model from which each Server gets created. A Blueprint is a saved workflow that can be defined and re-played at any time on the platform.

What are the benefits of using Azure Resource Manager templates?

The benefits of using Resource Manager With Resource Manager, you can: Manage your infrastructure through declarative templates rather than scripts. Deploy, manage, and monitor all the resources for your solution as a group, rather than handling these resources individually.


1 Answers

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'))]"
],
like image 180
bmoore-msft Avatar answered Nov 09 '22 11:11

bmoore-msft