1+ /* ***************************************************************
2+ Azure Cosmos DB + Azure OpenAI Python developer guide lab
3+ ******************************************************************
4+ This Azure resource deployment template uses some of the following practices:
5+ - [Abbrevation examples for Azure resources](https://learn.microsoft.com/en-us/azure/cloud-adoption-framework/ready/azure-best-practices/resource-abbreviations)
6+ */
7+
8+
9+
10+ /* *************************************************************** */
11+ /* Parameters */
12+ /* *************************************************************** */
13+
114@description ('Location where all resources will be deployed. This value defaults to the **East US** region.' )
215@allowed ([
316 'eastus'
@@ -18,6 +31,13 @@ The name defaults to a unique string generated from the resource group identifie
1831@maxLength (15 )
1932param name string = uniqueString (resourceGroup ().id )
2033
34+ @description ('Specifies the SKU for the Azure App Service plan. Defaults to **B1**' )
35+ @allowed ([
36+ 'B1'
37+ 'S1'
38+ ])
39+ param appServiceSku string = 'B1'
40+
2141@description ('Specifies the SKU for the Azure OpenAI resource. Defaults to **S0**' )
2242@allowed ([
2343 'S0'
@@ -33,6 +53,20 @@ param mongoDbUserName string
3353@secure ()
3454param mongoDbPassword string
3555
56+
57+
58+ /*
59+ @description('Git repository URL for the application source. This defaults to the [`solliancenet/cosmos-db-openai-python-dev-guide-labs`](https://github.com/solliancenet/cosmos-db-openai-python-dev-guide-labs.git) repository.')
60+ param appGitRepository string = 'https://github.com/solliancenet/cosmos-db-openai-python-dev-guide-labs.git'
61+
62+ @description('Git repository branch for the application source. This defaults to the [**main** branch of the `solliancenet/cosmos-db-openai-python-dev-guide-labs`](https://github.com/solliancenet/cosmos-db-openai-python-dev-guide-labs/tree/main) repository.')
63+ param appGetRepositoryBranch string = 'main'
64+ */
65+
66+ /* *************************************************************** */
67+ /* Variables */
68+ /* *************************************************************** */
69+
3670var openAiSettings = {
3771 name : '${name }-openai'
3872 sku : openAiSku
@@ -58,8 +92,42 @@ var mongovCoreSettings = {
5892 mongoClusterName : '${name }-mongo'
5993 mongoClusterLogin : mongoDbUserName
6094 mongoClusterPassword : mongoDbPassword
95+
96+ mongoDatabaseName : 'cosmic_works'
97+ mongoCollectionNames : 'products,customers,sales'
6198}
6299
100+ var appServiceSettings = {
101+ plan : {
102+ name : '${name }-web'
103+ sku : appServiceSku
104+ }
105+ web : {
106+ name : '${name }-web'
107+ /*
108+ git: {
109+ repo: appGitRepository
110+ branch: appGetRepositoryBranch
111+ }
112+ */
113+ }
114+ api : {
115+ name : '${name }-api'
116+ /*
117+ git: {
118+ repo: appGitRepository
119+ branch: appGetRepositoryBranch
120+ }
121+ */
122+ }
123+ }
124+
125+
126+
127+ /* *************************************************************** */
128+ /* Azure Cosmos DB for MongoDB vCore */
129+ /* *************************************************************** */
130+
63131resource mongoCluster 'Microsoft.DocumentDB/mongoClusters@2023-03-01-preview' = {
64132 name : mongovCoreSettings .mongoClusterName
65133 location : location
@@ -97,6 +165,11 @@ resource mongoFirewallRulesAllowAll 'Microsoft.DocumentDB/mongoClusters/firewall
97165 }
98166}
99167
168+
169+ /* *************************************************************** */
170+ /* Azure OpenAI */
171+ /* *************************************************************** */
172+
100173resource openAiAccount 'Microsoft.CognitiveServices/accounts@2022-12-01' = {
101174 name : openAiSettings .name
102175 location : location
@@ -142,3 +215,184 @@ resource openAiCompletionsModelDeployment 'Microsoft.CognitiveServices/accounts/
142215 openAiEmbeddingsModelDeployment
143216 ]
144217}
218+
219+
220+
221+ /* *************************************************************** */
222+ /* App Plan Hosting - Azure App Service Plan */
223+ /* *************************************************************** */
224+
225+ resource appServicePlan 'Microsoft.Web/serverfarms@2022-03-01' = {
226+ name : '${appServiceSettings .plan .name }-asp'
227+ location : location
228+ sku : {
229+ name : appServiceSettings .plan .sku
230+ }
231+ kind : 'linux'
232+ properties : {
233+ reserved : true
234+ }
235+ }
236+
237+
238+ /* *************************************************************** */
239+ /* App Hosting - Azure App Service */
240+ /* *************************************************************** */
241+
242+ resource appServiceWeb 'Microsoft.Web/sites@2022-03-01' = {
243+ name : appServiceSettings .web .name
244+ location : location
245+ properties : {
246+ serverFarmId : appServicePlan .id
247+ httpsOnly : true
248+ siteConfig : {
249+ linuxFxVersion : 'NODE|20-lts'
250+ }
251+ }
252+ }
253+
254+ resource appServiceWebSettings 'Microsoft.Web/sites/config@2022-03-01' = {
255+ parent : appServiceWeb
256+ name : 'appsettings'
257+ kind : 'string'
258+ properties : {
259+ APPINSIGHTS_INSTRUMENTATIONKEY : appServiceWebInsights .properties .InstrumentationKey
260+ API_ENDPOINT : 'https://${appServiceFunction .properties .defaultHostName }'
261+ }
262+ }
263+
264+ resource appServiceWebConnectionStrings 'Microsoft.Web/sites/config@2022-03-01' = {
265+ parent : appServiceWeb
266+ name : 'connectionstrings'
267+ kind : 'string'
268+ properties : {
269+ MONGODB__CONNECTION : {
270+ value : 'mongodb+srv://${mongovCoreSettings .mongoClusterLogin }:${mongovCoreSettings .mongoClusterPassword }@${mongovCoreSettings .mongoClusterName }.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000'
271+ type : 'Custom'
272+ }
273+ }
274+ }
275+
276+ resource appServiceWebInsights 'Microsoft.Insights/components@2020-02-02' = {
277+ name : '${appServiceSettings .web .name }-appi'
278+ location : location
279+ kind : 'web'
280+ properties : {
281+ Application_Type : 'web'
282+ }
283+ }
284+
285+ /*
286+ resource appServiceWebDeployment 'Microsoft.Web/sites/sourcecontrols@2021-03-01' = {
287+ parent: appServiceWeb
288+ name: 'web'
289+ properties: {
290+ repoUrl: appServiceSettings.web.git.repo
291+ branch: appServiceSettings.web.git.branch
292+ isManualIntegration: true
293+ }
294+ dependsOn: [
295+ appServiceWebSettings
296+ ]
297+ }
298+ */
299+
300+ /* *************************************************************** */
301+ /* API Hosting - Azure Functions */
302+ /* *************************************************************** */
303+
304+ resource storageAccount 'Microsoft.Storage/storageAccounts@2021-09-01' = {
305+ name : '${replace (toLower (appServiceSettings .api .name ), '-' , '' )}funcst'
306+ location : location
307+ kind : 'Storage'
308+ sku : {
309+ name : 'Standard_LRS'
310+ }
311+ }
312+
313+ resource appServiceFunction 'Microsoft.Web/sites@2022-03-01' = {
314+ name : '${appServiceSettings .api .name }-func'
315+ location : location
316+ kind : 'functionapp'
317+ properties : {
318+ serverFarmId : appServicePlan .id
319+ httpsOnly : true
320+ siteConfig : {
321+ pythonVersion : '3.11'
322+ alwaysOn : true
323+ cors : {
324+ allowedOrigins : [
325+ 'https://${appServiceWeb .properties .defaultHostName }'
326+ ]
327+ }
328+ }
329+ }
330+ dependsOn : [
331+ storageAccount
332+ ]
333+ }
334+
335+ resource appServiceFunctionSettings 'Microsoft.Web/sites/config@2022-03-01' = {
336+ parent : appServiceFunction
337+ name : 'appsettings'
338+ kind : 'string'
339+ properties : {
340+ AzureWebJobsStorage : 'DefaultEndpointsProtocol=https;AccountName=${name }fnstorage;EndpointSuffix=core.windows.net;AccountKey=${storageAccount .listKeys ().keys [0 ].value }'
341+ APPLICATIONINSIGHTS_CONNECTION_STRING : appServiceFunctionsInsights .properties .ConnectionString
342+ FUNCTIONS_EXTENSION_VERSION : '~4'
343+ FUNCTIONS_WORKER_RUNTIME : 'python'
344+ OPENAI__ENDPOINT : openAiAccount .properties .endpoint
345+ OPENAI__KEY : openAiAccount .listKeys ().key1
346+ OPENAI__EMBEDDINGSDEPLOYMENT : openAiEmbeddingsModelDeployment .name
347+ OPENAI__MAXTOKENS : '8191'
348+ MONGODB__DATABASENAME : mongovCoreSettings .mongoDatabaseName
349+ MONGODB__COLLECTIONNAMES : mongovCoreSettings .mongoCollectionNames
350+ }
351+ }
352+
353+ resource appServiceFunctionConnectionStrings 'Microsoft.Web/sites/config@2022-03-01' = {
354+ parent : appServiceFunction
355+ name : 'connectionstrings'
356+ kind : 'string'
357+ properties : {
358+ MONGODB__CONNECTION : {
359+ value : 'mongodb+srv://${mongovCoreSettings .mongoClusterLogin }:${mongovCoreSettings .mongoClusterPassword }@${mongovCoreSettings .mongoClusterName }.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000'
360+ type : 'Custom'
361+ }
362+ }
363+ }
364+
365+ resource appServiceFunctionsInsights 'Microsoft.Insights/components@2020-02-02' = {
366+ name : '${appServiceSettings .api .name }-appi'
367+ location : location
368+ kind : 'web'
369+ properties : {
370+ Application_Type : 'web'
371+ }
372+ }
373+
374+ /*
375+ resource appServiceFunctionsDeployment 'Microsoft.Web/sites/sourcecontrols@2021-03-01' = {
376+ parent: appServiceFunction
377+ name: 'web'
378+ properties: {
379+ repoUrl: appServiceSettings.web.git.repo
380+ branch: appServiceSettings.web.git.branch
381+ isManualIntegration: true
382+ }
383+ dependsOn: [
384+ appServiceFunctionSettings
385+ ]
386+ }
387+ */
388+
389+
390+
391+
392+ /* *************************************************************** */
393+ /* Outputs */
394+ /* *************************************************************** */
395+
396+ output deployedWebUrl string = appServiceWeb .properties .defaultHostName
397+
398+ output deployedFunctionUrl string = appServiceFunction .properties .defaultHostName
0 commit comments