skip to Main Content

Hi Looking at the final line. Is this an acceptable way of outputting the array ? Or is there a better way ?

@description('This is the location which needs to be provided, this is the Azure region')
param location string = resourceGroup().location

var storageAccountSku = 'standard_LRS'

resource storageAccounts 'Microsoft.Storage/storageAccounts@2022-09-01' = [for i in range(0, 4): {
  name: '${i}mystorage1234'
  location: location
  sku: {
    name: storageAccountSku
  }
  kind: 'StorageV2'
}]

output storageNames array = [for i in range(0,4) : storageAccounts[i].name]

Thank you in advance,

Jason

PS : I tried to look for the answer on Microsoft pages but didn’t see

2

Answers


  1. Yes it’s fine, although here’s how I’d tweak it.

    @description('This is the location which needs to be provided, this is the Azure region')
    param location string = resourceGroup().location
    
    param numberOfStorageAccounts int = 4
    
    var storageAccountSku = 'standard_LRS'
    
    var storageAccountNames = [for i in range(0,numberOfStorageAccounts): 'st${uniqueString(resourceGroup().id, string(i))}']
    
    resource storageAccounts 'Microsoft.Storage/storageAccounts@2022-09-01' = [for st in storageAccountNames: {
      name: st
      location: location
      sku: {
        name: storageAccountSku
      }
      kind: 'StorageV2'
    }]
    
    output resOut array = storageAccountNames
    
    output resOutObj array = [for (st, i) in storageAccountNames: {
      Index: i
      Name: storageAccounts[i].name
      Id: storageAccounts[i].id
    }]
    
    Login or Signup to reply.
  2. Is this an acceptable way of outputting the array? Or is there a better way?

    Yes, it is possible to extract the name property of each object in the storageAccounts array and generate a new array with those values in the way you export the array in the last line.

    I tried with the same bicep code with adding id properties and got excepted results:

    test.bicep

    @description('This is the location which needs to be provided, this is the Azure region')
    param location string = resourceGroup().location
    
    var storageAccountSku = 'standard_LRS'
    
    resource storageAccounts 'Microsoft.Storage/storageAccounts@2022-09-01' = [for i in range(0, 4): {
      name: '${i}venkat123'
      location: location
      sku: {
        name: storageAccountSku
      }
      kind: 'StorageV2'
    }]
    
    output storageNames array = [for i in range(0,4) : {
         Name:storageAccounts[i].name
         Id:storageAccounts[i].id}]
    

    Deployed using below command:

     az deployment group create --resource-group <Your-resource-group> --template-file test.bicep
    

    Output:

    "outputs": {
          "storageNames": {
            "type": "Array",
            "value": [
              {
                "Id": "/subscriptions/subid/resourceGroups/xxxx/providers/Microsoft.Storage/storageAccounts/0venkat123",
                "Name": "0venkat123"
              },
              {
                "Id": "/subscriptions/xxxx/resourceGroups/xxxx/providers/Microsoft.Storage/storageAccounts/1venkat123",
                "Name": "1venkat123"
              },
              {
                "Id": "/subscriptions/xxxxx/resourceGroups/v-venkat-mindtree/providers/Microsoft.Storage/storageAccounts/2venkat123",
                "Name": "2venkat123"
              },
              {
                "Id": "/subscriptions/xxxxx/resourceGroups/xxxx/providers/Microsoft.Storage/storageAccounts/3venkat123",
                "Name": "3venkat123"
              }
            ]
          }
        }
    

    enter image description here

    Portal:
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search