使用 BICEP 我可以通过名称的子字符串找到数组中的特定元素吗?

Using BICEP can I find a specific element in an array by a substring of the name?

我有一个二头肌模块 storageAccounts.bicep,它正在创建多个存储帐户并输出它们的名称数组:

@description('Name of Environment')
param environment string

param storageAccounts array = [
  {
    name : 'api${environment}${uniqueString(resourceGroup().id)}'
    skuName : 'Standard_LRS'
    accessTier : 'Hot'
  }
  {
    name : 'web${environment}${uniqueString(resourceGroup().id)}'
    skuName : 'Standard_LRS'
    accessTier : 'Hot'
  }
]

resource storage_resource 'Microsoft.Storage/storageAccounts@2021-06-01' = [for storage in storageAccounts: {
  name : storage.name
  location : resourceGroup().location
  sku:{
    name : storage.skuName
  }
  kind : 'StorageV2'
  properties:{
    accessTier: storage.accessTier
    allowBlobPublicAccess: true
    minimumTlsVersion: 'TLS1_2'
    supportsHttpsTrafficOnly: true
    encryption:{
      keySource: 'Microsoft.Storage'
      services:{
        blob:{
          keyType: 'Account'
          enabled: true
        }
        file:{
          keyType: 'Account'
          enabled: true
        }
      }
    }    
  }
}]

output storageAccounts array = [for (name, i) in storageAccounts:{
    storageAccountName : name
}]

我将该数组传递到另一个模块以创建一个函数应用程序,并且我正在尝试在该数组中查找特定项目,如下所示:

@description('array of storage account names')
param storageAccounts array

var storageAccountName = storagesAccounts.where(function(storageAccount) {
  return storageAccount.name.startsWith('api')
})


resource storageAccount 'Microsoft.Storage/storageAccounts@2019-04-01' existing = {
  name: storageAccountName
}

这是我能做的吗?或者我最好单独创建存储帐户并直接传递名称?

目前 不支持在 bicep 中查找数组元素。同一个 Github 问题 Issue 1 and Issue 2.

作为解决方法,您可以传递您需要的存储帐户 到模块 然后在模块 中使用它,即 nested template.

我测试了以下 创建两个存储帐户 然后引用 api 第二个模板中的存储帐户 将存储帐户的 ID 存储在密钥库机密中 如下所示:

multiplestorage.bicep:

@description('Name of Environment')
param environment string = 'test'

param storageAccounts array = [
  {
    name : 'api${environment}${uniqueString(resourceGroup().id)}'
    skuName : 'Standard_LRS'
    accessTier : 'Hot'
  }
  {
    name : 'web${environment}${uniqueString(resourceGroup().id)}'
    skuName : 'Standard_LRS'
    accessTier : 'Hot'
  }
]

resource storage_resource 'Microsoft.Storage/storageAccounts@2021-06-01' = [for storage in storageAccounts: {
  name : storage.name
  location : resourceGroup().location
  sku:{
    name : 'Standard_LRS'
  }
  kind : 'StorageV2'
  properties:{
    accessTier: 'Hot'
    allowBlobPublicAccess: true
    minimumTlsVersion: 'TLS1_2'
    supportsHttpsTrafficOnly: true
    encryption:{
      keySource: 'Microsoft.Storage'
      services:{
        blob:{
          keyType: 'Account'
          enabled: true
        }
        file:{
          keyType: 'Account'
          enabled: true
        }
      }
    }    
  }
}]

output storageAccounts array = [for (name, i) in storageAccounts:{
    storageAccountName : name
}]

module connectionString './functionapp.bicep'=[for (name,i) in storageAccounts :if(startsWith(storage_resource[i].name,'api')){
  name: 'functionappNested${i}'
  params: {
    storageAccounts:storage_resource[i].name
}
}]

functionapp.bicep

param storageAccounts string

resource storageAccount 'Microsoft.Storage/storageAccounts@2019-04-01' existing = {
  name: storageAccounts
}

resource keyVaultShared 'Microsoft.KeyVault/vaults@2021-06-01-preview' existing = {
  name: 'keyvaulttest1234ans'
}
resource storageAccountConnectionString 'Microsoft.KeyVault/vaults/secrets@2021-06-01-preview' = {
  parent:keyVaultShared
  name: '${keyVaultShared.name}-test'
  properties:{
    contentType: 'Storage Account Connection String'
    value: storageAccount.id
}
}

输出:

注意:同样你可以传递以web[=79开头的存储帐户名称=]到另一个嵌套模板作为模块作为将它用于另一个资源.