Easy handling of optional Azure Web App settings in Bicep

Easy handling of optional Azure Web App settings in Bicep

Easy handling of optional Azure Web App settings in Bicep

Often you don't need all the configurations of an app per stage; sometimes they are optional. This is often the case for the development stage in particular.

Bicep supports conditions by default, but in many examples they are not really well described or even readable. Here is a snippet of how I usually do it:

// sample param, empty string as fallback
param corsOption string = ''

// required web app settings
var requiredWebAppSettings  = [
  {
    name: 'CosmosDB__Endpoint'
    value: cosmosAccount.outputs.endpoint
  }
  {
    name: 'CosmosDB__Database'
    value: db.outputs.name
  }
]

// optional entry for cors

var optionalWebAppCorsConfig = corsOption != '' ? [
  {
    name: 'CorsOptions__Origins'
    value: corsOption
  }
] : []


// create web app config
module backendApp './modules/app-service-webapp-linux.bicep' = {
  name: 'deploy-${backendAppName}'
  params: {
    name: backendAppName
    location: location
    environmentVariables: concat(appApiEnvVarsCollection, appApiCorsOptionsOriginsSection)
  }
}

Thanks to concat we have an array with mandatory variables that must always be set and an array with optional cors settings that are only set if the corsOption parameter is not empty. This is now very easy to control, for example with variables in Azure DevOps.