I am trying to deploy FunctionApp with an Event Grid Trigger, upon a blob creation within Storage Account. I can succesfully create FunctionApp, Storage Account, as well as Event Grid Topic, however I am struggling with creation of a Function resource (I need that for the Event Grid Subscription). What could be wrong in the template below?
modules/FunctionApp/Function.bicep
:
@allowed([
'http'
'eventgrid'
])
param triggerType string = 'http'
param functionAppName string
resource functionApp 'Microsoft.Web/sites@2022-09-01' existing = {
name: functionAppName
}
// var functionApphref = 'https://${toLower(functionAppName)}.azurewebsites.net'
var functionName = (triggerType == 'eventgrid') ? 'EventGridTrigger'
: 'HttpTrigger'
resource function2inModule 'Microsoft.Web/sites/functions@2022-09-01' = if(triggerType == 'eventgrid') {
parent: functionApp
name: functionName
// location: location
properties: {
// script_href: '${functionApphref}/admin/vfs/home/site/wwwroot/function_app.py'
// test_data_href: '${functionApphref}/admin/vfs/tmp/FunctionsData/${functionName}.dat'
// href: '${functionApphref}/admin/functions/${functionName}'
// config: {}
language: 'python'
isDisabled: false
}
}
In the above template you can see some lines commented out - my intention (for now) is to skip them and just create a "placeholder function" under Function App for the Event Grid Subscription. (I hope that's possible?). For now, I am planning to deploy application code later with "Deploy to Function App..." in VSCode.
This is the main.bicep
(relevant bits):
// storage account for data
module storageAccount4data 'modules/Storage/StorageAccount.bicep' = {
name: storageAccount4dataName
params: {
location: location
storageAccountName: storageAccount4dataName
}
}
// blob service
module blobServices 'modules/Storage/BlobService.bicep' = {
name: 'default'
params: {
blobServiceName: 'default'
storageAccountName: storageAccount4data.outputs.dtls.name
}
}
// container
module container 'modules/Storage/Container.bicep' = {
name: containerName
params: {
storageAccountName: storageAccount4data.outputs.dtls.name
blobServiceName: blobServices.outputs.name
containerName: containerName
}
}
var functionApp2Name = '${funcAppNamePrefix}-2-preProcAndWriteToDB'
module functionApp2 'modules/FunctionApp/FunctionApp.bicep' = {
name: functionApp2Name
params: {
location: location
functionAppName: functionApp2Name
functionStorageAccountName: storageAccounts4func[1].outputs.dtls.name
// triggerType: 'eventgrid'
}
}
// event grid resources
resource systemtopic 'Microsoft.EventGrid/systemTopics@2023-06-01-preview' = {
name: eventGridTopicName
location: location
identity: {
type: 'SystemAssigned'
}
properties: {
source: storageAccount4data.outputs.dtls.id
topicType: 'Microsoft.Storage.StorageAccounts'
}
}
// the above deploys without any problems
// This is the problematic part:
module function2 'modules/FunctionApp/Function.bicep' = {
name: 'function2Name'
params: {
functionAppName: functionApp2.outputs.functionAppName
triggerType: 'eventgrid'
}
}
// Later aim to create the following resource:
// resource eventsubscription 'Microsoft.EventGrid/systemTopics/eventSubscriptions@2023-06-01-preview' = {
// parent: systemtopic
// name: functionApp2.name
// properties: {
// destination: {
// properties: {
// resourceId: '${functionApp2.outputs.functionAppId}/functions/EventGridTrigger'
// maxEventsPerBatch: 1
// preferredBatchSizeInKilobytes: 64
// }
// endpointType: 'AzureFunction'
// }
// filter: {
// subjectBeginsWith: '/blobServices/default/containers/${containerName}/blobs/'
// subjectEndsWith: '.json'
// includedEventTypes: [
// 'Microsoft.Storage.BlobCreated'
// ]
// enableAdvancedFilteringOnArrays: true
// }
// labels: []
// eventDeliverySchema: 'EventGridSchema'
// retryPolicy: {
// maxDeliveryAttempts: 30
// eventTimeToLiveInMinutes: 1440
// }
// }
// }
The error I get is:
Encountered an error (ServiceUnavailable) from host runtime.
(Target: /subscriptions/{}/resourceGroups/{}/providers/Microsoft.Resources/deployments/function2Name)
Any idea what I am doing wrong?