1

I am working on automating event grid subscription through ARM templates. We are using EventGridTrigger in Azure Functions V2. I have tried below two approaches and getting the same error for both of the approaches:

{
    "name": "[parameters('topicName')]",
    "type": "Microsoft.EventGrid/topics",
     "location": "[parameters('location')]",
     "apiVersion": "2018-01-01",
     "resources": [
       {
         "apiVersion": "2018-01-01",
         "name": "[concat(parameters('topicName'), '/Microsoft.EventGrid/', parameters('subscriptionName'))]",
         "type": "Microsoft.EventGrid/topics/providers/eventSubscriptions",
         "dependsOn": [
           "[parameters('topicName')]"
         ],
         "properties": {
           "destination": {
             "endpointType": "WebHook",
             "properties": {
               "endpointUrl": "[listsecrets(resourceId(parameters('resourceGroupName'), 'Microsoft.Web/sites/functions', parameters('functionAppName'), 'FunctionName'),'2015-08-01').trigger_url]"
             }
           },
           "filter": {
             "includedEventTypes": [
               "EventType"
             ]
           }
         }
}]}

In the second approach, I used endpointUrl: [concat('https://', parameters('functionAppName'),'.azurewebsites.net/api/FunctionName?code=', listKeys(concat(variables('functionAppId'), '/functions/FunctionName'),'2016-08-01').default)]

Getting below error:

{
    "status": "Failed",
    "error": {
        "code": "ResourceDeploymentFailure",
        "message": "The resource operation completed with terminal provisioning state 'Failed'.",
        "details": [
            {
                "code": "Url validation",
                "message": "The attempt to validate the provided endpoint https://<functionAppName>.azurewebsites.net/api/FunctionName failed. For more details, visit https://aka.ms/esvalidation."
            }
        ]
    }
}

Has anyone done automation using latest SDKs?

Mr Robot
  • 28
  • 4
  • have a look at an answer in the https://stackoverflow.com/questions/60207556/event-subscription-by-arm-template-for-topic-with-endpointtype-as-azurefunction – Roman Kiss Aug 23 '20 at 09:18

1 Answers1

2

You can refer to this sample code published by PG. Adding the template below for quick reference:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "eventGridTopicName": {
            "type": "string",
            "defaultValue": "MyEventGridTopicwithUniqueName",
            "metadata": {
                "description": "The name of the Event Grid custom topic."
            }
        },
        "eventGridSubscriptionName": {
            "type": "string",
            "defaultValue": "MyEventGridEndpointwithUniqueName",
            "metadata": {
                "description": "The name of the Event Grid custom topic's subscription."
            }
        },
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]",
            "metadata": {
                "description": "The location in which the Event Grid resources should be deployed."
            }
        },
        "appName": {
            "type": "string",
            "defaultValue": "MyUniqueAppName-randomuniquevalue",
            "metadata": {
              "description": "The name of the function app that you wish to create."
            }
          },
          "functionName":{
            "type": "string",
            "defaultValue": "EventGridFunction",
            "metadata": {
                "description" : "Function App Name"
            }
          },
          "storageAccountType": {
            "type": "string",
            "defaultValue": "Standard_LRS",
            "allowedValues": ["Standard_LRS", "Standard_GRS", "Standard_RAGRS"],
            "metadata": {
              "description": "Storage Account type"
            }
          },
          "runtime": {
            "type": "string",
            "defaultValue": "dotnet",
            "allowedValues": ["node", "dotnet", "java"],
            "metadata": {
              "description": "The language worker runtime to load in the function app."
            }
          }

    },
    "variables": {
        "functionUrl" : "[concat('https://', variables('FunctionAppName'),'.azurewebsites.net/runtime/webhooks/eventgrid?functionName=', parameters('FunctionName'),'&code=')]",
        "functionAppName": "[parameters('appName')]",
        "hostingPlanName": "[parameters('appName')]",
        "applicationInsightsName": "[parameters('appName')]",
        "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'azfunction')]",
        "storageAccountid": "[concat(resourceGroup().id,'/providers/','Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
        "functionWorkerRuntime": "[parameters('runtime')]"
    },
    "resources": [
        {
            "name": "[parameters('eventGridTopicName')]",
            "type": "Microsoft.EventGrid/topics",
            "location": "[parameters('location')]",
            "apiVersion": "2018-01-01",
            "dependsOn": [
                "[resourceId('Microsoft.Web/sites', variables('functionAppName'))]"
                
            ]
        },
        {
            "name": "[concat(parameters('eventGridTopicName'), '/Microsoft.EventGrid/', parameters('eventGridSubscriptionName'))]",
            "type": "Microsoft.EventGrid/topics/providers/eventSubscriptions",
            "location": "[parameters('location')]",
            "apiVersion": "2018-01-01",
            "properties": {
                "destination": {
                    "endpointType": "WebHook",
                    "properties": {
                        "endpointUrl": "[concat(variables('functionUrl'), listKeys(resourceId('Microsoft.Web/sites/host/', variables('functionAppName'), 'default'),'2016-08-01').systemkeys.eventgrid_extension)]"
                    }
                },
                "filter": {
                    "includedEventTypes": [
                        "All"
                    ]
                }
            },
            "dependsOn": [
                "[parameters('eventGridTopicName')]",
              

            ]
        },
        {
            "type": "Microsoft.Storage/storageAccounts",
            "name": "[variables('storageAccountName')]",
            "apiVersion": "2016-12-01",
            "location": "[parameters('location')]",
            "kind": "Storage",
            "sku": {
              "name": "[parameters('storageAccountType')]"
            }
        },
        {
            "type": "Microsoft.Web/serverfarms",
            "apiVersion": "2018-02-01",
            "name": "[variables('hostingPlanName')]",
            "location": "[parameters('location')]",
            "sku": {
              "name": "Y1",
              "tier": "Dynamic"
            },
            "properties": {
              "name": "[variables('hostingPlanName')]",
              "computeMode": "Dynamic"
            }
        },
        {
            "apiVersion": "2015-08-01",
            "type": "Microsoft.Web/sites",
            "name": "[variables('functionAppName')]",
            "location": "[parameters('location')]",
            "kind": "functionapp",
            "dependsOn": [
              "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
              "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
            ],
            "properties": {
              "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
              "siteConfig": {
                "appSettings": [
                  {
                    "name": "AzureWebJobsDashboard",
                    "value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';AccountKey=', listKeys(variables('storageAccountid'),'2015-05-01-preview').key1)]"
                  },
                  {
                    "name": "AzureWebJobsStorage",
                    "value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';AccountKey=', listKeys(variables('storageAccountid'),'2015-05-01-preview').key1)]"
                  },
                  {
                    "name": "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING",
                    "value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';AccountKey=', listKeys(variables('storageAccountid'),'2015-05-01-preview').key1)]"
                  },
                  {
                    "name": "WEBSITE_CONTENTSHARE",
                    "value": "[toLower(variables('functionAppName'))]"
                  },
                  {
                    "name": "FUNCTIONS_EXTENSION_VERSION",
                    "value": "~2"
                  },
                  {
                    "name": "WEBSITE_NODE_DEFAULT_VERSION",
                    "value": "8.11.1"
                  },
                  {
                    "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
                    "value": "[reference(resourceId('microsoft.insights/components/', variables('applicationInsightsName')), '2015-05-01').InstrumentationKey]"
                  },
                  {
                    "name": "FUNCTIONS_WORKER_RUNTIME",
                    "value": "[variables('functionWorkerRuntime')]"
                  },
                  {
                      "name": "WEBSITE_RUN_FROM_PACKAGE",
                      "value": "https://dontdeletemestorage.blob.core.windows.net/package/function.zip"
                  }
                ]
              }
            }
        },
        {
            "apiVersion": "2018-05-01-preview",
            "name": "[variables('applicationInsightsName')]",
            "type": "microsoft.insights/components",
            "location": "East US",
            "tags": {
              "[concat('hidden-link:', resourceGroup().id, '/providers/Microsoft.Web/sites/', variables('applicationInsightsName'))]": "Resource"
        },
            "properties": {
              "ApplicationId": "[variables('applicationInsightsName')]",
              "Request_Source": "IbizaWebAppExtensionCreate"
            }
        }

    ],
    "outputs": {
        "return": {
            "type": "string",
            "value": "[concat(variables('functionUrl'), listKeys(resourceId('Microsoft.Web/sites/host/', variables('functionAppName'), 'default'),'2016-08-01').systemkeys.eventgrid_extension)]"
        }
    }
}
Harshita Singh
  • 4,590
  • 1
  • 10
  • 13