Search code examples
azureazure-bicep

Bicep Region Pairs


For Azure Bicep:

Given an array of region names ['eastus','westeurope','westus'], how can I produce an array of unique region pairs?

i.e.: [{ A:'eastus', B:'westeurope' },{ A:'westeurope', B:'westus' },{ A:'westus', B:'eastus' }]


Solution

  • The range function creates an array of integers that begins at the start index and contains the number of specified elements as detailed in MSDoc.

    Below code generates an array of objects with two properties, A and B.

    A: - Represents the current region in an array, and

    B: - Represents the next region in an array.

    param regionlist array = ['eastus','westeurope','westus']
    var regionpairs = [
      for i in range(0, length(regionlist)): {
        A: regionlist[i]
        B: regionlist[(i + 1) % length(regionlist)]
      }
    ]
    output regionpairs array = regionpairs
    

    Deployment:

    az bicep build --file array.bicep
    az deployment group create --resource-group "xxxx" --template-file array.bicep
    

    enter image description here