0
votes

I've been trying to learn ARM but I keep getting stuck on the same error and I can't find much on ARM online so I don't really get what I'm doing wrong, could someone help me out here and tell me what I'm doing wrong and how to fix it?

Here is my Error

Template deployment returned the following errors:
11:05:16 - 11:05:15 - Resource Microsoft.Insights/alertRules 'cpu_percent' failed with message '{
11:05:16 -   "code": "TargetResourceNotFound",
11:05:16 -   "message": "Exception of type 'Microsoft.WindowsAzure.Management.Monitoring.MonitoringServiceException' was thrown."
11:05:16 - Deploying template using PowerShell script failed.
11:05:16 - Tell us about your experience at https://go.microsoft.com/fwlink/?LinkId=691202
11:05:16 - }'
11:05:16 - 11:05:15 - Template output evaluation skipped: at least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-debug for usage details.
11:05:16 - 11:05:15 - Template output evaluation skipped: at least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-debug for usage details.
11:05:16 - 
11:05:16 -

And here is my code

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "sendToServiceOwners": {
      "type": "bool",
      "defaultValue": true,
      "metadata": {
            "description": "Specifies if alerts are sent to service owners"
      }
    },
    "windowSize": {
      "type": "string",
      "defaultValue": "00:05:00",
      "metadata": {
            "description": "Period of time to monitor for"
      }
    },
    "aggregation": {
      "type": "string",
      "defaultValue": "Average",
      "allowedValues": [
        "Average",
        "Last",
        "Maximum",
        "Minimum",
        "Total"
      ],
      "metadata": {
            "description": "The way collected data should be compared"  
      }
    },
    "threshold": {
      "type": "string",
      "defaultValue": "80",
      "metadata": {
            "description":  "The treshold value at which the alert will trigger"
      }
    },
    "operator": {
      "type": "string",
      "defaultValue": "GreaterThan",
      "allowedValues": [
        "GreaterThan",
        "GreaterThanOrEqual",
        "LessThan",
        "LessThanOrEqual"
      ],
      "metadata": {
            "description": "Different sorts of operators for comparing the value"
      }
    },
    "metricName": {
      "type": "string",
      "defaultValue": "cpu_percent",
      "metadata": {
            "description": "Name of the specific Metric"
      }
    },
    "resourceId": {
      "type": "string",
      "defaultValue": "[resourceId('Microsoft.Sql/servers', parameters('serverName'))]",
      "metadata": {
            "description": "Resource ID"
      }
    },
    "isEnabled": {
      "type": "bool",
      "defaultValue": true,
      "metadata": {
            "description": "Specifying if alert is enabled"
      }
    },
    "alertDescription": {
      "type": "string",
      "defaultValue": "Alert used for Checking CPU for 80% Spikes"
    },
    "alertName": {
      "type": "string",
      "defaultValue": "cpu_percent"
    },
    "ugentecdbtestName": {
      "type": "string",
      "minLength": 1
    },
    "serverNameAdminLogin": {
      "type": "string",
      "minLength": 1,
      "defaultValue": "*********"
    },
    "serverNameAdminLoginPassword": {
      "defaultValue": ********,
      "minLength": 1,
      "type": "securestring"
    },
    "ugentecdbtestCollation": {
      "type": "string",
      "minLength": 1,
      "defaultValue": "SQL_Latin1_General_CP1_CI_AS"
    },
    "ugentecdbtestEdition": {
      "type": "string",
      "defaultValue": "Basic",
      "allowedValues": [
        "Basic",
        "Standard",
        "Premium"
      ]
    },
    "ugentecdbtestRequestedServiceObjectiveName": {
      "type": "string",
      "defaultValue": "Basic",
      "allowedValues": [
        "Basic",
        "S0",
        "S1",
        "S2",
        "P1",
        "P2",
        "P3"
      ],
      "metadata": {
        "description": "Describes the performance level for Edition"
      }
    },
    "serverName": {
      "type": "string",
      "defaultValue": "ugentecserver"
    }
  },
  "variables": {
  },
  "resources": [
    {
      "name": "[parameters('serverName')]",
      "type": "Microsoft.Sql/servers",
      "location": "West Europe",
      "apiVersion": "2014-04-01-preview",
      "properties": {
        "administratorLogin": "[parameters('serverNameAdminLogin')]",
        "administratorLoginPassword": "[parameters('serverNameAdminLoginPassword')]",
        "version": "12.0"
      }
    },
    {
          "name": "[concat(parameters('serverName'), '/', parameters('ugentecdbtestName'))]",
          "type": "Microsoft.Sql/servers/databases",
          "location": "westeurope",
          "apiVersion": "2014-04-01-preview",
          "dependsOn": [ "[resourceId('Microsoft.Sql/servers', parameters('serverName'))]" ],
          "properties": {
            "collation": "[parameters('ugentecdbtestCollation')]",
            "edition": "[parameters('ugentecdbtestEdition')]",
            "maxSizeBytes": "1073741824",
            "requestedServiceObjectiveName": "[parameters('ugentecdbtestRequestedServiceObjectiveName')]"
          }
     },
    {
      "type": "Microsoft.Insights/alertRules",
      "name": "[parameters('alertName')]",
      "dependsOn": ["[resourceId('Microsoft.Sql/servers/databases', parameters('serverName'),parameters('ugentecdbtestName'))]"],
      "location": "West Europe",
      "apiVersion": "2016-03-01",
      "properties": {
        "name": "[parameters('alertName')]",
        "description": "[parameters('alertDescription')]",
        "isEnabled": "[parameters('isEnabled')]",
        "condition": {
          "odata.type": "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition",
          "dataSource": {
            "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleMetricDataSource",
            "resourceUri": "[parameters('resourceId')]",
            "metricName": "[parameters('metricName')]",
            "operator": "[parameters('aggregation')]"
          },
          "threshold": "[parameters('threshold')]",
          "windowSize": "[parameters('windowSize')]",
          "timeAggregation": "[parameters('aggregation')]"
        },
        "actions": [
          {
            "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleEmailAction",
            "sendToServiceOwners": "[parameters('sendToServiceOwners')]"
          }
        ]
      }
    }
   ],
  "outputs": {
    "testOutput": {
      "type": "string",
      "value": "test123"
    },
    "DbAdoConnString": {
      "type": "string",
      "value": "[concat('Server=tcp:',reference(parameters('serverName')).fullyQualifiedDomainName,',1433;Initial Catalog=',parameters('ugentecdbtestName'),';Persist Security Info=False;User ID=',reference(parameters('serverName')).administratorLogin,';Password=',reference(parameters('serverName')).administratorLoginPassword,';MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;')]"
    }

  }
    }
2

2 Answers

1
votes

If I would guess what is going wrong it is the parameter 'resourceId':

"resourceUri": "[parameters('resourceId')]",

Change this to:

"resourceUri":  "[resourceId('Microsoft.Sql/servers', parameters('serverName'))]",

to set it on the server level

0
votes

I've fixed a couple of issues and did some refactoring based on the original ARM template in the question above and tested working in Azure portal's template deployment.

Note: Need to update the values of the necessary params such as serverNameAdminLogin and serverNameAdminLoginPassword etc.

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "sendToServiceOwners": {
      "type": "bool",
      "defaultValue": true,
      "metadata": {
        "description": "Specifies if alerts are sent to service owners"
      }
    },
    "windowSize": {
      "type": "string",
      "defaultValue": "00:05:00",
      "metadata": {
        "description": "Period of time to monitor for"
      }
    },
    "aggregation": {
      "type": "string",
      "defaultValue": "Average",
      "allowedValues": [
        "Average",
        "Last",
        "Maximum",
        "Minimum",
        "Total"
      ],
      "metadata": {
        "description": "The way collected data should be compared"
      }
    },
    "threshold": {
      "type": "string",
      "defaultValue": "80",
      "metadata": {
        "description": "The treshold value at which the alert will trigger"
      }
    },
    "operator": {
      "type": "string",
      "defaultValue": "GreaterThan",
      "allowedValues": [
        "GreaterThan",
        "GreaterThanOrEqual",
        "LessThan",
        "LessThanOrEqual"
      ],
      "metadata": {
        "description": "Different sorts of operators for comparing the value"
      }
    },
    "metricName": {
      "type": "string",
      "defaultValue": "cpu_percent",
      "metadata": {
        "description": "Name of the specific Metric"
      }
    },
    "isEnabled": {
      "type": "bool",
      "defaultValue": true,
      "metadata": {
        "description": "Specifying if alert is enabled"
      }
    },
    "alertDescription": {
      "type": "string",
      "defaultValue": "Alert used for Checking CPU for 80% Spikes"
    },
    "alertName": {
      "type": "string",
      "defaultValue": "cpu_percent"
    },
    "ugentecdbtestName": {
      "type": "string",
      "minLength": 1
    },
    "serverNameAdminLogin": {
      "type": "string",
      "minLength": 1,
      "defaultValue": "**********"
    },
    "serverNameAdminLoginPassword": {
      "defaultValue": "**********",
      "minLength": 1,
      "type": "securestring"
    },
    "ugentecdbtestCollation": {
      "type": "string",
      "minLength": 1,
      "defaultValue": "SQL_Latin1_General_CP1_CI_AS"
    },
    "ugentecdbtestEdition": {
      "type": "string",
      "defaultValue": "Basic",
      "allowedValues": [
        "Basic",
        "Standard",
        "Premium"
      ]
    },
    "ugentecdbtestRequestedServiceObjectiveName": {
      "type": "string",
      "defaultValue": "Basic",
      "allowedValues": [
        "Basic",
        "S0",
        "S1",
        "S2",
        "P1",
        "P2",
        "P3"
      ],
      "metadata": {
        "description": "Describes the performance level for Edition"
      }
    },
    "serverName": {
      "type": "string",
      "defaultValue": "ugentecserver"
    }
  },
  "variables": {
  },
  "resources": [
    {
      "name": "[parameters('serverName')]",
      "type": "Microsoft.Sql/servers",
      "location": "West Europe",
      "apiVersion": "2014-04-01-preview",
      "properties": {
        "administratorLogin": "[parameters('serverNameAdminLogin')]",
        "administratorLoginPassword": "[parameters('serverNameAdminLoginPassword')]",
        "version": "12.0"
      },
      "resources": [
        {
          "name": "[parameters('ugentecdbtestName')]",
          "type": "databases",
          "location": "westeurope",
          "apiVersion": "2015-05-01-preview",
          "dependsOn": [ "[resourceId('Microsoft.Sql/servers', parameters('serverName'))]" ],
          "properties": {
            "collation": "[parameters('ugentecdbtestCollation')]",
            "edition": "[parameters('ugentecdbtestEdition')]",
            "maxSizeBytes": "1073741824",
            "requestedServiceObjectiveName": "[parameters('ugentecdbtestRequestedServiceObjectiveName')]"
          }
        }
      ]
    },
    {
      "type": "Microsoft.Insights/alertRules",
      "name": "[parameters('alertName')]",
      "dependsOn": [
        "[resourceId('Microsoft.Sql/servers', parameters('serverName'))]",
        "[resourceId('Microsoft.Sql/servers/databases', parameters('serverName'),parameters('ugentecdbtestName'))]"
      ],
      "location": "West Europe",
      "apiVersion": "2016-03-01",
      "properties": {
        "name": "[parameters('alertName')]",
        "description": "[parameters('alertDescription')]",
        "isEnabled": "[parameters('isEnabled')]",
        "condition": {
          "odata.type": "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition",
          "dataSource": {
            "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleMetricDataSource",
            "resourceUri": "[resourceId('Microsoft.Sql/servers/databases', parameters('serverName'),parameters('ugentecdbtestName'))]",
            "metricName": "[parameters('metricName')]",
            "operator": "[parameters('aggregation')]"
          },
          "threshold": "[parameters('threshold')]",
          "windowSize": "[parameters('windowSize')]",
          "timeAggregation": "[parameters('aggregation')]"
        },
        "actions": [
          {
            "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleEmailAction",
            "sendToServiceOwners": "[parameters('sendToServiceOwners')]"
          }
        ]
      }
    }
  ],
  "outputs": {
    "DbAdoConnString": {
      "type": "string",
      "value": "[concat('Server=tcp:',reference(parameters('serverName')).fullyQualifiedDomainName,',1433;Initial Catalog=',parameters('ugentecdbtestName'),';Persist Security Info=False;User ID=',reference(parameters('serverName')).administratorLogin,';Password=',reference(parameters('serverName')).administratorLoginPassword,';MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;')]"
    }
  }
}

enter image description here

enter image description here