如何使用Bicep在Function应用程序中组合动态设置和静态设置



我正在尝试使用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  
}
}

问题

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

问题

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

我挣扎了几个星期才找到一个实际的解决方案。我的发现依赖于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;']
}
}

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

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数组上循环并指定名称&值:

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
}]
}
}
}

相关内容

  • 没有找到相关文章

最新更新