4
votes

I have an ARM template that creates an arbitrary number of Azure webapps using the copy construct, like so (non-relevant portions removed):

{
  "parameters": { 
    "numWebsites": {
      "type": "int",
      "defaultValue": 2
    }
  },
  "resources": [
    "name": "[concat('webapp-', copyIndex()]",
    "type": "Microsoft.Web/sites",
    "copy": {
      "name": "websitescopy",
      "count": "[parameters('numWebsites')]"
    }
  ]
}

I'd also like to create a Traffic Manager profile with an endpoint for each of the websites created. However, there doesn't seem to be a way to make use of copy in the Traffic Manager resource's endpoints parameter. All of the examples I've seen have the endpoints explicitly listed out, but I don't know how many webapps are being created ahead of time so that doesn't work for me.

How can I generate the endpoints in my template dynamically? I've tried using a copy statement in the trafficManagerProfiles resource, but that creates multiple profiles with a single endpoint each.

4

4 Answers

1
votes

Here's an example of creating an external endpoint as a "child resource", the profile is created separately without any endpoints and then this resource adds the endpoint. It uses an external endpoint but should work just as well for a webapp and is compatible with the standard copy function.

HtH, Gareth

    {
        "apiVersion": "2015-11-01",
        "type": "Microsoft.Network/trafficManagerProfiles/ExternalEndpoints",
        "name": "ExternalEndpointExample/endpoint1",
        "dependsOn": ["Microsoft.Network/trafficManagerProfiles/ExternalEndpointExample"],
        "location": "global",
        "properties": {
            "target": "ep1.microsoft.com",
            "endpointStatus": "Enabled",
            "endpointLocation": "northeurope"
        }
    }
1
votes

I have not tested this yet, but it appears that copy/copyIndex should now be a supported scenario for traffic manager endpoints:

https://feedback.azure.com/forums/217313-networking/suggestions/12907815-support-copy-copyindex-in-traffic-manager-depend

Here is a sample that I implemented a while back:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "solution-abbreviation": {
      "type": "string",
      "minLength": 1
    },
    "environment-abbreviation": {
      "type": "string",
      "allowedValues": [
        "dev",
        "test",
        "prod"
      ]
    },

    "userinterface-abbreviation": {
      "type": "string",
      "minLength": 1
    },
    "userinterface-locations": {
      "type": "array",
      "minLength": 1
    },
    "userinterface-appserviceplan-sku": {
      "type": "string",
      "allowedValues": [
        "Free",
        "Shared",
        "Basic",
        "Standard"
      ]
    },
    "userinterface-appserviceplan-workersize": {
      "type": "string",
      "allowedValues": [
        "0",
        "1",
        "2"
      ]
    },
    "userinterface-appserviceplan-numberofworkers": {
      "type": "int"
    }
  },
  "variables": {
    "userinterface-trafficmanager-name": "[concat(parameters('solution-abbreviation'), '-', parameters('environment-abbreviation'), '-', parameters('userinterface-abbreviation'))]"
  },
  "resources": [
    {
      "name": "[concat(variables('userinterface-trafficmanager-name'), '-', parameters('userinterface-locations')[copyIndex()])]",
      "type": "Microsoft.Web/serverfarms",
      "location": "[parameters('userinterface-locations')[copyIndex()]]",
      "apiVersion": "2014-06-01",
      "dependsOn": [ ],
      "tags": {
        "displayName": "[concat(variables('userinterface-trafficmanager-name'), '-', parameters('userinterface-locations')[copyIndex()])]"
      },
      "copy": {
        "name": "[concat('serverfarms', '-copy')]",
        "count": "[length(parameters('userinterface-locations'))]"
      },
      "properties": {
        "name": "[concat(variables('userinterface-trafficmanager-name'), '-', parameters('userinterface-locations')[copyIndex()])]",
        "sku": "[parameters('userinterface-appserviceplan-sku')]",
        "workerSize": "[parameters('userinterface-appserviceplan-workersize')]",
        "numberOfWorkers": "[parameters('userinterface-appserviceplan-numberofworkers')]"
      }
    }
  ],
  "outputs": {
  }
}
1
votes

You can refer the below template to add traffic manager endpoints with copy enabled.

Azure does not provide functionality to add endpoints in copy, so you need to create a separate resource and link it to the original resource to add the endpoints. This way copy functionality is supported inside the template.

"resources": [
    {
      "apiVersion": "2017-05-01",
      "type": "Microsoft.Network/trafficManagerProfiles",
      "name": "[parameters('resourceName')]",
      "location": "global",
      "properties": {
        "profileStatus": "Enabled",
        "trafficRoutingMethod": "Performance",
        "dnsConfig": {
          "relativeName": "[parameters('uniqueDnsName')]",
          "ttl": "[parameters('timeToLive')]"
        },
        "monitorConfig": {
          "protocol": "[parameters('protocol')]",
          "port": "[parameters('portName')]",
          "path": "[parameters('pathName')]",
          "intervalInSeconds": "[parameters('monitorIntervalInSeconds')]",
          "timeoutInSeconds": "[parameters('monitorTimeoutInSeconds')]",
          "toleratedNumberOfFailures": "[parameters('toleratedNumberOfFailures')]"
        }
      }
    },
    {
      "apiVersion": "2017-05-01",
      "type": "Microsoft.Network/trafficManagerProfiles/azureEndpoints",
      "dependsOn": [
        "Microsoft.Network/trafficManagerProfiles/ExampleTMProfile"
      ],
      "location": "global",
      "name": "[concat('ExampleTMProfile/Endpoint', copyIndex())]",
      "copy": {
        "name": "azureEndpointsCopy",
        "count": "[length(parameters('azureEndpointNameArray'))]"
      },
      "properties": {
        "targetResourceId": "[resourceId('Microsoft.Network/publicIPAddresses', parameters('azureEndpointNameArray')[copyIndex()])]",
        "endpointStatus": "Enabled"
      }
    }
  ]
0
votes

The accepted answer was unclear to me and Paul's answer so far provides only part of the example. During troubleshooting I faced another error related to names segment lengths which was not easy to understand, so here is mine working solution (non-relevant portions removed too):

    {
  "type": "Microsoft.Network/trafficManagerProfiles",
  "apiVersion": "2017-05-01",
  "location": "global",
  "name": "[variables('trafManagerProfileName')]",
  "properties": { ...}
},
{
  "apiVersion": "2015-11-01",
  "type": "Microsoft.Network/trafficManagerProfiles/ExternalEndpoints",
  "name": "[concat(variables('trafManagerProfileName'), '/Endpoint', copyIndex())]",
  "dependsOn": [
    "[concat('Microsoft.Network/trafficManagerProfiles/', variables('trafManagerProfileName'))]",
    "[concat(parameters('app_name')[copyIndex()])]"
  ],
  "location": "global",
  "properties": {
    "target": "[concat(parameters('app_name')[copyIndex()])]"        
  },
  "copy": {
    "count": "[variables('app_count')]",
    "name": "app_copy"
  }
},
{
  "type": "Microsoft.Web/sites",
  "name": "[concat(parameters('app_name')[copyIndex()])]",
  "copy": {
    "count": "[variables('app_count')]",
    "name": "app_copy"
  }
}