Search code examples
azureazure-resource-managerazure-appserviceazure-bicep

Bicep: merge objects to avoid redundancy


I define multiple app services in a bicep script. For each one i use config resource to define the configuration.

resource appSettings1 'Microsoft.Web/sites/config@2022-03-01' = {
  parent: appService1
  name: 'appsettings'
  properties: {
    CONFIG1: value1
    CONFIG2: value2
    COMMON_CONFIG1: commom1
    COMMON_CONFIG2: commom2
  }
}

resource appSettings2 'Microsoft.Web/sites/config@2022-03-01' = {
  parent: appService2
  name: 'appsettings'
  properties: {
    CONFIG3: value3
    CONFIG4: value4
    COMMON_CONFIG1: commom1
    COMMON_CONFIG2: commom2
  }
}

There are a lot of common settings across the app services. Is there a way that im able to define the common settings only once and inject them to every config, so that i dont have to define the same settings in every app service? Somehow like this:

var commomSettings: {
    COMMON_CONFIG1: commom1
    COMMON_CONFIG2: commom2
}

resource appSettings1 'Microsoft.Web/sites/config@2022-03-01' = {
  parent: appService1
  name: 'appsettings'
  properties: {
    CONFIG1: value1
    CONFIG2: value2
    ...commonSettings //somehow inject commonSettings
  }
}

Solution

  • Yes you are on the right track, you can use the union function to merge the objects:

    var commomSettings = {
      COMMON_CONFIG1: commom1
      COMMON_CONFIG2: commom2
    }
    
    resource appSettings1 'Microsoft.Web/sites/config@2022-03-01' = {
      parent: appService1
      name: 'appsettings'
      properties: union(commomSettings, {
          CONFIG1: value1
          CONFIG2: value2
        }
      )
    }