Problem
StorageAccounts.bicep is a bicep module that creates numerous storage accounts and outputs an array of their names:
@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
}]
I’m giving that array to another module to make a function app, and I’m looking for a specific item in the array, like this:
@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
}
Is this something I’m capable of doing? Is it better to create the storage accounts individually and send the name to them directly?
Asked by John McArthur
Solution #1
Currently its not supported to find the element of an array in bicep . There are Github Issues on the same Issue 1 and Issue 2 .
As a workaround you can just pass the storage account which you need to the module and use it then in the module i.e. nested template.
I tried the following to create two storage accounts, then referring to the api storage account in the second template and storing the storage account’s id in a keyvault secret:
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
}
}
Output:
Note: You can provide the storage account name that starts with web to another nested template as a module and use it for another resource in the same way.
Answered by AnsumanBal-MT
Post is based on https://stackoverflow.com/questions/70671540/using-bicep-can-i-find-a-specific-element-in-an-array-by-a-substring-of-the-name