How to configure diagnostic settings for APIM for all api using Bicep

Viewed 27

I have created a APIM Service, LogAnalyticWorkSpace and AppInsight resource using Bicep. Now I want add diagnostic settings to All API using bicep. I am able to do it at api level, but how do I do it at "ALL API" using bicep.

The code I have used to configure at API level

 resource apiManagementServiceName_api_name 'Microsoft.ApiManagement/service/apis@2021-08-01' = {
  parent: apiManagementServiceName_resource
  name: api_name
  properties: {
    displayName: api_name
    apiRevision: '1'
    subscriptionRequired: true
    path: api_name
    protocols: [
      'https'
    ] 
  }
}

resource apiManagementAPIAppInsights 'Microsoft.ApiManagement/service/apis/diagnostics@2021-08-01'={
  name:'appInsights'
  parent:apiManagementServiceName_api_name
  properties:{
    loggerId:loggerid
    alwaysLog:'allErrors'
    verbosity:'error'
  }
}

I want to configure a setting which on UI is shown here

Azure APIM Setting

1 Answers

We need to use service/loggers and service/diagnostics for this

resource apiManagementLogging 'Microsoft.ApiManagement/service/loggers@2021-08-01'={
  name:'${apiManagementServiceName}/${appInsightName}'
  properties:{
    loggerType:'applicationInsights'
    description:'Logger resources for APIM'
    credentials:{
      instrumentationKey:instrumentationKey   
    }
  }
}
resource apiManagemementDiagnostics 'Microsoft.ApiManagement/service/diagnostics@2021-08-01'={
  name:'applicationinsights'
  parent:apiManagementServiceName_resource
  properties:{
    loggerId:apiManagementLogging.id
    alwaysLog:'allErrors'

  }
  dependsOn:[apiManagementLogging]
}
Related