-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.bicep
More file actions
641 lines (595 loc) · 21.6 KB
/
main.bicep
File metadata and controls
641 lines (595 loc) · 21.6 KB
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
metadata name = 'Modernize Your Code Solution Accelerator'
metadata description = '''CSA CTO Gold Standard Solution Accelerator for Modernize Your Code.
'''
@description('Set to true if you want to deploy WAF-aligned infrastructure.')
param useWafAlignedArchitecture bool
@minLength(3)
@maxLength(16)
@description('Required. A unique application/solution name for all resources in this deployment. This should be 3-16 characters long.')
param solutionName string
@maxLength(5)
@description('Optional. A unique token for the solution. This is used to ensure resource names are unique for global resources. Defaults to a 5-character substring of the unique string generated from the subscription ID, resource group name, and solution name.')
param solutionUniqueToken string = substring(uniqueString(subscription().id, resourceGroup().name, solutionName), 0, 5)
@minLength(3)
@metadata({ azd: { type: 'location' } })
@description('Optional. Azure region for all services. Defaults to the resource group location.')
param location string = resourceGroup().location
@allowed([
'australiaeast'
'eastus'
'eastus2'
'francecentral'
'japaneast'
'norwayeast'
'southindia'
'swedencentral'
'uksouth'
'westus'
'westus3'
])
@metadata({
azd : {
type: 'location'
usageName : [
'OpenAI.GlobalStandard.gpt-4o, 150'
]
}
})
@description('Optional. Location for all AI service resources. This location can be different from the resource group location.')
param aiDeploymentsLocation string
@description('Optional. AI model deployment token capacity. Defaults to 150K tokens per minute.')
param capacity int = 150
@description('Optional. Enable monitoring for the resources. This will enable Application Insights and Log Analytics. Defaults to false.')
param enableMonitoring bool = useWafAlignedArchitecture? true : false
@description('Optional. Enable scaling for the container apps. Defaults to false.')
param enableScaling bool = useWafAlignedArchitecture? true : false
@description('Optional. Enable redundancy for applicable resources. Defaults to false.')
param enableRedundancy bool = false
@description('Optional. The secondary location for the Cosmos DB account if redundancy is enabled.')
param secondaryLocation string?
@description('Optional. Enable private networking for the resources. Set to true to enable private networking. Defaults to false.')
param enablePrivateNetworking bool = useWafAlignedArchitecture? true : false
@description('Optional. Size of the Jumpbox Virtual Machine when created. Set to custom value if enablePrivateNetworking is true.')
param vmSize string?
@description('Optional. Admin username for the Jumpbox Virtual Machine. Set to custom value if enablePrivateNetworking is true.')
@secure()
//param vmAdminUsername string = take(newGuid(), 20)
param vmAdminUsername string?
@description('Optional. Admin password for the Jumpbox Virtual Machine. Set to custom value if enablePrivateNetworking is true.')
@secure()
//param vmAdminPassword string = newGuid()
param vmAdminPassword string?
@description('Optional. Specifies the resource tags for all the resources. Tag "azd-env-name" is automatically added to all resources.')
param tags object = {}
@description('Optional. Enable/Disable usage telemetry for module.')
param enableTelemetry bool = true
@minLength(1)
@description('GPT model deployment type:')
param deploymentType string = 'GlobalStandard'
@minLength(1)
@description('Name of the GPT model to deploy:')
param llmModel string = 'gpt-4o'
@minLength(1)
@description('Set the Image tag:')
param imageVersion string = 'latest'
@minLength(1)
@description('Version of the GPT model to deploy:')
param gptModelVersion string = '2024-08-06'
@description('Use this parameter to use an existing AI project resource ID')
param azureExistingAIProjectResourceId string = ''
param existingLogAnalyticsWorkspaceId string = ''
var allTags = union(
{
'azd-env-name': solutionName
},
tags
)
var resourcesName = toLower(trim(replace(
replace(
replace(replace(replace(replace('${solutionName}${solutionUniqueToken}', '-', ''), '_', ''), '.', ''), '/', ''),
' ',
''
),
'*',
''
)))
var modelDeployment = {
name: llmModel
model: {
name: llmModel
format: 'OpenAI'
version: gptModelVersion
}
sku: {
name: deploymentType
capacity: capacity
}
raiPolicyName: 'Microsoft.Default'
}
var abbrs = loadJsonContent('./abbreviations.json')
// ========== Resource Group Tag ========== //
resource resourceGroupTags 'Microsoft.Resources/tags@2021-04-01' = {
name: 'default'
properties: {
tags: {
...allTags
TemplateName: 'Code Modernization'
}
}
}
#disable-next-line no-deployments-resources
resource avmTelemetry 'Microsoft.Resources/deployments@2024-03-01' = if (enableTelemetry) {
name: take(
'46d3xbcp.ptn.sa-modernizeyourcode.${replace('-..--..-', '.', '-')}.${substring(uniqueString(deployment().name, location), 0, 4)}',
64
)
properties: {
mode: 'Incremental'
template: {
'$schema': 'https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#'
contentVersion: '1.0.0.0'
resources: []
outputs: {
telemetry: {
type: 'String'
value: 'For more information, see https://aka.ms/avm/TelemetryInfo'
}
}
}
}
}
module appIdentity 'br/public:avm/res/managed-identity/user-assigned-identity:0.4.1' = {
name: take('identity-app-${resourcesName}-deployment', 64)
params: {
name: '${abbrs.security.managedIdentity}${resourcesName}'
location: location
tags: allTags
enableTelemetry: enableTelemetry
}
}
// Extracts subscription, resource group, and workspace name from the resource ID when using an existing Log Analytics workspace
var useExistingLogAnalytics = !empty(existingLogAnalyticsWorkspaceId)
var existingLawSubscription = useExistingLogAnalytics ? split(existingLogAnalyticsWorkspaceId, '/')[2] : ''
var existingLawResourceGroup = useExistingLogAnalytics ? split(existingLogAnalyticsWorkspaceId, '/')[4] : ''
var existingLawName = useExistingLogAnalytics ? split(existingLogAnalyticsWorkspaceId, '/')[8] : ''
resource existingLogAnalyticsWorkspace 'Microsoft.OperationalInsights/workspaces@2020-08-01' existing = if (useExistingLogAnalytics) {
name: existingLawName
scope: resourceGroup(existingLawSubscription, existingLawResourceGroup)
}
// Deploy new Log Analytics workspace only if required and not using existing
module logAnalyticsWorkspace 'br/public:avm/res/operational-insights/workspace:0.11.2' = if ((enableMonitoring || enablePrivateNetworking) && !useExistingLogAnalytics) {
name: take('log-analytics-${resourcesName}-deployment', 64)
params: {
name: '${abbrs.managementGovernance.logAnalyticsWorkspace}${resourcesName}'
location: location
skuName: 'PerGB2018'
dataRetention: 30
diagnosticSettings: [{ useThisWorkspace: true }]
tags: allTags
enableTelemetry: enableTelemetry
}
}
// Log Analytics workspace ID, customer ID, and shared key (existing or new)
var logAnalyticsWorkspaceResourceId = useExistingLogAnalytics ? existingLogAnalyticsWorkspaceId : logAnalyticsWorkspace.outputs.resourceId
var LogAnalyticsPrimarySharedKey string = useExistingLogAnalytics? existingLogAnalyticsWorkspace.listKeys().primarySharedKey : logAnalyticsWorkspace.outputs.primarySharedKey
var LogAnalyticsWorkspaceId = useExistingLogAnalytics? existingLogAnalyticsWorkspace.properties.customerId : logAnalyticsWorkspace.outputs.logAnalyticsWorkspaceId
module applicationInsights 'br/public:avm/res/insights/component:0.6.0' = if (enableMonitoring) {
name: take('app-insights-${resourcesName}-deployment', 64)
params: {
name: '${abbrs.managementGovernance.applicationInsights}${resourcesName}'
location: location
workspaceResourceId: logAnalyticsWorkspaceResourceId
diagnosticSettings: [{ workspaceResourceId: logAnalyticsWorkspaceResourceId }]
tags: allTags
enableTelemetry: enableTelemetry
}
}
module network 'modules/network.bicep' = if (enablePrivateNetworking) {
name: take('network-${resourcesName}-deployment', 64)
params: {
resourcesName: resourcesName
logAnalyticsWorkSpaceResourceId: logAnalyticsWorkspaceResourceId
vmAdminUsername: vmAdminUsername ?? 'JumpboxAdminUser'
vmAdminPassword: vmAdminPassword ?? 'JumpboxAdminP@ssw0rd1234!'
vmSize: vmSize ?? 'Standard_DS2_v2' // Default VM size
location: location
tags: allTags
enableTelemetry: enableTelemetry
}
}
module aiServices 'modules/ai-foundry/main.bicep' = {
name: take('avm.res.cognitive-services.account.${resourcesName}', 64)
#disable-next-line no-unnecessary-dependson
dependsOn: [logAnalyticsWorkspace, network] // required due to optional flags that could change dependency
params: {
name: '${abbrs.ai.aiFoundry}${resourcesName}'
location: aiDeploymentsLocation
sku: 'S0'
kind: 'AIServices'
deployments: [ modelDeployment ]
projectName: '${abbrs.ai.aiFoundryProject}${resourcesName}'
projectDescription: '${abbrs.ai.aiFoundryProject}${resourcesName}'
logAnalyticsWorkspaceResourceId: enableMonitoring ? logAnalyticsWorkspaceResourceId : ''
privateNetworking: enablePrivateNetworking
? {
virtualNetworkResourceId: network.outputs.vnetResourceId
subnetResourceId: network.outputs.subnetPrivateEndpointsResourceId
}
: null
existingFoundryProjectResourceId: azureExistingAIProjectResourceId
disableLocalAuth: true //Should be set to true for WAF aligned configuration
customSubDomainName: 'ais-${resourcesName}'
apiProperties: {
//staticsEnabled: false
}
allowProjectManagement: true
managedIdentities: {
systemAssigned: true
}
publicNetworkAccess: 'Enabled'
networkAcls: {
bypass: 'AzureServices'
defaultAction: 'Allow'
}
privateEndpoints: []
roleAssignments: [
{
principalId: appIdentity.outputs.principalId
principalType: 'ServicePrincipal'
roleDefinitionIdOrName: 'Cognitive Services OpenAI Contributor'
}
{
principalId: appIdentity.outputs.principalId
principalType: 'ServicePrincipal'
roleDefinitionIdOrName: '64702f94-c441-49e6-a78b-ef80e0188fee' // Azure AI Developer
}
{
principalId: appIdentity.outputs.principalId
principalType: 'ServicePrincipal'
roleDefinitionIdOrName: '53ca6127-db72-4b80-b1b0-d745d6d5456d' // Azure AI User
}
]
tags: allTags
enableTelemetry: enableTelemetry
}
}
var appStorageContainerName = 'appstorage'
module storageAccount 'modules/storageAccount.bicep' = {
name: take('storage-account-${resourcesName}-deployment', 64)
#disable-next-line no-unnecessary-dependson
dependsOn: [logAnalyticsWorkspace, network] // required due to optional flags that could change dependency
params: {
name: take('${abbrs.storage.storageAccount}${resourcesName}', 24)
location: location
tags: allTags
skuName: enableRedundancy ? 'Standard_GZRS' : 'Standard_LRS'
logAnalyticsWorkspaceResourceId: enableMonitoring ? logAnalyticsWorkspaceResourceId : ''
privateNetworking: enablePrivateNetworking
? {
virtualNetworkResourceId: network.outputs.vnetResourceId
subnetResourceId: network.outputs.subnetPrivateEndpointsResourceId
}
: null
containers: [
{
name: appStorageContainerName
properties: {
publicAccess: 'None'
}
}
]
roleAssignments: [
{
principalId: appIdentity.outputs.principalId
principalType: 'ServicePrincipal'
roleDefinitionIdOrName: 'Storage Blob Data Contributor'
}
]
enableTelemetry: enableTelemetry
}
}
module keyVault 'modules/keyVault.bicep' = {
name: take('keyvault-${resourcesName}-deployment', 64)
#disable-next-line no-unnecessary-dependson
dependsOn: [logAnalyticsWorkspace, network] // required due to optional flags that could change dependency
params: {
name: take('${abbrs.security.keyVault}${resourcesName}', 24)
location: location
sku: 'standard'
logAnalyticsWorkspaceResourceId: enableMonitoring ? logAnalyticsWorkspaceResourceId : ''
privateNetworking: enablePrivateNetworking
? {
virtualNetworkResourceId: network.outputs.vnetResourceId
subnetResourceId: network.outputs.subnetPrivateEndpointsResourceId
}
: null
roleAssignments: [
{
principalId: aiServices.outputs.?systemAssignedMIPrincipalId ?? appIdentity.outputs.principalId
principalType: 'ServicePrincipal'
roleDefinitionIdOrName: 'Key Vault Administrator'
}
]
tags: allTags
enableTelemetry: enableTelemetry
}
}
module cosmosDb 'modules/cosmosDb.bicep' = {
name: take('cosmos-${resourcesName}-deployment', 64)
#disable-next-line no-unnecessary-dependson
dependsOn: [logAnalyticsWorkspace, network] // required due to optional flags that could change dependency
params: {
name: take('${abbrs.databases.cosmosDBDatabase}${resourcesName}', 44)
location: location
dataAccessIdentityPrincipalId: appIdentity.outputs.principalId
logAnalyticsWorkspaceResourceId: enableMonitoring ? logAnalyticsWorkspaceResourceId : ''
zoneRedundant: enableRedundancy
secondaryLocation: enableRedundancy && !empty(secondaryLocation) ? secondaryLocation : ''
privateNetworking: enablePrivateNetworking
? {
virtualNetworkResourceId: network.outputs.vnetResourceId
subnetResourceId: network.outputs.subnetPrivateEndpointsResourceId
}
: null
tags: allTags
enableTelemetry: enableTelemetry
}
}
var containerAppsEnvironmentName = '${abbrs.containers.containerAppsEnvironment}${resourcesName}'
module containerAppsEnvironment 'br/public:avm/res/app/managed-environment:0.11.2' = {
name: take('container-env-${resourcesName}-deployment', 64)
#disable-next-line no-unnecessary-dependson
dependsOn: [applicationInsights, logAnalyticsWorkspace, network] // required due to optional flags that could change dependency
params: {
name: containerAppsEnvironmentName
infrastructureResourceGroupName: '${resourceGroup().name}-ME-${containerAppsEnvironmentName}'
location: location
zoneRedundant: enableRedundancy && enablePrivateNetworking
publicNetworkAccess: 'Enabled' // public access required for frontend
infrastructureSubnetResourceId: enablePrivateNetworking ? network.outputs.subnetWebResourceId : null
managedIdentities: {
userAssignedResourceIds: [
appIdentity.outputs.resourceId
]
}
appInsightsConnectionString: enableMonitoring ? applicationInsights.outputs.connectionString : null
appLogsConfiguration: enableMonitoring
? {
destination: 'log-analytics'
logAnalyticsConfiguration: {
customerId: LogAnalyticsWorkspaceId
sharedKey: LogAnalyticsPrimarySharedKey
}
}
: {}
workloadProfiles: enablePrivateNetworking
? [
// NOTE: workload profiles are required for private networking
{
name: 'Consumption'
workloadProfileType: 'Consumption'
}
]
: []
tags: allTags
enableTelemetry: enableTelemetry
}
}
module containerAppBackend 'br/public:avm/res/app/container-app:0.17.0' = {
name: take('container-app-backend-${resourcesName}-deployment', 64)
#disable-next-line no-unnecessary-dependson
dependsOn: [applicationInsights] // required due to optional flags that could change dependency
params: {
name: take('${abbrs.containers.containerApp}backend-${resourcesName}', 32)
location: location
environmentResourceId: containerAppsEnvironment.outputs.resourceId
managedIdentities: {
userAssignedResourceIds: [
appIdentity.outputs.resourceId
]
}
containers: [
{
name: 'cmsabackend'
image: 'cmsacontainerreg.azurecr.io/cmsabackend:${imageVersion}'
env: concat(
[
{
name: 'COSMOSDB_ENDPOINT'
value: cosmosDb.outputs.endpoint
}
{
name: 'COSMOSDB_DATABASE'
value: cosmosDb.outputs.databaseName
}
{
name: 'COSMOSDB_BATCH_CONTAINER'
value: cosmosDb.outputs.containerNames.batch
}
{
name: 'COSMOSDB_FILE_CONTAINER'
value: cosmosDb.outputs.containerNames.file
}
{
name: 'COSMOSDB_LOG_CONTAINER'
value: cosmosDb.outputs.containerNames.log
}
{
name: 'AZURE_BLOB_ACCOUNT_NAME'
value: storageAccount.outputs.name
}
{
name: 'AZURE_BLOB_CONTAINER_NAME'
value: appStorageContainerName
}
{
name: 'AZURE_OPENAI_ENDPOINT'
value: 'https://${aiServices.outputs.name}.openai.azure.com/'
}
{
name: 'MIGRATOR_AGENT_MODEL_DEPLOY'
value: modelDeployment.name
}
{
name: 'PICKER_AGENT_MODEL_DEPLOY'
value: modelDeployment.name
}
{
name: 'FIXER_AGENT_MODEL_DEPLOY'
value: modelDeployment.name
}
{
name: 'SEMANTIC_VERIFIER_AGENT_MODEL_DEPLOY'
value: modelDeployment.name
}
{
name: 'SYNTAX_CHECKER_AGENT_MODEL_DEPLOY'
value: modelDeployment.name
}
{
name: 'SELECTION_MODEL_DEPLOY'
value: modelDeployment.name
}
{
name: 'TERMINATION_MODEL_DEPLOY'
value: modelDeployment.name
}
{
name: 'AZURE_AI_AGENT_MODEL_DEPLOYMENT_NAME'
value: modelDeployment.name
}
{
name: 'AI_PROJECT_ENDPOINT'
value: aiServices.outputs.aiProjectInfo.apiEndpoint // or equivalent
}
{
name: 'AZURE_AI_AGENT_PROJECT_CONNECTION_STRING' // This was not really used in code.
value: aiServices.outputs.aiProjectInfo.apiEndpoint
}
{
name: 'AZURE_AI_AGENT_PROJECT_NAME'
value: aiServices.outputs.aiProjectInfo.name
}
{
name: 'AZURE_AI_AGENT_RESOURCE_GROUP_NAME'
value: resourceGroup().name
}
{
name: 'AZURE_AI_AGENT_SUBSCRIPTION_ID'
value: subscription().subscriptionId
}
{
name: 'AZURE_AI_AGENT_ENDPOINT'
value: aiServices.outputs.aiProjectInfo.apiEndpoint
}
{
name: 'AZURE_CLIENT_ID'
value: appIdentity.outputs.clientId // NOTE: This is the client ID of the managed identity, not the Entra application, and is needed for the App Service to access the Cosmos DB account.
}
],
enableMonitoring
? [
{
name: 'APPLICATIONINSIGHTS_INSTRUMENTATION_KEY'
value: applicationInsights.outputs.instrumentationKey
}
{
name: 'APPLICATIONINSIGHTS_CONNECTION_STRING'
value: applicationInsights.outputs.connectionString
}
]
: []
)
resources: {
cpu: 1
memory: '2.0Gi'
}
probes: enableMonitoring
? [
{
httpGet: {
path: '/health'
port: 8000
}
initialDelaySeconds: 3
periodSeconds: 3
type: 'Liveness'
}
]
: []
}
]
ingressTargetPort: 8000
ingressExternal: true
scaleSettings: {
maxReplicas: enableScaling ? 3 : 1
minReplicas: 1
rules: enableScaling
? [
{
name: 'http-scaler'
http: {
metadata: {
concurrentRequests: 100
}
}
}
]
: []
}
tags: allTags
enableTelemetry: enableTelemetry
}
}
module containerAppFrontend 'br/public:avm/res/app/container-app:0.17.0' = {
name: take('container-app-frontend-${resourcesName}-deployment', 64)
params: {
name: take('${abbrs.containers.containerApp}frontend-${resourcesName}', 32)
location: location
environmentResourceId: containerAppsEnvironment.outputs.resourceId
managedIdentities: {
userAssignedResourceIds: [
appIdentity.outputs.resourceId
]
}
containers: [
{
env: [
{
name: 'API_URL'
value: 'https://${containerAppBackend.outputs.fqdn}'
}
]
image: 'cmsacontainerreg.azurecr.io/cmsafrontend:${imageVersion}'
name: 'cmsafrontend'
resources: {
cpu: '1'
memory: '2.0Gi'
}
}
]
ingressTargetPort: 3000
ingressExternal: true
scaleSettings: {
maxReplicas: enableScaling ? 3 : 1
minReplicas: 1
rules: enableScaling
? [
{
name: 'http-scaler'
http: {
metadata: {
concurrentRequests: 100
}
}
}
]
: []
}
tags: allTags
enableTelemetry: enableTelemetry
}
}
@description('The resource group the resources were deployed into.')
output resourceGroupName string = resourceGroup().name
output WEB_APP_URL string = 'https://${containerAppFrontend.outputs.fqdn}'