Automate Registering an Event Grid Subscription for Logic Apps
  • 07 Sep 2020
  • 1 Minute to read
  • Contributors
  • Comments
  • Dark
  • PDF

Automate Registering an Event Grid Subscription for Logic Apps

  • Comments
  • Dark
  • PDF

If I am building a Logic App and want to subscribe for message from an event grid and have the messages forwarded to my Logic App then I can implement a Logic App with an HTTP request trigger and have a subscription setup to forward the events on event grid to the HTTP endpoint by using the push model and webhook endpoint on the subscription.

In this scenario when I am developing and building Azure DevOps pipelines I will want to be able to automate the setup for my Logic Apps to register on the event grid subscription to have messages forwarded to the Logic App.

Fortunately this is not difficult to do with Powershell. Below the script will do the following:

- Look up a logic app in your resource group and get the http trigger url
- Check the event grid to see if a subscription with the name of the logic app already exists
- If the subscription doesnt exist it will be added, and if it does exist it will be updated
- The subscription will filter the message types that are of interest

The powershell to setup a subscription on the event grid topic is below.

$azureSubscriptionId = 'My Azure Subscription ID'
Set-AzContext -Subscription $azureSubscriptionId

$resourceGroupName = 'My Resource Group Name'
$eventGridTopicName = 'My Event Grid Topic'

function RegisterLogicAppAsEventGridSubscription([string] $logicAppName, [string[]] $eventTypes){

    #Get the LogicApp and its url
    $logicApp = Get-AzLogicApp -ResourceGroupName $resourceGroupName -Name $logicAppName
    $logicAppEndpoint = Get-AzLogicAppTriggerCallbackUrl -ResourceGroupName $resourceGroupName -Name $logicAppName -TriggerName 'manual'
    $endPointUrl = $logicAppEndpoint.Value

    Write-Host 'Logic App Url:' $endPointUrl

    #Get the Event Grid and Create Subscription
    $eventGridSubscriptionName = $logicApp.Name

    $eventGridSubscription = Get-AzEventGridSubscription -EventSubscriptionName $eventGridSubscriptionName -ResourceGroupName $resourceGroupName -TopicName $eventGridTopicName

    if($eventGridSubscription -eq $null){

        $eventGridSubscription = New-AzEventGridSubscription -IncludedEventType $eventTypes -EndpointType webhook -Endpoint $endPointUrl -EventSubscriptionName $eventGridSubscriptionName -ResourceGroupName $resourceGroupName -TopicName $eventGridTopicName
        Update-AzEventGridSubscription -IncludedEventType $eventTypes  -EndpointType webhook -Endpoint $endPointUrl  -ResourceGroupName $resourceGroupName -TopicName $eventGridTopicName -EventSubscriptionName $subscriptionName

RegisterLogicAppAsEventGridSubscription -logicAppName 'Receiver' -eventTypes @('Test1','Test2')

It would be very easy to modify the above script and run it in an Azure DevOps pipeline to have subscriptions be created for any messages you want to be forwarded to specific logic apps.

Was this article helpful?