|
| 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