-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeployment.bicep
465 lines (421 loc) · 14 KB
/
deployment.bicep
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
@description('Resource Suffix to append to resources and give uniqueness.')
param resourceSuffix string = uniqueString(resourceGroup().id) // Resource Suffix to append to resources and give uniqueness
@description('Name of the web app.')
param webAppName string = toLower('chat-${resourceSuffix}') // Generate unique String for web app name
@description('The pricing tier for the hosting plan.')
@allowed([
'B1'
'B2'
'P0V3'
])
param sku string = 'B1' // The SKU of App Service Plan
@description('The instance size of the hosting plan (small, medium, or large).')
@allowed([
'0'
'1'
'2'
])
param workerSize string = '0' // The instance size of the hosting plan (small, medium, or large).
@description('The location for all resources.')
param location string = 'northeurope' // Location for all resources
@description('The location for open ai and azure search resources.')
param aiLocation string = 'swedencentral' // Location for ai resources
@description('Name of the azure search.')
param azureSearchName string = toLower('search-${resourceSuffix}') // The name of the Azure Search service
@description('SKU of the azure search.')
@allowed([
'basic'
'standard'
'standard2'
'standard3'
])
param azureSearchSku string = 'basic' // The SKU of the Azure Search service
@description('Replica count of the azure search.')
@minValue(1)
param azureSearchReplicaCount int = 1 // The replica count of the Azure Search service
@description('Partition count of the azure search.')
@minValue(1)
param azureSearchPartitionCount int = 1 // The partition count of the Azure Search service
@description('Name of the azure openai.')
param azureOpenAiName string = toLower('openai${resourceSuffix}') // The name of the Azure OpenAI service
@description('Name of the azure openai gpt-4o deployment.')
param gpt4oDeploymentName string = 'gpt-4o' // The name of the Azure OpenAI GPT-4o deployment
@description('Capacity of the azure openai gpt-4o deployment.')
param gpt4oDeploymentCapacity int = 450 // The capacity of the Azure OpenAI GPT-4o deployment
@description('Name of the azure openai ada text embedding deployment.')
param adaDeploymentName string = 'text-embedding-ada-002' // The name of the Azure OpenAI Ada deployment
@description('Capacity of the azure openai ada deployment.')
param adaDeploymentCapacity int = 350 // The capacity of the Azure OpenAI ada deployment
// variables
var linuxFxVersion = 'PYTHON|3.12' // The runtime stack of web app
var appServicePlanName = toLower('plan-${webAppName}')
var storageAccountName = toLower('storage${resourceSuffix}')
var storageAccountSuffix = environment().suffixes.storage
var searchSuffix = 'search.windows.net'
resource storageAccount 'Microsoft.Storage/storageAccounts@2021-04-01' = {
name: storageAccountName
location: location
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
properties: {
networkAcls: {
defaultAction: 'Allow'
bypass: 'AzureServices'
virtualNetworkRules: [
]
}
}
}
resource tableService 'Microsoft.Storage/storageAccounts/tableServices@2021-04-01' = {
name: 'default'
parent: storageAccount
properties: {}
}
resource historyTable 'Microsoft.Storage/storageAccounts/tableServices/tables@2021-04-01' = {
parent: tableService
name: 'history'
}
resource blobService 'Microsoft.Storage/storageAccounts/blobServices@2021-04-01' = {
name: 'default'
parent: storageAccount
properties: {
containerDeleteRetentionPolicy: {
enabled: true
days: 7
}
deleteRetentionPolicy: {
days: 7
enabled: true
}
}
}
resource blobContainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2021-04-01' = {
parent: blobService
name: 'documents'
}
resource appServicePlan 'Microsoft.Web/serverfarms@2022-09-01' = {
name: appServicePlanName
location: location
properties: {
reserved: true
}
sku: {
name: sku
capacity: int(workerSize)
}
kind: 'linux'
}
resource webApp 'Microsoft.Web/sites@2022-09-01' = {
name: webAppName
location: location
identity: {
type: 'SystemAssigned'
}
properties: {
enabled: true
serverFarmId: appServicePlan.id
httpsOnly: true
siteConfig: {
publicNetworkAccess: 'Enabled'
ipSecurityRestrictionsDefaultAction: 'Allow'
scmIpSecurityRestrictionsDefaultAction: 'Allow'
linuxFxVersion: linuxFxVersion
alwaysOn: true
appCommandLine: 'gunicorn --bind=0.0.0.0 --workers=4 startup:app'
appSettings: [
// {
// name: 'AZURE_STORAGEBLOB_CONNECTIONSTRING'
// value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccount.name};AccountKey=${storageAccount.listKeys().keys[0].value};EndpointSuffix=${storageAccountSuffix}'
// }
{
name: 'AZURE_STORAGEBLOB_RESOURCEENDPOINT'
value: 'https://${storageAccount.name}.blob.${storageAccountSuffix}'
}
{
name: 'OPENAI_API_BASE'
value: azureOpenAi.properties.endpoint
}
{
name: 'OPENAI_EMBEDDING_DEPLOYMENT_NAME'
value: adaDeploymentName
}
{
name: 'OPENAI_DEPLOYMENT_NAME'
value: gpt4oDeploymentName
}
{
name: 'AZURESEARCH_API_BASE'
value: 'https://${azureSearchName}.${searchSuffix}'
}
{
name: 'AZURESEARCH_INDEX_NAME'
value: 'documents'
}
{
name:'SCM_DO_BUILD_DURING_DEPLOYMENT'
value:'true'
}
{
name: 'CHATBOT_SECRET_KEY'
value: '${uniqueString(subscription().id)}${uniqueString(resourceGroup().id)}'
}
]
}
}
}
resource azureSearch 'Microsoft.Search/searchServices@2024-03-01-preview' = {
name: azureSearchName
location: location
sku: {
name: azureSearchSku
}
identity: {
type: 'SystemAssigned'
}
properties: {
replicaCount: azureSearchReplicaCount
partitionCount: azureSearchPartitionCount
semanticSearch: 'standard'
disableLocalAuth: false
authOptions: {
aadOrApiKey: {
aadAuthFailureMode: 'http401WithBearerChallenge'
}
}
}
}
resource azureOpenAi 'Microsoft.CognitiveServices/accounts@2023-05-01' = {
name: azureOpenAiName
location: aiLocation
sku: {
name: 'S0'
}
kind: 'OpenAI'
identity: {
type: 'SystemAssigned'
}
properties: {
networkAcls: {
defaultAction: 'Allow'
virtualNetworkRules: []
ipRules: []
}
publicNetworkAccess: 'Enabled'
customSubDomainName: toLower(azureOpenAiName)
}
}
resource azureOpenAiAdaDeplyoment 'Microsoft.CognitiveServices/accounts/deployments@2023-05-01' = {
parent: azureOpenAi
name: adaDeploymentName
sku: {
name: 'Standard'
capacity: adaDeploymentCapacity
}
properties: {
model: {
format: 'OpenAI'
name: 'text-embedding-ada-002'
version: '2'
}
versionUpgradeOption: 'OnceNewDefaultVersionAvailable'
raiPolicyName: 'Microsoft.DefaultV2'
}
}
resource azureOpenAiGpt4oDeplyoment 'Microsoft.CognitiveServices/accounts/deployments@2023-05-01' = {
parent: azureOpenAi
name: gpt4oDeploymentName
sku: {
name: 'GlobalStandard'
capacity: gpt4oDeploymentCapacity
}
properties: {
model: {
format: 'OpenAI'
name: 'gpt-4o'
version: '2024-05-13'
}
versionUpgradeOption: 'OnceNewDefaultVersionAvailable'
raiPolicyName: 'Microsoft.DefaultV2'
}
dependsOn: [
azureOpenAiAdaDeplyoment
]
}
// give azure search access to storage account (Storage Blob Data Contributor)
resource azureSearchToStorage 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(storageAccount.id, azureSearch.id, 'ba92f5b4-2d11-453d-a403-e96b0029c9fe')
scope: storageAccount
properties: {
principalId: azureSearch.identity.principalId
roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', 'ba92f5b4-2d11-453d-a403-e96b0029c9fe')
}
dependsOn: [
blobService
tableService
historyTable
blobContainer
]
}
// give open ai access to storage account (Storage Blob Data Contributor)
resource openAiToStorage 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(storageAccount.id, azureOpenAi.id, 'ba92f5b4-2d11-453d-a403-e96b0029c9fe')
scope: storageAccount
properties: {
principalId: azureOpenAi.identity.principalId
roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', 'ba92f5b4-2d11-453d-a403-e96b0029c9fe')
}
dependsOn: [
blobService
tableService
historyTable
blobContainer
azureOpenAiAdaDeplyoment
azureOpenAiGpt4oDeplyoment
azureSearchToStorage
]
}
// give web app access to storage account (Storage Blob Data Contributor)
resource webAppToStorage 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(storageAccount.id, webApp.id, 'ba92f5b4-2d11-453d-a403-e96b0029c9fe')
scope: storageAccount
properties: {
principalId: webApp.identity.principalId
roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', 'ba92f5b4-2d11-453d-a403-e96b0029c9fe')
}
dependsOn: [
blobService
tableService
historyTable
blobContainer
azureSearchToStorage
openAiToStorage
]
}
// give web app access to storage account (Storage Table Data Contributor)
resource webAppToStorage2 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(storageAccount.id, webApp.id, '0a9a7e1f-b9d0-4cc4-a60d-0319b160aaa3')
scope: storageAccount
properties: {
principalId: webApp.identity.principalId
roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', '0a9a7e1f-b9d0-4cc4-a60d-0319b160aaa3')
}
dependsOn: [
blobService
tableService
historyTable
blobContainer
azureSearchToStorage
openAiToStorage
webAppToStorage
]
}
// give open ai access to azure search (Search Index Data Contributor)
resource openAiToAzureSearch1 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(azureSearch.id, azureOpenAi.id, '8ebe5a00-799e-43f5-93ac-243d3dce84a7')
scope: azureSearch
properties: {
principalId: azureOpenAi.identity.principalId
roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', '8ebe5a00-799e-43f5-93ac-243d3dce84a7')
}
dependsOn: [
azureOpenAiAdaDeplyoment
azureOpenAiGpt4oDeplyoment
]
}
// give open ai access to azure search (Search Index Data Reader)
resource openAiToAzureSearch2 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(azureSearch.id, azureOpenAi.id, '1407120a-92aa-4202-b7e9-c0e197c71c8f')
scope: azureSearch
properties: {
principalId: azureOpenAi.identity.principalId
roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', '1407120a-92aa-4202-b7e9-c0e197c71c8f')
}
dependsOn: [
azureOpenAiAdaDeplyoment
azureOpenAiGpt4oDeplyoment
openAiToAzureSearch1
]
}
// give open ai access to azure search (Search Service Contributor)
resource openAiToAzureSearch3 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(azureSearch.id, azureOpenAi.id, '7ca78c08-252a-4471-8644-bb5ff32d4ba0')
scope: azureSearch
properties: {
principalId: azureOpenAi.identity.principalId
roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', '7ca78c08-252a-4471-8644-bb5ff32d4ba0')
}
dependsOn: [
azureOpenAiAdaDeplyoment
azureOpenAiGpt4oDeplyoment
openAiToAzureSearch1
openAiToAzureSearch2
]
}
// give web app access to azure search (Search Index Data Contributor)
resource webAppToAzureSearch1 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(azureSearch.id, webApp.id, '8ebe5a00-799e-43f5-93ac-243d3dce84a7')
scope: azureSearch
properties: {
principalId: webApp.identity.principalId
roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', '8ebe5a00-799e-43f5-93ac-243d3dce84a7')
}
dependsOn: [
openAiToAzureSearch1
openAiToAzureSearch2
openAiToAzureSearch3
]
}
// give web app access to azure search (Search Index Data Reader)
resource webAppToAzureSearch2 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(azureSearch.id, webApp.id, '1407120a-92aa-4202-b7e9-c0e197c71c8f')
scope: azureSearch
properties: {
principalId: webApp.identity.principalId
roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', '1407120a-92aa-4202-b7e9-c0e197c71c8f')
}
dependsOn: [
openAiToAzureSearch1
openAiToAzureSearch2
openAiToAzureSearch3
webAppToAzureSearch1
]
}
// give azure search access to open ai (Cognitive Services OpenAI Contributor)
resource azureSearchToOpenAi 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(azureOpenAi.id, azureSearch.id, 'a001fd3d-188f-4b5d-821b-7da978bf7442')
scope: azureOpenAi
properties: {
principalId: azureSearch.identity.principalId
roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', 'a001fd3d-188f-4b5d-821b-7da978bf7442')
}
dependsOn: [
azureOpenAiAdaDeplyoment
azureOpenAiGpt4oDeplyoment
]
}
// give web app access to open ai (Cognitive Services OpenAI User)
resource webAppToOpenAi 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(azureOpenAi.id, webApp.id, '5e0bd9bd-7b93-4f28-af87-19fc36ad61bd')
scope: azureOpenAi
properties: {
principalId: webApp.identity.principalId
roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', '5e0bd9bd-7b93-4f28-af87-19fc36ad61bd')
}
dependsOn: [
azureSearchToOpenAi
]
}
// output the name of the web app
output webAppName string = webAppName
// output the url of the web app
output webAppUrl string = webApp.properties.defaultHostName
// output the name of the azure search
output azureSearchName string = azureSearch.name
// output the storage account name
output storageAccountName string = storageAccount.name
// output the endpoint of the azure openai
output azureOpenAiEndpoint string = azureOpenAi.properties.endpoint
// output the endpoit of the azure search
output azureSearchEndpoint string = 'https://${azureSearchName}.${searchSuffix}'