Skip to content

Commit becc5c0

Browse files
committed
move cosmosdb database creation back to bicep
1 parent bd11643 commit becc5c0

File tree

3 files changed

+43
-17
lines changed

3 files changed

+43
-17
lines changed

backend/graphrag_app/main.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,17 @@ async def catch_all_exceptions_middleware(request: Request, call_next):
4747

4848

4949
def intialize_cosmosdb_setup():
50-
"""Initialise CosmosDB (if necessary) by setting up a database and containers that are expected at startup time."""
50+
"""Initialise database setup (if necessary) and configure CosmosDB containers that are expected at startup time if they do not exist."""
5151
azure_client_manager = AzureClientManager()
5252
client = azure_client_manager.get_cosmos_client()
5353
db_client = client.create_database_if_not_exists("graphrag")
5454
# create containers with default settings
55-
throughput = ThroughputProperties(
56-
auto_scale_max_throughput=1000, auto_scale_increment_percent=1
57-
)
5855
db_client.create_container_if_not_exists(
59-
id="jobs", partition_key=PartitionKey(path="/id"), offer_throughput=throughput
56+
id="jobs", partition_key=PartitionKey(path="/id")
6057
)
6158
db_client.create_container_if_not_exists(
6259
id="container-store",
6360
partition_key=PartitionKey(path="/id"),
64-
offer_throughput=throughput,
6561
)
6662

6763

infra/core/cosmosdb/cosmosdb.bicep

+21-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ param location string = resourceGroup().location
1010
@allowed(['Enabled', 'Disabled'])
1111
param publicNetworkAccess string = 'Disabled'
1212

13+
var maxThroughput = 1000
14+
1315
resource cosmosDb 'Microsoft.DocumentDB/databaseAccounts@2024-11-15' = {
1416
name: cosmosDbName
1517
location: location
@@ -64,7 +66,25 @@ resource cosmosDb 'Microsoft.DocumentDB/databaseAccounts@2024-11-15' = {
6466
}
6567
networkAclBypassResourceIds: []
6668
capacity: {
67-
totalThroughputLimit: 4000
69+
totalThroughputLimit: maxThroughput
70+
}
71+
}
72+
}
73+
74+
// create a single database that is used to maintain state information for graphrag indexing
75+
// NOTE: The current CosmosDB role assignments are not sufficient to allow the aks workload identity to create databases so we must do it in bicep at deployment time.
76+
// TODO: Identify and assign appropriate RBAC roles that allow the workload identity to create new databases instead of relying on this bicep implementation.
77+
resource graphragDatabase 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2024-11-15' = {
78+
parent: cosmosDb
79+
name: 'graphrag'
80+
properties: {
81+
options: {
82+
autoscaleSettings: {
83+
maxThroughput: maxThroughput
84+
}
85+
}
86+
resource: {
87+
id: 'graphrag'
6888
}
6989
}
7090
}

infra/core/rbac/workload-identity-rbac.bicep

+20-10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ var roleDefinitions = [
3131
{
3232
id: '3913510d-42f4-4e42-8a64-420c390055eb' // Monitoring Metrics Publisher Role
3333
}
34+
{
35+
id: '5bd9cd88-fe45-4216-938b-f97437e15450' // DocumentDB Account Contributor - enables control plane operations
36+
}
3437
]
3538

3639
resource roleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = [
@@ -50,10 +53,24 @@ resource cosmosDb 'Microsoft.DocumentDB/databaseAccounts@2024-12-01-preview' exi
5053
name: cosmosDbName
5154
}
5255

53-
var customRoleName = 'Custom cosmosDB role for graphrag - adds read/write permissions at the database and container level'
56+
// NOTE: The code snippet below is commented out because there is a known race condition issue at deployment time when assigning Cosmos DB built-in roles to an identity.
57+
// For more information: https://github.com/pulumi/pulumi-azure-native/issues/2816
58+
// For a temporary workaround, that seems to work in practice, we can create a custom role defintion with the same permissions as the built-in role and use it instead
59+
// var cosmosDbContainerReadWriteRoleId = '00000000-0000-0000-0000-000000000002'
60+
// resource sqlRoleAssignment 'Microsoft.DocumentDB/databaseAccounts/sqlRoleAssignments@2023-11-15' = {
61+
// name: guid(cosmosDb.id, principalId, principalType, cosmosDbContainerReadWriteRoleId)
62+
// parent: cosmosDb
63+
// properties: {
64+
// principalId: principalId
65+
// roleDefinitionId: '${cosmosDb.id}/sqlRoleDefinitions/${cosmosDbContainerReadWriteRoleId}'
66+
// scope: cosmosDb.id
67+
// }
68+
// }
69+
70+
var customRoleName = 'Custom cosmosDB role for graphrag - adds read/write permissions at the container level'
5471
resource customCosmosRoleDefinition 'Microsoft.DocumentDB/databaseAccounts/sqlRoleDefinitions@2024-12-01-preview' = {
5572
// note: the guid must be globally unique and deterministic (reproducible) across Azure
56-
name: guid(subscription().subscriptionId, resourceGroup().name, cosmosDb.id, customRoleName) // guid is used to ensure uniqueness
73+
name: guid(cosmosDb.id, customRoleName)
5774
parent: cosmosDb
5875
properties: {
5976
roleName: customRoleName
@@ -67,7 +84,6 @@ resource customCosmosRoleDefinition 'Microsoft.DocumentDB/databaseAccounts/sqlRo
6784
'Microsoft.DocumentDB/databaseAccounts/readMetadata'
6885
'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/*'
6986
'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/*'
70-
'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/write'
7187
]
7288
}
7389
]
@@ -76,13 +92,7 @@ resource customCosmosRoleDefinition 'Microsoft.DocumentDB/databaseAccounts/sqlRo
7692

7793
resource assignment 'Microsoft.DocumentDB/databaseAccounts/sqlRoleAssignments@2024-12-01-preview' = {
7894
// note: the guid must be globally unique and deterministic (reproducible) across Azure
79-
name: guid(
80-
subscription().subscriptionId,
81-
resourceGroup().name,
82-
cosmosDb.id,
83-
customCosmosRoleDefinition.id,
84-
principalId
85-
)
95+
name: guid(cosmosDb.id, principalId, principalType, customCosmosRoleDefinition.id)
8696
parent: cosmosDb
8797
properties: {
8898
principalId: principalId

0 commit comments

Comments
 (0)