Deploy an Event Grid Custom topic and an Event Hub handler
    • Dark
      Light
    • PDF

    Deploy an Event Grid Custom topic and an Event Hub handler

    • Dark
      Light
    • PDF

    Article Summary

    #ServerlessTips - Azure Bicep
    Author: Dave Rendon Microsoft MVP

    This article aims to help you deploy an Event Grid custom topic and an EventHub handler in your environment using Infrastructure-as-Code with Azure Bicep.

    Azure Bicep is a domain-specific language (DSL) that uses a declarative syntax to deploy Azure resources.

    The Bicep is an abstraction on top of Azure Resource Manager (ARM) templates to define Azure resources using declarative Infrastructure as Code.

    Prerequisites

    • An Active Azure account: You can create an account for free.
    Azure Bicep is installed on your local machine.
    • Azure PowerShell. See: Install Azure PowerShell.
    • A resource group in your Azure subscription

    Let's get started!

    1. Solution Overview

    We will author a Bicep template creates an Event Grid custom topic and an event hub. It also makes an event subscription that sends events from the custom topic to the event hub.

    The solution will include the following files:

    • 📄 main.bicep: This is the Bicep template with the actual resources
    • 📄 azuredeploy.parameters.json: This parameter file contains the values for deploying your Bicep template.

    2. Azure Bicep Template — parameters

    Please create a new file in your working directory and name it main. Bicep. We will define the following parameters:

    @description('The name of the Event Grid custom topic.')
    param topicName string = 'topic${uniqueString(resourceGroup().id)}'
    
    @description('The name of the Event Grid custom topic\'s subscription.')
    param subscriptionName string = 'subSendToEventHubs'
    
    @description('The name of the Event Hubs namespace.')
    param eventHubNamespace string = 'namespace${uniqueString(resourceGroup().id)}'
    
    @description('The name of the event hub.')
    param eventHubName string = 'eventhub'
    
    @description('The location in which the Event Grid resources should be deployed.')
    param location string = resourceGroup().location
    

    3. Azure Bicep Template — resources

    We will define the following resources:

    resource topic 'Microsoft.EventGrid/topics@2020-06-01' = {
      name: topicName
      location: location
    }
    
    resource eventHubNamespace_resource 'Microsoft.EventHub/namespaces@2018-01-01-preview' = {
      name: eventHubNamespace
      location: location
      sku: {
        name: 'Standard'
      }
      properties: {
        isAutoInflateEnabled: true
        maximumThroughputUnits: 7
      }
    }
    
    resource eventHubNamespace_eventHub 'Microsoft.EventHub/namespaces/EventHubs@2017-04-01' = {
      parent: eventHubNamespace_resource
      name: eventHubName
      properties: {
        messageRetentionInDays: 1
        partitionCount: 2
      }
    }
    
    resource subscription 'Microsoft.EventGrid/eventSubscriptions@2020-06-01' = {
      scope: topic
      name: subscriptionName
      properties: {
        destination: {
          endpointType: 'EventHub'
          properties: {
            resourceId: eventHubNamespace_eventHub.id
          }
        }
        filter: {
          isSubjectCaseSensitive: false
        }
      }
    }
    
    output endpoint string = topic.properties.endpoint
    

    4. Parameters file

    Create a new file named azuredeploy.parameters.json. The code below shows the definition of the parameters file:

    {
        "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "topicName": {
                "value": "azinsider" //your topic name
            }
        }
    }
    

    5. Azure Bicep Template — Deployment

    We will use the command below to deploy our Bicep template:

    $date = Get-Date -Format "MM-dd-yyyy"
    $rand = Get-Random -Maximum 1000
    $deploymentName = "AzInsiderDeployment-"+"$date"+"-"+"$rand"
    
    New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName azinsider_demo -TemplateFile .\main.bicep -TemplateParameterFile .\azuredeploy.parameters.json -c
    

    The image below shows the preview of the deployment:

    Picture1.png

    Then we will execute the deployment. The image below shows the deployment output:

    Picture2.png

    You can verify the deployment using the Azure Portal:

    Picture3.png

    Source Code

    You can find the code of this solution in the following URL. Feel free to contribute!

    https://github.com/daveRendon/azinsider/tree/main/application-workloads/azure-event-grid-custom-topic-and-event-hub-handler


    Was this article helpful?