Blogs

Azure bicep – Find an item in an array of objects

The process of finding a specific item within an array of objects should be straightforward in any programming language. It was, however, difficult to perform this operation in Bicep until recently. Although the language provided many array functions, the majority of them were fairly simple or dealt with primitive types such as strings and integers.

A common use case is to have a virtual network Bicep module that creates a virtual network with required subnets and outputs an array of objects which contain subnet details, name, and resourceId:

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

In order to obtain the resourceId of a specific subnet from a virtual network output array, we need to use its name. To accomplish this, we can use the newly introduced filter() lambda functions in bicep. This function takes an array as a parameter with an expression (a.k.a. predicate) to which items in the array should satisfy. The result is a new array that has been “filtered”.

outputArray filter(inputArray, lambda expression)

The array of subnet objects needs to be filtered based on the name of the required subnet. The resulting array should contain only one item, which we can access using first(). In the end, the code looks like this:

module aks 'aks.bicep' = {
  name: 'aks'
  scope: rg
  params:{
    location: location
    clusterSubnetId: first(filter(network.outputs.subnets, x => x.name == 'ClusterSubnet')).id
  }
}

[mailpoet_form id="1"]

Other Recent Blogs

Microsoft Teams IP Phones and Intune Enrollment

Microsoft Teams provides a growing portfolio of devices that can be used as desk and conference room phones. These IP phones run on Android 8.x or 9.x and are required to be enrolled in Intune. By default, these devices are enrolled as personal devices, which is not ideal as users should not be able to enrol their own personal Android devices.

Read More »

Level 9, 360 Collins Street, 
Melbourne VIC 3000

Level 2, 24 Campbell St,
Sydney NSW 2000

200 Adelaide St,
Brisbane QLD 4000

191 St Georges Terrace
Perth WA 6000

Level 10, 41 Shortland Street
Auckland

Part of

Arinco trades as Arinco (VIC) Pty Ltd and Arinco (NSW) Pty Ltd. © 2023 All Rights Reserved Arinco™ | Privacy Policy | Sustainability and Our Community
Arinco acknowledges the Traditional Owners of the land on which our offices are situated, and pay our respects to their Elders past, present and emerging.

Get started on the right path to cloud success today. Our Crew are standing by to answer your questions and get you up and running.