EventGrid Topic /Event Grid Subscription ARM Template Error

Riaz Ansary 21 Reputation points
2024-10-01T19:24:24.3933333+00:00

I have written a ARM Template script that should deploy a Event Grid Topic and then Event Grid Subscription with endpoint to an azure function. but when you running you get the following error. what am I doing wrong

New-AzSubscriptionDeployment : 3:09:54 PM - Error: Code=InvalidScope; Message=The request contains resources that must be deployed at a resource group scope but a different scope was found. Make sure to specify a resource group scope similar to:

'/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}'

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {},
  "variables": {
    "eventGridTopicName": "azuredevops-eventgridtopic",
    "eventGridSubscriptionName": "azuredevops-autotag",
    "location": "eastus",
   "eventGridSubscriptionUrl": "AzureFUnctionRUL"
  },
  "resources": [
    {
      "type": "Microsoft.EventGrid/topics",
      "apiVersion": "2023-12-15-preview",
      "name": "[variables('eventGridTopicName')]",
      "location": "[variables('location')]",
      "properties": {
                //"source": "/subscriptions/8cb88380-d13a-4940-8e6b-41f90c93a847",
                "inputSchema": "EventGridSchema"
                //"topicType": "Microsoft.Resources.Subscriptions"
            }
    },

   {
      "type": "Microsoft.EventGrid/eventSubscriptions",
      "apiVersion": "2020-06-01",
      "scope": "[format('Microsoft.EventGrid/topics/{0}', variables('eventGridTopicName'))]",
      "name": "[variables('eventGridSubscriptionName')]",
      "properties": {
        "destination": {
          "endpointType": "AzureFunction",
          "properties": {
            "endpointUrl": "[variables('eventGridSubscriptionUrl')]"
          },
          "filter": {
              "includedEventTypes": [ "Microsoft.Resources.ResourceWriteSuccess" ],
              "enableAdvancedFilteringOnArrays": false
      }
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.EventGrid/topics', variables('eventGridTopicName'))]"
      ]
    }
  
  ]
}
Azure Event Grid
Azure Event Grid
An Azure event routing service designed for high availability, consistent performance, and dynamic scale.
390 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. LeelaRajeshSayana-MSFT 15,811 Reputation points Microsoft Employee
    2024-10-01T21:57:51.11+00:00

    Hi @Riaz Ansary Greetings! Thank you for posting this question here.

    As the error message indicates, the ARM template should deploy the resources at the resource group level. However, looks like template is not formatted to target the resource group. When deploying to a subscription, there are different template formats you can follow to deploy resources to:

    • the target subscription from the operation
    • any subscription in the tenant
    • resource groups within the subscription or other subscriptions
    • the tenant for the subscription

    To deploy resources to a resource group within the subscription, add a nested deployment and include the resourceGroup property. In the following example, the nested deployment targets a resource group named demoResourceGroup.

    {
      "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "resources": [
        {
          "type": "Microsoft.Resources/deployments",
          "apiVersion": "2021-04-01",
          "name": "nestedDeployment",
          "resourceGroup": "demoResourceGroup",
          "properties": {
            "mode": "Incremental",
            "template": {
              resource-group-resources
            }
          }
        }
      ],
      "outputs": {}
    }
    
    
    

    Please refer the sample provided in the article section Create resource group and resources to deploy resources to a resource group using a nested template.

    Hope this helps! Please let us know if you have any additional questions or need further clarification.


    If the response helped, please do click Accept Answer and Yes for the answer provided. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.