I work for Microsoft as an App Innovation Cloud Solution Architect in the UK. The posts and comments on this site are my own opinions and are not endorsed in any way by my employer.

Adding Diagnostic Settings to API Management (and other resources) using ARM templates

This one is a bit of a gotcha around building an API Management (APIM) resource using ARM templates and concerns the diagnostic settings, shown below….

If you extract the Template (Export Template option) from the Azure portal you will notice that these settings are not included. The reason is that these are actually resources of the Azure Monitor implementation not the APIM.

For more details, take a look at the Azure Monitor documentation here. This contains a good set of examples for other resources but not APIM (is on my backlog to amend it).

Here is a sample ARM resource template that adds all the available logger types (Event Hub, Storage and Log Analytics)

{
    "type": "Microsoft.ApiManagement/service/providers/diagnosticSettings",
    "apiVersion": "2017-05-01-preview",
    "name": "[concat(parameters('apimName'),'/microsoft.insights/', parameters('diagnosticSettingName'))]",
    "dependsOn": [],
    "properties": {
        "workspaceId": "[parameters('diagnosticWorkspaceId')]",
        "storageAccountId": "[parameters('storageAccountId')]",
        "eventHubAuthorizationRuleId": "[parameters('eventHubAuthorizationRuleId')]",
        "eventHubName": "[parameters('eventHubName')]",
        "metrics": [
            {
                "category": "AllMetrics",
                "enabled": true,
                "retentionPolicy": {
                    "enabled": false,
                    "days": 0
                }
            }
        ],
        "logs": [
            {
                "category": "GatewayLogs",
                "categoryGroup": null,
                "enabled": true,
                "retentionPolicy": {
                    "enabled": false,
                    "days": 0
                }
            },
            {
                "category": "WebSocketConnectionLogs",
                "categoryGroup": null,
                "enabled": false,
                "retentionPolicy": {
                    "enabled": false,
                    "days": 0
                }
            }
        ]
    }
}

A few bits to call out…

The resource type is "Microsoft.ApiManagement/service/providers/diagnosticSettings" this denotes a sub resource provider used associated with API Management. The resource name is then "[concat(parameters('apimName'),'/microsoft.insights/', parameters('diagnosticSettingName'))]", joining together the specific api instance and the name of the new diagnostic setting.