Error while creating Cosmos DB using Bicep script

236 Views Asked by At

I am creating a template in bicep to create the Azure Cosmos DB for Mongo DB account and the script is throwing the below error when i execute it. Not sure what it is refeering to in the template.

Tried in different ways and removed the capabilities part as well but still facing the same issue. Cosmos DB account is getting created, but when it try to create the database or container it is throwing the error.

Facing the below error: Status Message: Requests for API sql are not supported for this account.

// Create Azure Cosmos DB with Mango DB RU
@description('Azure region of the deployment')
param location string = resourceGroup().location
param CosmosDbOfferType string
param CosmosDbAccountName string
param CosmosAutomaticFailover bool
param VirtualNetworkRules array
param DatabaseName string
param ContainerName string
param AapsSecOmsWorkSpaceId string
param CosmosLocations array
param VirtualNetworkName string 
param defaultConsistencyLevel string

resource CosmosDbAccount 'Microsoft.DocumentDB/databaseAccounts@2022-05-15' = {
  name: CosmosDbAccountName
  location: location
  kind: 'MongoDB'
  properties: {
    databaseAccountOfferType: CosmosDbOfferType
    locations: CosmosLocations
    enableAutomaticFailover: CosmosAutomaticFailover
    isVirtualNetworkFilterEnabled: true
    virtualNetworkRules: [for item in VirtualNetworkRules: {
      id: resourceId(item.resourcegroup, 'Microsoft.Network/virtualNetworks/subnets', item.virtualnetwork, item.subnet)
      ignoreMissingVNetServiceEndpoint: true
      }
    ]
    consistencyPolicy: {
      defaultConsistencyLevel: defaultConsistencyLevel
    }
  }
  identity: {
    type: 'SystemAssigned'
  }
}

// Database
resource database 'Microsoft.DocumentDB/databaseAccounts/sqldatabases@2022-05-15-preview' ={
  name: DatabaseName
  parent: CosmosDbAccount
  properties: {
    resource: {
      id: DatabaseName
    }
  }
}

// Container
resource container 'Microsoft.DocumentDB/databaseAccounts/sqldatabases/containers@2022-05-15-preview' = {
  name: ContainerName
  parent: database
  properties: {
    resource: {
      id: ContainerName
      partitionKey: {
        paths: [
          '/Id'
        ]
        kind: 'Hash'
      }
      indexingPolicy: {
        indexingMode: 'consistent'
        includedPaths: [
          {
            path: '/*'
            indexes: [
              {
                kind: 'Range'
                dataType: 'Number'
                precision: -1
              }
              {
                kind: 'Range'
                dataType: 'String'
                precision: -1
              }
            ]
          }
        ]
      }
    }
    options: {
      autoscaleSettings: {
        maxThroughput: 4000
      }
    }
  }
}

// Diagnostics Logs
resource CosmosDbAccountDiagnostics 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = {
  name: '${CosmosDbAccountName}-Diagnostics'
  scope: CosmosDbAccount
  properties: {
    workspaceId: WorkSpaceId
    logs: [
      {
        category: 'ControlPlaneRequests'
        enabled: true
        retentionPolicy: {
          days: 0
          enabled: false
        }
      }
      {
        category: 'DataPlaneRequests'
        enabled: true
        retentionPolicy: {
          days: 0
          enabled: false
        }
      }
      {
        category: 'MongoRequests'
        enabled: true
        retentionPolicy: {
          days: 0
          enabled: false
        }
      }
      {
        category: 'QueryRuntimeStatistics'
        enabled: true
        retentionPolicy: {
          days: 0
          enabled: false
        }
      }
      {
        category: 'PartitionKeyStatistics'
        enabled: true
        retentionPolicy: {
          days: 0
          enabled: false
        }
      }
    ]
    metrics: [
      {
        category: 'Requests'        
        enabled: true
        retentionPolicy: {
          enabled: false
          days: 0
        }
      }
    ]
  }
}

// Cosmos DB Lock
resource CosmosDbAccountLock 'Microsoft.Authorization/locks@2016-09-01' = {
  name: '${CosmosDbAccountName}Lock'
  scope: CosmosDbAccount
  properties: {
    level: 'CanNotDelete'
  }
}

// Private Endpoint
resource privateEndpoint 'Microsoft.Network/privateEndpoints@2021-02-01' = {
  name: '${CosmosDbAccountName}-privateEndpoint'
  location: location
  properties: {
    subnet: {
      id: resourceId('Microsoft.Network/virtualNetworks/subnets', VirtualNetworkName, 'PrivateEndpointsSubnet')
    }
    privateLinkServiceConnections: [
      {
        name: '${CosmosDbAccountName}-privateLinkServiceConnection'
        properties: {
          privateLinkServiceId: CosmosDbAccount.id
          groupIds: [
            'mongodb'
          ]
        }
      }
    ]
    manualPrivateLinkServiceConnections: []
  }
  dependsOn: [
    CosmosDbAccount
  ]
}

// Private DNS Zone Cosmos DB
resource privateDnsZone 'Microsoft.Network/privateDnsZones@2018-09-01' = {
  name: 'privatelink.documents.azure.com'
  location: 'global'
}

resource privateDnsVirtualNetworkLink 'Microsoft.Network/privateDnsZones/virtualNetworkLinks@2018-09-01' = {
  parent: privateDnsZone
  name: '${VirtualNetworkName}link'
  location: 'global'
  properties: {
    virtualNetwork: {
      id: resourceId('Microsoft.Network/virtualNetworks', VirtualNetworkName)
    }
    registrationEnabled: true
  }
  dependsOn: [
    privateDnsZone
    privateEndpoint
  ]
}
1

There are 1 best solutions below

0
Thomas On

You are trying to create a resource of type sqldatabases which is not supported for account with kind: MongoDB.

You need to use the mongodbDatabases resource type.