phydeauxman
phydeauxman

Reputation: 1712

How to reference an object in an array of objects using Bicep

I am trying to output the referenceId of each subnet in a module that creates a virtual network with 4 subnets. I can get the first one, [0], but when I try output the others, [1], [2], [3] the deployment fails and throws the error:

The language expression property array index "1" is out of bounds

Below is the code to create the virtualNetwork and the subnets:

resource virtualNetwork 'Microsoft.Network/virtualNetworks@2018-11-01' = {
  name: vNetName
  location: location
  tags: tags
  properties: {
    addressSpace: {
      addressPrefixes: [
        addressPrefix
      ]
    }
    subnets: subnets
  }
}

subnets is a variable of type array:

var subnets = [
  {
    name: mgmtSubnetName
    properties: {
      addressPrefix: mgmtSubnetAddressPrefix
    }
  }
  {
    name: intSubnetName
    properties: {
      addressPrefix: intSubnetAddressPrefix
    }
  }
  {
    name: extSubnetName
    properties: {
      addressPrefix: extSubnetAddressPrefix
    }
  }
  {
    name: vdmsSubnetName
    properties: {
      addressPrefix: vdmsSubnetAddressPrefix
    }
  }
]

When I use the output line below, it returns and array that has 4 objects...one for each subnet created:

output subnets array = virtualNetwork.properties.subnets

Each object has the format below:

{
    "name":"<value>",
    "id":"<value>",
    "etag":"<value>",
    "properties":{
        "provisioningState":"Succeeded",
        "addressPrefix":"<value>",
        "ipConfigurations":[
            {
                "id":"<value>"
            }
        ],
        "delegations":[]
    },
    "type":"Microsoft.Network/virtualNetworks/subnets"
}

When I use the output line below, it returns the first object in the subnets array:

output subnet1 object = virtualNetwork.properties.subnets[0]

When I use the output line below, it returns the resourceId of the first subnet:

output subnet1 string = virtualNetwork.properties.subnets[0].id

I am unable to retrieve the other objects in the array using indexes 1, 2, or 3.

I have also tried the resourceId function (example below) but the behavior is exactly the same for indexes 1, 2, or 3:

output subnet1Id string = resourceId('Microsoft.Network/VirtualNetworks/subnets', name, subnets[0].name)

Upvotes: 4

Views: 9244

Answers (1)

Ansuman Bal
Ansuman Bal

Reputation: 11401

You can use the below bicep template to deploy the vnet and subnets and output the subnets and subnet id's like below :

var subnets = [
  {
    name: 'vm-subnet'
    properties: {
      addressPrefix:'10.0.0.0/24'
    }
  }
  {
    name: 'webapp-subnet'
    properties: {
      addressPrefix:'10.0.1.0/24'
    }
  }
  {
    name: 'appgw-subnet'
    properties: {
      addressPrefix:'10.0.2.0/24'
    }
  }
  {
    name: 'bastion-subnet'
    properties: {
      addressPrefix:'10.0.3.0/24'
    }
  }
]
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2018-11-01' = {
  name: 'ansuman-vnet'
  location: 'east us'
  properties: {
    addressSpace: {
      addressPrefixes: [
        '10.0.0.0/16'
      ]
    }
    subnets: subnets
  }
}

output subnets array = [for (name, i) in subnets :{
  subnets : virtualNetwork.properties.subnets[i]
}]

output subnetids array = [for (name, i) in subnets :{
  subnets : virtualNetwork.properties.subnets[i].id
}]

output subnetappgw string = virtualNetwork.properties.subnets[2].id

output webappsubnet object = virtualNetwork.properties.subnets[1]

Outputs:

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Note: I am using the latest Bicep version i.e. Bicep CLI version 0.4.1124

Upvotes: 6

Related Questions