1

Is this possible to deploy stored procedure or user defined function together with Azure CosmosDB SQL API using bicep?

I know that is possible using directly arm template: https://github.com/Azure/azure-quickstart-templates/blob/master/quickstarts/microsoft.documentdb/cosmosdb-sql-container-sprocs/azuredeploy.json

I have not found any docs regarding doing this in biceps, but I tried doing it on my own. I tried to build bicep resource the same way like any other e.g. container, but I always have exceptions e.g:

Deployment failed. Correlation ID: 736b1c6e-fec7-479c-88e5-5a8034eac762. {
  "status": "Failed",
  "error": {
    "code": "ResourceDeploymentFailure",
    "message": "The resource operation completed with terminal provisioning state 'Failed'.",
    "details": [
      {
        "code": "DeploymentFailed",
        "message": "At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.",
        "details": [
          {
            "code": "Conflict",
            "message": "{\r\n  \"status\": \"Failed\",\r\n  \"error\": {\r\n    \"code\": \"ResourceDeploymentFailure\",\r\n    \"message\": \"The resource operation completed with terminal provisioning state 'Failed'.\",\r\n    \"details\": [\r\n      {\r\n        \"code\": \"NotFound\",\r\n        \"message\": \"Message: {\\\"code\\\":\\\"NotFound\\\",\\\"message\\\":\\\"Message: {\\\\\\\"Errors\\\\\\\":[\\\\\\\"Owner resource does not exist\\\\\\\"]}\\\\r\\\\nActivityId: 4c994fea-249f-4fe2-bdc9-7f9759ef0d15, Request URI: /apps/46a2c1a8-e060-40ad-9893-6f6573d9463d/services/232ea27a-4028-42ee-b997-4cfea450f978/partitions/8c2e10e9-37d3-471a-9f99-be392fa75342/replicas/132659688051702465s, RequestStats: \\\\r\\\\nRequestStartTime: 2021-05-20T13:11:03.8469017Z, RequestEndTime: 2021-05-20T13:11:03.8469017Z,  Number of regions attempted:1\\\\r\\\\nResponseTime: 2021-05-20T13:11:03.8469017Z, StoreResult: StorePhysicalAddress: rntbd://10.0.0.19:11000/apps/46a2c1a8-e060-40ad-9893-6f6573d9463d/services/232ea27a-4028-42ee-b997-4cfea450f978/partitions/8c2e10e9-37d3-471a-9f99-be392fa75342/replicas/132659688051702465s, LSN: 7, GlobalCommittedLsn: 7, PartitionKeyRangeId: , IsValid: True, StatusCode: 404, SubStatusCode: 1003, RequestCharge: 1, ItemLSN: -1, SessionToken: -1#7, UsingLocalLSN: False, TransportException: null, ResourceType: Collection, OperationType: Read\\\\r\\\\nResponseTime: 2021-05-20T13:11:03.8469017Z, StoreResult: StorePhysicalAddress: rntbd://10.0.0.20:11300/apps/46a2c1a8-e060-40ad-9893-6f6573d9463d/services/232ea27a-4028-42ee-b997-4cfea450f978/partitions/8c2e10e9-37d3-471a-9f99-be392fa75342/replicas/132659688051702467s, LSN: 7, GlobalCommittedLsn: 7, PartitionKeyRangeId: , IsValid: True, StatusCode: 404, SubStatusCode: 1003, RequestCharge: 1, ItemLSN: -1, SessionToken: -1#7, UsingLocalLSN: False, TransportException: null, ResourceType: Collection, OperationType: Read\\\\r\\\\n, SDK: Microsoft.Azure.Documents.Common/2.11.0\\\"}, Request URI: /dbs/dbone/colls/collone/udfs, RequestStats: , SDK: Microsoft.Azure.Documents.Common/2.11.0, Microsoft.Azure.Documents.Common/2.11.0, Microsoft.Azure.Documents.Common/2.11.0, Microsoft.Azure.Documents.Common/2.11.0, Microsoft.Azure.Documents.Common/2.11.0\"\r\n      }\r\n    ]\r\n  }\r\n}"
          }
        ]
      }
    ]
  }
}

Here is some snippet from my bicep:

resource databaseAccount 'Microsoft.DocumentDB/databaseAccounts@2021-01-15' = {
  name: accountName
  location: location
  kind: 'GlobalDocumentDB'
  properties: {
    consistencyPolicy: {
      defaultConsistencyLevel: 'Session'
    }
    locations: [
      {
        locationName: location
        failoverPriority: 0
        isZoneRedundant: false
      }
    ]
    databaseAccountOfferType: 'Standard'
    enableAutomaticFailover: false
    enableMultipleWriteLocations: false
    publicNetworkAccess: publicNetworkAccess
  }
}

resource cosmosdb 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2021-01-15' = {
  name: '${databaseAccount.name}/${databaseName}'
  properties: {
    resource: {
      id: databaseName
    }
  }
}

resource containers 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2021-01-15' = {
  name: '${databaseAccount.name}/${databaseName}/${containerName}'
  properties: {
    resource: {
      id: containerName
      partitionKey: {
        paths: [
          '/partitionKey'
        ]
        kind: 'Hash'
      }
    }
    options: {
      throughput: throughput
    }
  }
}

resource userDefinedFunctions 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/userDefinedFunctions@2021-01-15' = {
  name: '${databaseAccount.name}/${databaseName}/${containerName}/${userDefinedFunctionName}'
  properties: {
    resource: {
      id: userDefinedFunctionName
      body: 'function checkTime(ts){var ts_date=new Date(ts); var hour=ts_date.getHours(); return hour == 8;}'
    }
  }
}

When I add last resource userDefinedFunctions it always fails when executing:

az deployment group create -f main.bicep -g myresgroup

I mean it creates db, collection, but without sproc or udfs and with error from above. I am little worried it is not possible yet in bicep.

I tried also many others api versions @api.

UPDATE

I generated json file from bicep and checked that dependsOn property is wrongly generated:

"dependsOn": [
                "[resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('accountName'))]"
              ]

When I changed to:

"dependsOn": [
                "[resourceId('Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers', parameters('accountName'), parameters('databaseName'), parameters('containerName'))]"
              ]

deployment was successful.

Is there way to impact dependsOn property generation?

1 Answer 1

2

I have an example here that I built decompiling the arm template sample you showed into bicep.

https://github.com/markjbrown/azure-quickstart-templates/tree/sql-sp/quickstarts/microsoft.documentdb/cosmosdb-sql-container-sprocs

I have not yet had a chance to complete the PR I have on this updated sample but this will get published where the ARM template you pointed to lives. But if you need an example that works to unblock you this is what you need.

UPDATE: Apologies for the empty bicep file. Not sure how that happened.

To get an example of how to deploy a UDF using bicep you can take the ARM template located here and then pass it to bicep's decompile function. Example below.

bicep decompile "path/to/azuredeploy.json"

The resulting .bicep file can then be deployed.

2
  • main.bicep file is empty
    – gorrch
    May 20, 2021 at 15:12
  • Apologies. Updated my original answer. This should get you what you need.
    – Mark Brown
    May 20, 2021 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.