azureazure-resource-managerazure-bicepazure-app-configurationazure-feature-manager

Azure App Configuration - Conditionally create feature flag using bicep


I plan to use the Feature Management functionality within Azure App Configuration service.

Using bicep I create the configuration store.

Using bicep I add a feature flag.

The value should be initially set to false and once available it's up to the business/ops to change the value to true in the Azure Portal.

So I'm looking for a way to conditionally create the feature flag in my bicep; create the feature flag if it not exists. When it already exists, the bicep should skip creation because it could otherwise overwrite/reset the flag value changed by the business/ops.

I found this issue on GitHub from which I conclude that bicep does not yet support this requirement: https://github.com/Azure/bicep/issues/4023

Any suggestions for a workaround?


Solution

  • You can't really do that unless you check manually if the featureFlag exists before running the deployment. ARM templates (and Bicep) try to be idempotent so if you apply the same template multiple times it will reset any manual changes.

    Here is a bicep file that creates a config store and feature flag:

    // main.bicep
    param location string = resourceGroup().location
    param configurationStoreName string
    param featureFlagExists bool
    param featureFlagName string
    
    // Create the configuration store
    resource configurationStore 'Microsoft.AppConfiguration/configurationStores@2021-10-01-preview' = {
      name: configurationStoreName
      location: location
      sku: {
        name: 'free'
      }
      properties: {
        disableLocalAuth: false
        enablePurgeProtection: false
        encryption: {}
        softDeleteRetentionInDays: 0
      }
    }
    
    // Only create the feature flag if not exists
    resource featureFlag 'Microsoft.AppConfiguration/configurationStores/keyValues@2021-10-01-preview' = if (!featureFlagExists) {
      name: '.appconfig.featureflag~2F${featureFlagName}'
      parent: configurationStore
      properties: {
        contentType: 'application/vnd.microsoft.appconfig.ff+json;charset=utf-8'
        tags: {}
        value: '{"id": "${featureFlagName}", "description": "", "enabled": false, "conditions": {"client_filters":[]}}'
      }
    }
    

    And here is a sample powershell script that invoke it:

    1. check if the config store exists
    2. check if the feature flag exists
    3. run the deployment
    $resourceGroupName = "<resource group name>"
    $configurationStoreName = "<config store name>"
    $featureFlagName = "<feature flag name>"
    
    # Check if the app configuration exists
    $appConfigExists = (az appconfig list `
        --resource-group $resourceGroupName `
        --query "[?name=='$configurationStoreName'].id" `
    | ConvertFrom-Json).Length -gt 0
    
    # Check if the feature flag exists
    $featureFlagExists = $false
    if ($appConfigExists) {
      $featureFlagExists = (az appconfig kv list `
          --name $configurationStoreName `
          --query "[?key=='.appconfig.featureflag/$featureFlagName'].key" `
      | ConvertFrom-Json).Length -gt 0
    }
    
    az deployment group create `
      --resource-group $resourceGroupName `
      --template-file .\main.bicep `
      --parameters `
      configurationStoreName=$configurationStoreName `
      featureFlagExists=$featureFlagExists `
      featureFlagName=$featureFlagName