Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically Build Dependencies #16600

Open
joehubbert opened this issue Mar 10, 2025 · 3 comments
Open

Dynamically Build Dependencies #16600

joehubbert opened this issue Mar 10, 2025 · 3 comments
Labels
enhancement New feature or request Needs: Triage 🔍

Comments

@joehubbert
Copy link

As a Bicep Developer
I want to be able dynamically build dependsOn objects natively in Bicep based on conditions
So that I am able to fully support modulised deployments so I don't have to deploy the whole solution/main.bicep every time

Describe the solution you'd like
My example main.bicep has modules for:

  • appConfigurationStore
  • containerAppManagedEnvironment
  • keyVault
  • postgresqlFlexibleServer
  • storageAccount
  • virtualNetwork

Each of these modules will deploy if a corresponding bool parameter is true e.g. deployAppConfigurationStore.
Some of these conditions are simple and are supported in bicep today.

In the case of containerAppManagedEnvironment I can do this as there are no other dependencies.

  dependsOn: deployVirtualNetwork ? [
    virtualNetwork
  ] : []

In the cases of appConfigurationStore and postgresqlFlexibleServer I have dependencies on virtualNetwork and keyVault. However, that dependency is only valid for a "full deployment". There is a use case where I only want to deploy specific components in the solution such and which components I pick changes each time.

I have asked ChatGPT this question and it suggested this solution:

var appConfigurationStoreConditionalDependsOn = [
  deployVirtualNetwork ? virtualNetwork : null
  deployKeyVault ? keyVault : null
]

var appConfigurationStoreConditionalDependsOnFiltered = [for dependency in appConfigurationStoreConditionalDependsOn: dependency != null ? dependency : []]

module appConfigurationStore 'appConfigurationStore.bicep' = if (deployAppConfigurationStore) {
  dependsOn: appConfigurationStoreConditionalDependsOnFiltered

This does not seem to be supported:
[for dependency in appConfigurationStoreConditionalDependsOn - This expression is being used in the for-expression, which requires a value that can be calculated at the start of the deployment. You are referencing a variable which cannot be calculated at the start ("appConfigurationStoreConditionalDependsOn" -> "virtualNetwork"). Properties of virtualNetwork which can be calculated at the start include "name".

dependency != null ? dependency : []] - The property "dependsOn" expected a value of type "module[] | (resource | module) | resource[]" but the provided value is of type " | module | module | null".

It would be awesome to solve this, there are other pipelines I have that have even more dependencies

@jeskew
Copy link
Member

jeskew commented Mar 10, 2025

You should be able to include conditionally deployed resources in another resource's dependsOn list without an issue -- the backend should filter any non-deployed resources from the list. Did you run into any issues that prompted this approach?

@joehubbert
Copy link
Author

That approach doesn't work for me because I'm using unique deployment names e.g. keyVault-2025-03-11-10-00 and it will search for a deployment that doesn't exist in that scenario

@jeskew
Copy link
Member

jeskew commented Mar 11, 2025

That shouldn't be a problem. The following template deploys without issue, even though the anotherOne module has a dependency on the empty module, which won't be deployed:

param moduleName string = newGuid()

module empty 'empty.bicep' = if (false) {
  name: moduleName
}

module anotherOne 'empty.bicep' = {
  name: uniqueString(moduleName)
  dependsOn: [
    empty
  ]
}

The deployments engine will look at what's in dependsOn, verify that everything in the list corresponds to a resource in the template, and then ignore any dependency on a resource with a false condition. There's a bit more detail on the docs page on the expected behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Needs: Triage 🔍
Projects
Status: Todo
Development

No branches or pull requests

2 participants