Search code examples
azureazure-logic-appsazure-resource-managerazure-eventgridazure-logic-app-standard

Create an EventGrid subscription to a STANDARD logic app


I mainly want to create a standard logic app that use this trigger: When a resource event occurs:

enter image description here

I want to be able to create this logic app via ARM. I have added templates for logic app, workflows, API connection and event grid subscription. but I get this error:

##[error]Url validation: Webhook validation handshake failed for https://myLogicApp.azurewebsites.net/runtime/webhooks/workflow/scaleUnits/prod-00/workflows/9382f38a3bc54b528e50dcc4351cd665/triggers/When_a_resource_event_occurs/paths/invoke. Http POST request retuned 2XX response with response body . When a validation request is accepted without validation code in the response body, Http GET is expected on the validation url included in the validation event(within 10 minutes).

So, I have tried to create the eventgrid subscription manually in the portal through copying the workflow url manually and pasting it under the webhook endpoint url (That I need to fill to create the subscription manually). However, this didn't work either. I got a very similar error:

enter image description here

I did the previous step just as a debugging step. I know that if I created the trigger in the workflow manually, the problem will be fixed and the subscription will be created alone (or by Azure backend system).

So, is there anyone that has countered this problem with the standard logic app? Do you have any idea to how to solve this problem?

I mean how to create an eventgrid subscription ARM template that connects to a standard logic app??


Solution

  • After communicating with MS support team, I found that first the name of the logic app name shouldn't exceed 43 characters. Then creating this workflow may work in portal but not using ARM. since it will have a validation error because of an internal bug in Microsoft side.

    A workaround to solve this problem is inspired from this page:

    The eventgrid validation request will always have validation Url (only if the LA has been deployed via ARM). Thus we can add these actions: enter image description here

    The http request can then have: enter image description here

    So the workflow will have code that is similar to this:

    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "Condition": {
                    "actions": {
                        "HTTP_try_to_return_the_validation": {
                            "inputs": {
                                "body": "@triggerBody()?['data']?['validationCode']",
                                "method": "POST",
                                "uri": "@{triggerBody()?['data']?['validationUrl']}"
                            },
                            "runAfter": {},
                            "type": "Http"
                        },
                        "Terminate": {
                            "inputs": {
                                "runStatus": "Cancelled"
                            },
                            "runAfter": {
                                "HTTP_try_to_return_the_validation": [
                                    "Succeeded"
                                ]
                            },
                            "type": "Terminate"
                        }
                    },
                    "expression": {
                        "and": [
                            {
                                "equals": [
                                    "@triggerBody()?['eventType']",
                                    "Microsoft.EventGrid.SubscriptionValidationEvent"
                                ]
                            }
                        ]
                    },
                    "runAfter": {},
                    "type": "If"
                }
            },
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "triggers": {
                "When_a_resource_event_occurs": {
                    "inputs": {
                        "body": {
                            "properties": {
                                "destination": {
                                    "endpointType": "webhook",
                                    "properties": {
                                        "endpointUrl": "@{listCallbackUrl()}"
                                    }
                                },
                                "filter": {
                                    "includedEventTypes": [
                                        "X.Y.Z"
                                    ],
                                    "subjectBeginsWith": "/x/y/z"
                                },
                                "topic": "@{appsetting('eventgrid_name')}"
                            }
                        },
                        "host": {
                            "connection": {
                                "referenceName": "eventgrid"
                            }
                        },
                        "path": "/subscriptions/@{encodeURIComponent(appsetting('subscriptionId'))}/providers/@{encodeURIComponent('Microsoft.EventGrid.Topics')}/resource/eventSubscriptions",
                        "queries": {
                            "subscriptionName": "testName",
                            "x-ms-api-version": "2017-09-15-preview"
                        }
                    },
                    "splitOn": "@triggerBody()",
                    "type": "ApiConnectionWebhook"
                }
            }
        },
        "kind": "Stateful"
    }