How do i format the endpoint URL? - Trigger Azure functions using event subscriptions using bicep

Adam Evans 20 Reputation points
2024-12-03T07:46:49.2433333+00:00

Hi,

I have followed this approach and have had success setting this up via the portal. However, when attempting the same using Bicep, the failure is at the point of creating the Event Grid subscription.
I receive a generic error but believe its to do with the endpointUrl:

{"code":"DeploymentFailed","target":"/subscriptions/xyz/resourceGroups/rg1/providers/Microsoft.Resources/deployments/blob-event-subscription","message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-deployment-operations for usage details.","details":[{"code":"BadRequest","message":""}]}}

Here is an example of the eventSubscription bicep code.

resource eventSubscription 'Microsoft.EventGrid/systemTopics/eventSubscriptions@2023-12-15-preview' = {
  parent: systemTopic
  name: eventSubName
  properties: {
    eventDeliverySchema:'EventGridSchema'
    destination: {
      properties: {
        endpointUrl: endpoint
      }
      endpointType: 'WebHook'
    }
    filter: {
      includedEventTypes: [
        'Microsoft.Storage.BlobCreated'
      ]
    }
  }
}

The format of my endpoint URL is as follows:

https://<FUNCTION_APP_NAME>.azurewebsites.net/runtime/webhooks/blobs?functionName=Host.Functions.Function1&code=<BLOB_EXTENSION_KEY>

As mentioned above, this works if setting it up via azure portal, and I require guidance when it comes to the creating the subscription using bicep.

Kind regards,

Adam Evans

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,999 questions
Azure Event Grid
Azure Event Grid
An Azure event routing service designed for high availability, consistent performance, and dynamic scale.
406 questions
{count} votes

Accepted answer
  1. JananiRamesh-MSFT 29,191 Reputation points
    2024-12-03T17:28:38.96+00:00

    @Adam Evans could you please try the below Bicep template for creating an Azure Function with an Event Grid Blob Trigger

    please refer: https://learn.microsoft.com/en-us/azure/templates/microsoft.eventgrid/eventsubscriptions?pivots=deployment-language-bicep

    param storageAccountName string
    param functionAppName string
    param location string = resourceGroup().location
    
    resource storageAccount 'Microsoft.Storage/storageAccounts@2021-04-01' = {
      name: storageAccountName
      location: location
      sku: {
        name: 'Standard_LRS'
      }
      kind: 'StorageV2'
    }
    
    resource functionApp 'Microsoft.Web/sites@2021-02-01' = {
      name: functionAppName
      location: location
      kind: 'functionapp'
      properties: {
        serverFarmId: 'YOUR_APP_SERVICE_PLAN_ID'
        siteConfig: {
          appSettings: [
            {
              name: 'AzureWebJobsStorage'
              value: storageAccount.properties.primaryEndpoints.blob
            }
            {
              name: 'FUNCTIONS_EXTENSION_VERSION'
              value: '~3'
            }
            {
              name: 'WEBSITE_NODE_DEFAULT_VERSION'
              value: '~14'
            }
          ]
        }
      }
    }
    
    resource eventGridSubscription 'Microsoft.EventGrid/eventSubscriptions@2021-06-01-preview' = {
      name: 'blob-created-subscription'
      scope: storageAccount
      properties: {
        destination: {
          endpointType: 'AzureFunction'
          properties: {
            resourceId: functionApp.id
          }
        }
        filter: {
          includedEventTypes: [
            'Microsoft.Storage.BlobCreated'
          ]
        }
      }
    }
    
    
    

    This template creates a storage account, a function app, and an Event Grid subscription that triggers the function app when a blob is created in the storage account. You will need to replace 'YOUR_APP_SERVICE_PLAN_ID' with the ID of your App Service plan.

    do let me know incase of further queries, I would be happy to assist you.


0 additional answers

Sort by: Most helpful

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.