Arm Template (Bicep): Circular Dependency when merging appsettings (list function)
Asked Answered
A

2

6

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?

Aristarchus answered 30/4, 2021 at 6:11 Comment(1)
T
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.

Thermometer answered 3/5, 2021 at 10:27 Comment(4)
Yes this works, we've found this same solution in the meantime. Thanks anyway!Aristarchus
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.Appendicle
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.Malo
@Malo - start a discussion on github.com/Azure/bicep/discussions and paste your code or sample there.Thermometer
M
1

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)
}

Malo answered 15/2, 2023 at 21:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.