5

I'm trying to update the AppSettings of an App Service through a bicep file.

When doing this in my bicep template:

var currentAppSettings = list('Microsoft.Web/sites/appServiceName/config/appsettings', '2018-11-01')

var newAppSettings = {
  test: 'testValue'
}

var mergedAppSettings = union(currentAppSettings, newAppSettings)

resource appServiceConfig 'Microsoft.Web/sites/config@2018-11-01' = {
  name: 'appServiceName/appSettings'
  properties: mergedAppSettings
}

...I get a circular dependency error when deploying the bicep file:

"Deployment template validation failed: 'Circular dependency detected on resource: '/subscriptions/293d7347-b26f-4413-9986-d61717aaff26/resourceGroups/WSAPlayground/providers/Microsoft.Web/sites/playground-fitxp-backend-euw-wa/config/appSettings'. Please see https://aka.ms/arm-template/#resources for usage details.'."

Is there a way to do this without getting the dependency error?

Peter Wyss
  • 395
  • 2
  • 16

2 Answers2

5

try using modules. Bicep modules are essentially nested deployments. in the top level file (i. e. main) extract the current values and pass them to the submodule (appsettings) as parameter of type object and then execute merge and update. clue is to deploy the update in a different module than reading current value.

Miq
  • 3,931
  • 2
  • 18
  • 32
  • 2
    Yes this works, we've found this same solution in the meantime. Thanks anyway! – Peter Wyss May 04 '21 at 12:44
  • 2
    It doesn't work for me when I have a main.bicep that calls 2 modules. One to create a FunctionApp and the other to add/merge Appsettings. – Oliver Nilsen Jul 19 '22 at 11:59
  • I doesn't work for me either, same scenario: I have a main.bicep that references two modules, one creates the function app, the other the settings. – DaveF Feb 15 '23 at 15:55
  • @DaveF - start a discussion on https://github.com/Azure/bicep/discussions and paste your code or sample there. – Miq Feb 16 '23 at 14:49
0

Using modules doesn't seem to help if the deployment both creates the app service / function app and sets the app settings. This causes a circular dependency error.

Instead, the solution I have ended up using is to move the extraction of the current app settings outside of the bicep template, and pass them in as a parameter. Something like this bash script:

existingAppSettings="{}"
functionAppAlreadyDeployed=$(az appservice plan list --query "[?name=='ic-portfolio-service-preprod-app-plan']" | jq length)

if [functionAppAlreadyDeployed -eq 1]
then
  existingAppSettings=$(az functionapp config appsettings list --name ic-${{parameters.serviceName}}-${{parameters.environment}} --resource-group ${{parameters.serviceName}}-${{parameters.environment}} | jq -r 'map( { (.name): .value } ) | add')
fi

az deployment group create \
--name "deploymentNameGoesHere" \
--resource-group "resourceGroupNameGoesHere" \
--template-file "$(Pipeline.Workspace)/templatepathgoeshere/main.bicep" \
--parameters "buildNumber=$(Build.BuildNumber)" \
  "existingAppSettings=$existingAppSettings" \
--mode Complete

NB - I am using az appservice plan list because appservice doesn't support exists

You can then pass this into the bicep template as an object:

@secure()
param existingAppSettings object

And use as follows:

var appSettings = { 
  New app settings go here
}

resource functionAppAppsettings 'Microsoft.Web/sites/config@2018-11-01' = {
  name: '${functionAppName}/appsettings'
  properties: union(existingAppSettings, appSettings)
}

DaveF
  • 391
  • 2
  • 9