1

I aim to create such a bicep file that would deploy a function app with an event trigger in it, however, I'm not sure if it's possible. I've tried googling but Function App deployment via bicep tutorials tend to stop right after the deployment of an empty function app. I need to go a step further and add triggers to it.

Why do I want to deploy at least an empty event trigger via bicep?

The core of the issue is: I would like to perform two stages of CD, where the first one will be all about bicep (preparing all of the resources) and the second one about code deployment.
However, at the first CD stage, the following error occurs:

{"code":"Endpoint validation","message":"Destination endpoint not found. Resource details: resourceId: /subscriptions/.../functionAppNameHere/functions/eventTriggerFuncName. Resource should pre-exist before attempting this operation.}

I assume that EventGrid EventSubscriptions requires prior presence of a resource with the trigger (seems understandable though).

Is it even possible to add an event trigger via bicep so it would move smoothly to event subscription creation? If yes: can you please give me some tips?, if no: do you have any ideas on how to circumvent this?

Here are my bicep files that may come in handy when thinking about the issue:

main.bicep

module azStorageAccount 'modules/azStorageAccount.bicep'=  {
  name: '${deployName}-st'
  params: {
    location: location
    name: '${stNamePrefix}${resourceSuffix}'
  }
}

module azAppInsights 'modules/azAppInsights.bicep' = {
  name: '${deployName}-appi'
  params: {
    location: location
    name: '${appiNamePrefix}${resourceSuffix}'
  }
}

module azHostingPlan 'modules/azHostingPlan.bicep' = {
  name: '${deployName}-asp'
  params: {
    location: location
    name: '${aspNamePrefix}${resourceSuffix}'
  }
}

module azFunctionApp 'modules/azFunctionApp.bicep' = {
  name: '${deployName}-func'
  params: {
    storageId:azStorageAccount.outputs.storageId
    storageName: azStorageAccount.outputs.storageName
    azAppInsightsInstrumentationKey: azAppInsights.outputs.azAppInsightsInstrumentationKey

    location: location
    name: '${funcNamePrefix}${resourceSuffix}'
    serverFarmId: azHostingPlan.outputs.azHostingPlaniD 
    
  }
}

module azEventGrid 'modules/azEventGrid.bicep' = {
  name: '${deployName}-evg'
  params: {
    subscription: subscription
    evgtName: '${evgtNamePrefix}${resourceSuffix}'
    evgsName: '${evgsNamePrefix}${resourceSuffix}'
    resourceId: '/subscriptions/${subscription}/resourceGroups/${resourceGroup}/providers/Microsoft.Web/sites/${funcNamePrefix}${resourceSuffix}/functions/${eventFunction}'
  }
}

modules/azFunctionApp

// params here

resource azFunctionApp 'Microsoft.Web/sites@2021-03-01' = {
  name: name
  kind: kind
  location: location
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    httpsOnly: true
    serverFarmId: serverFarmId

    clientAffinityEnabled: true
    reserved: true
    
    siteConfig: {
      appSettings: [
        {
          name: 'FUNCTIONS_EXTENSION_VERSION'
          value: '~3'
        }
        // more settings here
      ]     
      alwaysOn: false
    }
  }
}

modules/azEventGrid

// params here

resource azEventGridSystemTopic 'Microsoft.EventGrid/systemTopics@2022-06-15' = {
  name: evgtName
  location: 'global'
  tags: {
    // tags
  }
  properties: {
    source: '/subscriptions/${subscription}'
    topicType: 'Microsoft.Resources.Subscriptions'
  }
}
resource azEventGridEventSubscriptions 'Microsoft.EventGrid/systemTopics/eventSubscriptions@2022-06-15' = {
  parent: azEventGridSystemTopic
  name: evgsName
  properties: {
    destination: {
      properties: {
        resourceId: resourceId
        maxEventsPerBatch: 1
        preferredBatchSizeInKilobytes: 64
      }
      endpointType: 'AzureFunction'
    }
    filter: {
      includedEventTypes: [
        'Microsoft.Resources.ResourceWriteSuccess'
        'Microsoft.Resources.ResourceDeleteSuccess'
      ]
      enableAdvancedFilteringOnArrays: true
    }
    labels: []
    eventDeliverySchema: 'EventGridSchema'
    retryPolicy: {
      maxDeliveryAttempts: 30
      eventTimeToLiveInMinutes: 1440
    }
  }
}
Muerte
  • 83
  • 9
  • It is in fact possible. What type of resource/event are triggering your function ? Could you share you existing bicep file ? – Thomas Dec 13 '22 at 05:24
  • That's great to hear. I'm listening on Microsoft.Resources.ResourceWriteSuccess and Microsoft.Resources.ResourceDeleteSuccess. I've just added my bicep files - everything up to subscription creation works so I did not share other modules than the function and eventgrid ones. – Muerte Dec 13 '22 at 08:45
  • Thanks I will try to have a look shortly – Thomas Dec 13 '22 at 09:26

0 Answers0