如何使用 Bicep 在 Function App 中组合动态设置和静态设置?

How can I combine dynamic settings and static settings in a Function App using Bicep?

我正在尝试使用 Bicep.

Function App 定义设置(静态和动态)

下面是生成Function App本身的模块。该模块包含一个创建动态设置集合的循环(有效):

param serverFarmId string
param availableHubs array

resource functionAppProdSlotResource 'Microsoft.Web/sites@2021-03-01' = {
  name:'function-app-name'
  location: 'Canada Central'
  kind: 'functionapp,linux'
  identity: {
     type: 'SystemAssigned'
  }
  properties: {
    serverFarmId: serverFarmId
    httpsOnly: true
    siteConfig: {
      linuxFxVersion: 'dotnet|3.1'
      appSettings: [for (availableHubId, hubIndex) in availableHubs: {
        'name': 'AvailableHubsConfiguration__Hub__${hubIndex}'
        'value': 'Endpoint=https://hub-${availableHubId}.service.signalr.net;AccessKey=${listKeys(resourceId('Microsoft.SignalRService/SignalR', 'hub-${availableHubId}'), providers('Microsoft.SignalRService', 'SignalR').apiVersions[0]).primaryKey};Version=1.0;'
      }]
    }
  }
}

但是我还有另一个模块,它为相同的功能定义了静态设置:

param functionIdentity object = {
  prodSlotName: ''
  stagingSlotName: ''
}
@secure()
param systemStorageAccountConnectionString string
param applicationInsightsInstrumentationKey string
param stsDiscoveryEndpoint string
param accountsManagerEndpoint string
param azureActiveDirectory object
param updateManagerClientIdKeyVaultUrl string
param updateManagerClientSecretKeyVaultUrl string

var functionExtensionVersion = '~4'
var functionWorkerRuntime = 'dotnet-isolated'

resource prodSlotAppSettingsResource 'Microsoft.Web/sites/config@2021-03-01' = {
  name: '${functionIdentity.prodSlotName}/appsettings'
  properties: {
    AzureWebJobsStorage: systemStorageAccountConnectionString
    FUNCTIONS_EXTENSION_VERSION: functionExtensionVersion
    FUNCTIONS_WORKER_RUNTIME: functionWorkerRuntime
    APPINSIGHTS_INSTRUMENTATIONKEY: applicationInsightsInstrumentationKey
    StsConfiguration__DiscoveryEndpoints__0: stsDiscoveryEndpoint
    AccountsManagerConfiguration__Endpoint: accountsManagerEndpoint
    AzureActiveDirectoryConfiguration__ClientId: '@Microsoft.KeyVault(SecretUri=${updateManagerClientIdKeyVaultUrl})'
    AzureActiveDirectoryConfiguration__ClientSecret: '@Microsoft.KeyVault(SecretUri=${updateManagerClientSecretKeyVaultUrl})' 
    AzureActiveDirectoryConfiguration__Scope: azureActiveDirectory.scope
    AzureActiveDirectoryConfiguration__TokenEndpoint: azureActiveDirectory.tokenEndpoint
    AzureActiveDirectoryConfiguration__TenantId: subscription().tenantId
    AzureActiveDirectoryConfiguration__SubscriptionId: subscription().subscriptionId  
  }
}

问题

问题是第二个模块覆盖了第一个模块设置的动态设置。由于第一个模块中的循环,我找不到一种方法来防止第二个模块覆盖或以某种方式将两者结合起来。

问题

如何使用 Bicep 在 Function App 中组合动态设置和静态设置?

我努力了几个星期才找到一个实际的解决方案。我发现的内容依赖于 concat 函数。

我删除了第二个模块(用于定义函数设置的资源),只保留了第一个(它具有函数资源本身以及动态设置)。

我现在用我构建的数组调用第一个模块:

module functionAppModule 'modules/function-app.bicep' = {
  name: 'Function_App'
  dependsOn: [
    serverFarmModule
  ]
  params: {
    [...]
    availableHubs: [for availableHub in availableHubs: 'Endpoint=https://hub-${environment}-${availableHub}.service.signalr.net;AccessKey=${listKeys(resourceId('Microsoft.SignalRService/SignalR', 'hub-${environment}-${availableHub}'), providers('Microsoft.SignalRService', 'SignalR').apiVersions[0]).primaryKey};Version=1.0;']
  }
}

在该模块中,我构建了 2 个不同的数组:一个具有静态设置,另一个具有动态设置:

var staticSettings = [
  {
    name: 'FUNCTIONS_EXTENSION_VERSION'
    value: '~4'
  }
  {
    name: 'FUNCTIONS_WORKER_RUNTIME'
    value: 'dotnet-isolated'
  }
  {
    name: 'AzureWebJobsStorage'
    value: systemStorageAccountConnectionString
  }
  {
    name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
    value: applicationInsightsInstrumentationKey
  }
  [...]
]

var dynamicSettings = [for (availableHub, hubIndex) in availableHubs: {
  
  name: 'AvailableHubsConfiguration__Hub__${hubIndex}'
  value: availableHub
}]

然后,使用 concat 函数,我将数组合并在一起:

var functionSettings = concat(staticSettings, dynamicSettings)

最后,我可以通过循环并指定名称和值将合并的 functionSettings 数组分配给 Function App:

resource functionAppProdSlotResource 'Microsoft.Web/sites@2021-03-01' = {
  name: 'function-app-name'
  location: 'Canada Central'
  kind: 'functionapp,linux'
  identity: {
     type: 'SystemAssigned'
  }
  properties: {
    serverFarmId: serverFarmId
    httpsOnly: true
    siteConfig: {
      linuxFxVersion: 'dotnet|3.1'
      appSettings: [for setting in functionSettings: {
        name: setting.name
        value: setting.value
      }]
    }
  }
}