Coder Perfect

Azure Bicep resource modules have a dependency issue.

Problem

Cosmos account, Cosmos database, and Cosmos containers are the three resources I’m building. Except for the containers, which I modularized, I have them all in one folder.

main.bicep

// Cosmos DB Account
resource cosmos 'Microsoft.DocumentDB/databaseAccounts@2020-06-01-preview' = if (deployDB) {
  name: cosmosAccountName
  location: location
  tags: appTags
  kind: 'GlobalDocumentDB'
  identity: {
    type: 'None'
  }
  properties: {
  ...
  }
  dependsOn: [
    virtualNetwork
  ]
}

// Cosmos SQL Database
resource cosmosdb 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2020-04-01' = if (deployDB) {
  name: '${cosmos.name}/${databaseName}'
  properties: {
    resource: {
      id: databaseName
    }
    options: {
      throughput: 400
    }
  }
}


// Cosmos DB Containers
module cosmosContainersUser '../../../cicd/bicep/modules/datastore/cosmos-db.bicep' = [for container in cosmosContainers: if (deployDB) {
  name: container.containerName
  params: {
    cosmosContainerName: container.name
    id: container.id
    partitionKey: container.partitionKey
  }
}]

cosmos-db.bicep

param cosmosContainerName string
param id string
param partitionKey string

// Cosmos Containers
resource cosmosContainerUser 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2021-06-15' = {
  name: cosmosContainerName
  properties: {
    resource: {
      id: id
      indexingPolicy: {
        indexingMode: 'consistent'
        automatic: true
        includedPaths:[
          {
            path: '/*'
          }
        ]
        excludedPaths: [
          {
            path: '/"_etag"/?'
          }
        ]
      }
      partitionKey: {
        kind: 'Hash'
        paths: [
          partitionKey
        ]
      } 
      conflictResolutionPolicy: {
        mode: 'LastWriterWins'
        conflictResolutionPath: '/_ts'
      }
    }
  }
}

This works OK, but main.bicep is still quite large. I’d like to continuing modularizing it, but I’m having difficulties shifting the other resources to cosmos-db.bicep. If I were to include the Cosmos database resource in the file, it would look like this:

cosmos-db.bicep

param databaseName string
param cosmosDbName string
param cosmosContainerName string
param id string
param partitionKey string

// Cosmos Database
resource cosmosdb 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2020-04-01' = {
  name: cosmosDbName
  properties: {
    resource: {
      id: databaseName
    }
    options: {
      throughput: 400
    }
  }
}

// Cosmos Containers
resource cosmosContainerUser 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2021-06-15' = {
  name: cosmosContainerName
  properties: {
    resource: {
      id: id
      indexingPolicy: {
        indexingMode: 'consistent'
        automatic: true
        includedPaths:[
          {
            path: '/*'
          }
        ]
        excludedPaths: [
          {
            path: '/"_etag"/?'
          }
        ]
      }
      partitionKey: {
        kind: 'Hash'
        paths: [
          partitionKey
        ]
      } 
      conflictResolutionPolicy: {
        mode: 'LastWriterWins'
        conflictResolutionPath: '/_ts'
      }
    }
  }
}

main.bicep

module cosmosdb '../../../cicd/bicep/modules/datastore/cosmos-db.bicep' = {
  name: 'cosmosDbName'
  params: {
    cosmosDbName: 'cosmosDbName'
    databaseName: databaseName
  }
}

In both the cosmosdb and cosmosContainersUser modules of main.bicep, I get a red line under the params keyword. The following necessary properties are missing from the provided “object” declaration for the cosmosdb module: “id”, “partitionKey”, “cosmosContainerName”. The params keyword in the cosmosContainersUser module and the bicep(BCP035) say The following necessary properties are missing from the requested “object” declaration: “cosmosDbName”, “databaseName”.bicep (BCP035).

Because these resources are interdependent, I’m presuming this is a dependency issue. Because modules don’t allow it, I can’t use the parent key word in main.bicep. I tried using dependsOn and adding parent to the cosmosContainerUser in cosmos-db.bicep, but I’m still getting the same errors.

Asked by agw2021

Post is based on https://stackoverflow.com/questions/69139009/dependency-issue-with-azure-bicep-resource-modules