0
votes

I'm having a strange issue, I have a simple ARM template, to send activity log to log analytics workspace, should be simple enough, my code is as following

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "workspaceName": {
      "type": "String"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Insights/diagnosticSettings",
      "apiVersion": "2017-05-01-preview",
      "name": "send activity log to workspace",
      "dependsOn": [],
      "properties": {
        "workspaceId": "[resourceId('Microsoft.OperationalInsights/workspaces', parameters('workspaceName'))]",
        "logs": [
          {
            "category": "Administrative",
            "enabled": true
          },
          {
            "category": "Alert",
            "enabled": true
          },
          {
            "category": "Autoscale",
            "enabled": true
          },
          {
            "category": "Policy",
            "enabled": true
          },
          {
            "category": "Recommendation",
            "enabled": true
          },
          {
            "category": "ResourceHealth",
            "enabled": true
          },
          {
            "category": "Security",
            "enabled": true
          },
          {
            "category": "ServiceHealth",
            "enabled": true
          }
        ]
      }
    }
  ]
}

but after enter my workspaceName parameter, and run deployment, it keeps complaining workspace can not be found.

"message": "The resource '/subscriptions/0000000-0000-0000-0000-0000000000000/providers/microsoft.operationalinsights/workspaces/cstackhub-mcslog-analytics' doesn't exist."

( I replace actual sub id with 0000) I know the workspace is there, what strange is if I view resource explorer, and view that workspace id, I can see it actually is "/subscriptions/0000000-0000-0000-0000-0000000000000/resourcegroups/cstackhub-sharedsvcs-rg/providers/microsoft.operationalinsights/workspaces/cstackhub-mcslog-analytics"

so seems missing resourcegroups/cstackhub-sharedsvs-rg in the resource ID is the problem, question is why that resource ID function not giving me correct resource ID? I've been using the exact same resouce ID function in other templates without any issues

1

1 Answers

0
votes

By the looks of the message, it appears you are deploying to a subscription (New-AzDeployment or New-AzSubscriptionDeployment) instead of a resource group (New-AzResourceGroupDeployment).

When deploying to a subscription, the function resourceId() will return

/subscriptions/{subscriptionId}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}

When deploying to a resource group, the function returns

/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}

MS Documentation: https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-resource#return-value-6