Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure ARM Templates (DependsOn)

Looking to get some guidance and explanation around dependsOn.

I have seen in templates there are two methods of providing dependencies in a template.

One method is to provide resourceId and the other method is to provide a string value using concat. I'm trying to understand the difference between the two.

Example

[concat('Microsoft.Network/networkInterfaces/', variables('networkInterfaceName'))]

and also on some examples, this is referenced with resourceId:

[resourceId('Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]

I am looking to understand the difference and also in which scenarios we should use concat to reference dependencies and where we can use resourceID

like image 629
WS2012R2 Avatar asked Mar 09 '17 01:03

WS2012R2


People also ask

How do I get the resource ID in ARM template?

The first way is to look for it on this Microsoft Azure resource page here; the second option is when using ARM Templates, just look at the type line of the resource and you will find it there; a third and easy way to spot the option is to check the id of the object. It provides you the resource type on it.

Where are Azure ARM templates stored?

Since January, a new feature to manage ARM templates is being tested and made available in Azure Portal: ARM Template Specs. The Template Specs allow us to store ARM templates in Azure Portal and re-use them. These ARM templates will work as a model to build resources in our company cloud environment.

What is 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 best practices to create Azure ARM template?

Always use parameters for user names and passwords (or secrets). Use securestring for all passwords and secrets. If you pass sensitive data in a JSON object, use the secureObject type. Template parameters with secure string or secure object types can't be read after resource deployment.


2 Answers

The real difference between when you use resourceId or not (in dependsOn) is this: Is the resource you depend on in the same template? If so, you can simply just have the name. For example, here is a load balancer, which depends on a public IP and a vNet that are created in the same template:

  "apiVersion": "[variables('lbApiVersion')]",
  "type": "Microsoft.Network/loadBalancers",
  "name": "[variables('lbName1')]",
  "location": "[variables('computeLocation')]",
  "dependsOn": [
    "[variables('lbIPName1')]",
    "[variables('virtualNetworkName')]"
  ],
  "properties": {
    "frontendIPConfigurations": [
      {
        "name": "LoadBalancerIPConfig",
        "properties": {
          "publicIPAddress": {
            "id": "[resourceId('Microsoft.Network/publicIPAddresses', variables('lbIPName1'))]"
          }

If you are referencing a resource from outside the template, then you need resourceId. This is not needed with dependsOn, as you can only depend on resources that are in the same template. Where it is needed in my example, is the publicIPAddress id. This property needs a full resource Id, which is what the resourceId function provides. So the question becomes, does that property need a full resource Id?

like image 182
Edward Rixon Avatar answered Sep 21 '22 17:09

Edward Rixon


Difference:

concat: Combines multiple string values and returns the concatenated string.

resourceId: Returns the unique identifier of a resource.

Example "[concat('Microsoft.Network/networkInterfaces/', variables('networkInterfaceName'))]"

Result: Microsoft.Network/networkInterfaces/{networkInterfaceName}

Example: [resourceId('Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]",

Result: /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/{Microsoft.Compute/virtualMachines}/{networkInterfaceName}/

More Functions in the ARM template please refer to document.

like image 40
Tom Sun - MSFT Avatar answered Sep 17 '22 17:09

Tom Sun - MSFT