Search code examples
azureazure-resource-managerazure-rm-templateazure-rm

Azure RM Template. Conditinally deploy Copy resource


I need to deploy specific resource if there are more than 4 entries in an array parameter. I'm able to do this with 5 (and more) entries, but I also need the deployment not to fail with 3 or less entries, but rather not create that resource at all. Right now I get the following error with 3 or less entries:

Error: Code=InvalidTemplate; Message=Deployment template validation failed: 'The template 'copy' definition at line '56' and column '19' has an invalid copy count. The copy count must be a positive integer value and cannot exceed '800'. Please see https://aka.ms/arm-copy for usage details.'.

I tried adding condition to the resource:

...
  "resources": [
    {
      "condition": "[greater(length(parameters('apps')),4)]",
      "name": "[concat(parameters('apps')[copyIndex(4)].name,'-webtest')]",
...

and even:

...
  "resources": [
    {
      "condition": false,
      "name": "[concat(parameters('apps')[copyIndex(4)].name,'-webtest')]",
...

but still getting the same error. This is the template:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "apps": {
      "type": "array",
      "defaultValue": [
        {
          "name": "name1",
          "value": "111"
        },
        {
          "name": "name2",
          "value": "222"
        },
        {
          "name": "name3",
          "value": "333"
        },
        {
          "name": "name4",
          "value": "444"
        },
        {
          "name": "webtest5",
          "value": "555"
        }
      ]
    },
    "existingApplicationInsightsName": {
      "type": "string",
      "defaultValue": "appname1"
    }
  },
  "variables": {},
  "resources": [
    {
      "name": "[concat(parameters('apps')[copyIndex(4)].name,'-webtest')]",
      "apiVersion": "2015-05-01",
      "type": "microsoft.insights/webtests",
      "location": "westeurope",
      "tags": {
        "[concat('hidden-link:', resourceId('microsoft.insights/components/', parameters('existingApplicationInsightsName')))]": "Resource"
      },
      "properties": {
        "SyntheticMonitorId": "[parameters('apps')[copyIndex(4)].name]",
        "Name": "[parameters('apps')[copyIndex(4)].name]",
        "Enabled": true,
        "Frequency": 300,
        "Timeout": 120,
        "Kind": "ping",
        "RetryEnabled": true,
        "Locations": [
          {
            "Id": "us-ca-sjc-azr"
          }
        ],
        "Configuration": {
          "WebTest": "[concat('<WebTest Name=\"', parameters('apps')[copyIndex(4)].name, '\"',  ' Id=\"', '9d420f1a-f797-427a-804c-f37373eefc82' ,'\"    Enabled=\"True\" CssProjectStructure=\"\" CssIteration=\"\" Timeout=\"0\" WorkItemIds=\"\" xmlns=\"http://microsoft.com/schemas/VisualStudio/TeamTest/2010\" Description=\"\" CredentialUserName=\"\" CredentialPassword=\"\" PreAuthenticate=\"True\" Proxy=\"default\" StopOnError=\"False\" RecordedResultFile=\"\" ResultsLocale=\"\">        <Items>        <Request Method=\"GET\" Guid=\"a5f10126-e4cd-570d-961c-cea43999a200\" Version=\"1.1\" Url=\"', 'http://www.microsoft.com' ,'\" ThinkTime=\"0\" Timeout=\"300\" ParseDependentRequests=\"True\" FollowRedirects=\"True\" RecordResult=\"True\" Cache=\"False\" ResponseTimeGoal=\"0\" Encoding=\"utf-8\" ExpectedHttpStatusCode=\"', 200 ,'\" ExpectedResponseUrl=\"\" ReportingName=\"\" IgnoreHttpStatusCode=\"False\" /></Items></WebTest>')]"
        }
      },
      "copy": {
        "name": "createWebTests",
        "count": "[sub(length(parameters('apps')),4)]"
      }
    }
  ]
}

Solution

  • try doing it like so:

    "condition": "[greater(length(parameters('apps')),4)]",
    

    and set you copy to this:

    "copy": {
      "name": "createWebTests",
      "count": "[if(greater(length(parameters('apps')),4), sub(length(parameters('apps')),4), 1)]"
    }
    

    this should work around the fact that in your case count is negative and still not deploy anything when there are less than 4 items in your array