How to stick app config settings to a staging slot via bicep

3.8k Views Asked by At

How to stick app config settings to a slot via bicep?

Here is my bicep file:

var stagingSettings = [  
  {
    name: 'AzureFunctionsJobHost__extensions__durableTask__hubName'
    value: 'staging'
    slotSetting: true
  }
]


resource functionApp 'Microsoft.Web/sites/slots@2018-11-01' = {
  name: name
  kind: kind
  location: location
  properties: {
    clientAffinityEnabled: true
    enabled: true
    httpsOnly: true
    serverFarmId: resourceId('Microsoft.Web/serverfarms', servicePlanName)
    siteConfig: {
      use32BitWorkerProcess : false
      appSettings: stagingSettings 
    }
  }
  identity: {
    type: 'SystemAssigned'
  }
}

On deploying this code, I don't see app config settings stick to a slot:

enter image description here

checkbox is not checked. What am I missing?

3

There are 3 best solutions below

8
On

You need to create a slotConfigNames resource:

Names for connection strings, application settings, and external Azure storage account configuration identifiers to be marked as sticky to the deployment slot and not moved during a swap operation. This is valid for all deployment slots in an app.

Something like that should work:

param functionAppName string

resource functionApp 'Microsoft.Web/sites@2018-11-01' existing = {
  name: functionAppName
}

resource functionApps 'Microsoft.Web/sites/config@2021-03-01' = {
  name: 'slotConfigNames'
  parent: functionApp
  properties: {
    // Sticky app settings
    appSettingNames: [
      'AzureFunctionsJobHost__extensions__durableTask__hubName'
    ]
  }
}
0
On

TL'DR

The Microsoft.Web sites/config 'slotConfigNames' resource has a string array property called appSettingNames. ALL slots' settings are scanned and matched against this array. Matches produce sticky settings.

For example:

  • Production slot settings: { DB: 'prod-db', FOO: 'bar' }
  • Staging slot settings: { DB: 'stg-db', BAZ: 'qux' }
  • SlotConfigNames: [ 'DB', 'BAZ' ]
  • Result: DB sticky in prod and stg slots, BAZ sticky in stg slot

Bicep sample

param location string = resourceGroup().location

resource appServicePlan 'Microsoft.Web/serverfarms@2022-09-01' = {
  // omitted for breveity
}

var webAppProperties = {
  // omitted for brevity
}

// Deployed to the staging slot, and eventually swapped to the production slot, NOT STICKY!
var webAppSettings = {
  DOTNET_ENVIRONMENT: 'Demo'
}

var webAppStickySettings = {
  productionSlot: {
    DB: 'prod-db'
    FOO: 'bar'
  }
  stagingSlot: {
    DB: 'staging-db'
    BAZ: 'qux'
  }
}

// In this example: [ 'FOO', 'DB', 'BAZ' ]
var webAppStickySettingsKeys = [for setting in items(union(webAppStickySettings.productionSlot, webAppStickySettings.stagingSlot)): setting.key]

resource webApp 'Microsoft.Web/sites@2022-09-01' = {
  // Production slot
  name: 'sample-webapp'
  location: location
  properties: webAppProperties
  resource appsettings 'config' = { name: 'appsettings', properties: webAppStickySettings.productionSlot /* Only prod slot sticky settings, other settings will arrive after staging slot swap */ }

  // Staging slot
  resource stagingSlot 'slots' = {
    name: 'staging'
    location: location
    properties: webAppProperties
    resource appsettings 'config' = { name: 'appsettings', properties: union(webAppSettings, webAppStickySettings.stagingSlot) }
  }

  // Sticky settings
  // Note: although this resource is a direct child of the webApp's production slot, this config applies to both the production and staging slots
  // This config simply matches the appSettingNames of the webApp's production slot AND staging slot
  // For all matches, it will mark the appSetting as sticky
  resource slotsettings 'config' = {
    name: 'slotConfigNames'
    properties: {
      appSettingNames: webAppStickySettingsKeys // in this example: [ 'FOO', 'DB', 'BAZ' ]
    }
  }
}

Final Results

Production Slot (Azure Web App Configuration Screenshot)

enter image description here

Staging Slot (Azure Web App Configuration Screenshot)

enter image description here

0
On

This is not currently possible via bicep for staging slots, only production slots. You can update config settings staging slots via YAML, the portal or CLI.