Skip to content

Commit e9651a0

Browse files
committed
add bicep for cosmos db
1 parent 3b8223b commit e9651a0

File tree

3 files changed

+204
-1
lines changed

3 files changed

+204
-1
lines changed

docs/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@
6060
### Azure AI Document Intelligence
6161

6262
- [Azure AI Document Intelligence client library for Python](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/documentintelligence/azure-ai-documentintelligence/README.md)
63+
64+
### Azure Cosmos DB
65+
66+
- [Manage Azure Cosmos DB for NoSQL resources with Bicep > Azure Cosmos DB account with autoscale throughput](https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/manage-with-bicep#azure-cosmos-db-account-with-autoscale-throughput)

infra/main.bicep

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ param location string = resourceGroup().location
88
@description('Specifies the resource tags.')
99
param tags object = {}
1010

11-
// OpenAI
1211
@description('Specifies the name of the Azure OpenAI resource.')
1312
param openAiName string = '${prefix}openai'
1413

@@ -24,6 +23,9 @@ param storageAccountName string = '${prefix}sa'
2423
@description('Specifies the name of the Azure Event Grid resource.')
2524
param eventGridName string = '${prefix}eg'
2625

26+
@description('Specifies the name of the Azure Cosmos DB resource.')
27+
param cosmosDbName string = '${prefix}cosmosdb'
28+
2729
module openAi './modules/openAi.bicep' = {
2830
name: 'openAi'
2931
params: {
@@ -73,6 +75,17 @@ module eventGrid './modules/eventGrid.bicep' = {
7375
}
7476
}
7577

78+
module cosmosDb './modules/cosmosDb.bicep' = {
79+
name: 'cosmosDb'
80+
params: {
81+
name: cosmosDbName
82+
location: location
83+
tags: tags
84+
primaryRegion: location
85+
secondaryRegion: 'japanwest'
86+
}
87+
}
88+
7689
// Output
7790
output cognitiveServicesName string = cognitiveServices.outputs.name
7891
output cognitiveServicesEndpoint string = cognitiveServices.outputs.endpoint

infra/modules/cosmosDb.bicep

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
// Parameters
2+
@description('Specifies the name of the Azure Cosmos DB resource')
3+
param name string
4+
5+
@description('Specifies the primary location of Azure resources.')
6+
param location string = resourceGroup().location
7+
8+
@description('Specifies the resource tags.')
9+
param tags object = {}
10+
11+
@description('Specifies the name of the Azure Cosmos DB account name, max length 44 characters, lowercase.')
12+
param accountName string = toLower('${name}account')
13+
14+
@description('Specifies the name of the Azure Cosmos DB database name')
15+
param databaseName string = toLower('${name}database')
16+
17+
@description('Specifies the name of the Azure Cosmos DB container name')
18+
param containerName string = toLower('${name}container')
19+
20+
@description('The primary region for the Cosmos DB account.')
21+
param primaryRegion string
22+
23+
@description('The secondary region for the Cosmos DB account.')
24+
param secondaryRegion string
25+
26+
@description('The default consistency level of the Cosmos DB account.')
27+
@allowed([
28+
'Eventual'
29+
'ConsistentPrefix'
30+
'Session'
31+
'BoundedStaleness'
32+
'Strong'
33+
])
34+
param defaultConsistencyLevel string = 'Session'
35+
36+
@description('Max stale requests. Required for BoundedStaleness. Valid ranges, Single Region: 10 to 2147483647. Multi Region: 100000 to 2147483647.')
37+
@minValue(10)
38+
@maxValue(2147483647)
39+
param maxStalenessPrefix int = 100000
40+
41+
@description('Max lag time (minutes). Required for BoundedStaleness. Valid ranges, Single Region: 5 to 84600. Multi Region: 300 to 86400.')
42+
@minValue(5)
43+
@maxValue(86400)
44+
param maxIntervalInSeconds int = 300
45+
46+
@description('Enable system managed failover for regions')
47+
param systemManagedFailover bool = true
48+
49+
@description('Maximum autoscale throughput for the container')
50+
@minValue(1000)
51+
@maxValue(1000000)
52+
param autoscaleMaxThroughput int = 1000
53+
54+
var consistencyPolicy = {
55+
Eventual: {
56+
defaultConsistencyLevel: 'Eventual'
57+
}
58+
ConsistentPrefix: {
59+
defaultConsistencyLevel: 'ConsistentPrefix'
60+
}
61+
Session: {
62+
defaultConsistencyLevel: 'Session'
63+
}
64+
BoundedStaleness: {
65+
defaultConsistencyLevel: 'BoundedStaleness'
66+
maxStalenessPrefix: maxStalenessPrefix
67+
maxIntervalInSeconds: maxIntervalInSeconds
68+
}
69+
Strong: {
70+
defaultConsistencyLevel: 'Strong'
71+
}
72+
}
73+
var locations = [
74+
{
75+
locationName: primaryRegion
76+
failoverPriority: 0
77+
isZoneRedundant: false
78+
}
79+
{
80+
locationName: secondaryRegion
81+
failoverPriority: 1
82+
isZoneRedundant: false
83+
}
84+
]
85+
86+
resource account 'Microsoft.DocumentDB/databaseAccounts@2024-02-15-preview' = {
87+
name: accountName
88+
location: location
89+
tags: tags
90+
kind: 'GlobalDocumentDB'
91+
properties: {
92+
consistencyPolicy: consistencyPolicy[defaultConsistencyLevel]
93+
locations: locations
94+
databaseAccountOfferType: 'Standard'
95+
enableAutomaticFailover: systemManagedFailover
96+
}
97+
}
98+
99+
resource database 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2024-02-15-preview' = {
100+
parent: account
101+
name: databaseName
102+
location: location
103+
tags: tags
104+
properties: {
105+
resource: {
106+
id: databaseName
107+
}
108+
}
109+
}
110+
111+
resource container 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2024-02-15-preview' = {
112+
parent: database
113+
name: containerName
114+
location: location
115+
tags: tags
116+
properties: {
117+
resource: {
118+
id: containerName
119+
partitionKey: {
120+
paths: [
121+
'/myPartitionKey'
122+
]
123+
kind: 'Hash'
124+
}
125+
indexingPolicy: {
126+
indexingMode: 'consistent'
127+
includedPaths: [
128+
{
129+
path: '/*'
130+
}
131+
]
132+
excludedPaths: [
133+
{
134+
path: '/myPathToNotIndex/*'
135+
}
136+
{
137+
path: '/_etag/?'
138+
}
139+
]
140+
compositeIndexes: [
141+
[
142+
{
143+
path: '/name'
144+
order: 'ascending'
145+
}
146+
{
147+
path: '/age'
148+
order: 'descending'
149+
}
150+
]
151+
]
152+
spatialIndexes: [
153+
{
154+
path: '/path/to/geojson/property/?'
155+
types: [
156+
'Point'
157+
'Polygon'
158+
'MultiPolygon'
159+
'LineString'
160+
]
161+
}
162+
]
163+
}
164+
defaultTtl: 86400
165+
uniqueKeyPolicy: {
166+
uniqueKeys: [
167+
{
168+
paths: [
169+
'/phoneNumber'
170+
]
171+
}
172+
]
173+
}
174+
}
175+
options: {
176+
autoscaleSettings: {
177+
maxThroughput: autoscaleMaxThroughput
178+
}
179+
}
180+
}
181+
}
182+
183+
// Outputs
184+
output accountId string = account.id
185+
output databaseId string = database.id
186+
output containerId string = container.id

0 commit comments

Comments
 (0)