-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapp.bicep
More file actions
420 lines (366 loc) · 14.5 KB
/
app.bicep
File metadata and controls
420 lines (366 loc) · 14.5 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
targetScope = 'resourceGroup'
@minLength(1)
@maxLength(48)
@description('Name of the workload which is used to generate a short unique hash used in all resources.')
param workloadName string
@minLength(1)
@description('Primary location for all resources.')
param location string
@description('Tags for all resources.')
param tags object = {
WorkloadName: workloadName
Environment: 'Dev'
ApplicationName: applicationName
}
@description('Name of the application.')
param applicationName string = 'ai-document-pipeline'
@description('Name of the container image.')
param containerImageName string
@description('Name of the Azure OpenAI completion model for the application. Default is gpt-4o.')
param chatModelDeployment string = 'gpt-4o'
@description('Name of the existing workload App Configuration Store where the application settings are stored. If left empty, the default name generated using Azure CAF best practices will be used.')
param appConfigName string = ''
@description('Name of the existing workload Container Registry. If left empty, the default name generated using Azure CAF best practices will be used.')
param containerRegistryName string = ''
@description('Name of the existing workload Application Insights resource. If left empty, the default name generated using Azure CAF best practices will be used.')
param applicationInsightsName string = ''
@description('Name of the existing workload Storage Account. If left empty, the default name generated using Azure CAF best practices will be used.')
param storageAccountName string = ''
@description('Name of the existing workload AI Services resource. If left empty, the default name generated using Azure CAF best practices will be used.')
param aiServicesName string = ''
@description('Name of the existing workload Container Apps Environment. If left empty, the default name generated using Azure CAF best practices will be used.')
param containerAppsEnvironmentName string = ''
// Variables
@description('List of recommended abbreviation prefixes for resources, as defined in https://learn.microsoft.com/en-us/azure/cloud-adoption-framework/ready/azure-best-practices/resource-abbreviations.')
var abbrs = loadJsonContent('../../abbreviations.json')
@description('List of built-in roles for Azure resources, as defined in https://learn.microsoft.com/en-us/azure/role-based-access-control/built-in-roles.')
var roles = loadJsonContent('../../roles.json')
@description('Resource token for the workload, used to generate unique names for shared resources.')
var resourceToken = toLower(uniqueString(subscription().id, workloadName, location))
@description('Resource token for the application, used to generate unique names for application-specific resources.')
var appResourceToken = toLower(uniqueString(subscription().id, workloadName, location, applicationName))
@description('Name of the existing App Configuration Store where the application settings are stored. If left empty, the default name generated using Azure CAF best practices will be used.')
var _appConfigName = !empty(appConfigName)
? appConfigName
: '${abbrs.developerTools.appConfigurationStore}${resourceToken}'
@description('Name of the existing workload Container Registry. If left empty, the default name generated using Azure CAF best practices will be used.')
var _containerRegistryName = !empty(containerRegistryName)
? containerRegistryName
: '${abbrs.containers.containerRegistry}${resourceToken}'
@description('Name of the existing workload Application Insights resource.')
var _applicationInsightsName = !empty(applicationInsightsName)
? applicationInsightsName
: '${abbrs.managementGovernance.applicationInsights}${resourceToken}'
@description('Name of the existing workload Storage Account.')
var _storageAccountName = !empty(storageAccountName)
? storageAccountName
: '${abbrs.storage.storageAccount}${resourceToken}'
@description('Name of the existing workload AI Services resource.')
var _aiServicesName = !empty(aiServicesName) ? aiServicesName : '${abbrs.ai.aiServices}${resourceToken}'
@description('Name of the existing workload Container Apps Environment.')
var _containerAppsEnvironmentName = !empty(containerAppsEnvironmentName)
? containerAppsEnvironmentName
: '${abbrs.containers.containerAppsEnvironment}${resourceToken}'
@description('Name of the Container App for the application.')
var containerAppName = '${abbrs.containers.containerApp}${appResourceToken}'
@description('Name of the user-assigned managed identity for the application.')
var applicationManagedIdentityName = '${abbrs.security.managedIdentity}${containerAppName}'
@description('Application config key for the Azure Storage Queue connection string.')
var documentsConnectionStringVariableName = 'AZURE_STORAGE_QUEUES_CONNECTION_STRING'
@description('Name of the Container App secret for the Application Insights connection string.')
var applicationInsightsConnectionStringSecretName = 'applicationinsightsconnectionstring'
@description('Name of the Container App secret for the Application Insights instrumentation key.')
var applicationInsightsKeySecretName = 'applicationinsightskey'
@description('Name of the Azure Storage Queue for processing documents.')
var documentsQueueName = 'documents'
// Deployments
resource appConfigStoreRef 'Microsoft.AppConfiguration/configurationStores@2024-05-01' existing = {
name: _appConfigName
}
resource containerRegistryRef 'Microsoft.ContainerRegistry/registries@2025-04-01' existing = {
name: _containerRegistryName
}
resource applicationInsightsRef 'Microsoft.Insights/components@2020-02-02' existing = {
name: _applicationInsightsName
}
resource storageAccountRef 'Microsoft.Storage/StorageAccounts@2024-01-01' existing = {
name: _storageAccountName
}
resource aiServicesRef 'Microsoft.CognitiveServices/accounts@2025-04-01-preview' existing = {
name: _aiServicesName
}
resource containerAppsEnvironmentRef 'Microsoft.App/managedEnvironments@2025-01-01' existing = {
name: _containerAppsEnvironmentName
}
module applicationManagedIdentity '../../security/managed-identity.bicep' = {
name: applicationManagedIdentityName
params: {
name: applicationManagedIdentityName
location: location
tags: union(tags, {})
}
}
resource acrPullRole 'Microsoft.Authorization/roleDefinitions@2022-05-01-preview' existing = {
name: roles.containers.acrPull
}
module containerRegistryIdentityRoleAssignment '../../security/resource-role-assignment.json' = {
name: 'containerRegistryIdentityRoleAssignment'
params: {
resourceId: containerRegistryRef.id
roleAssignments: [
{
principalId: applicationManagedIdentity.outputs.principalId
roleDefinitionId: acrPullRole.id
principalType: 'ServicePrincipal'
}
]
}
}
// Required RBAC roles for Azure Functions to access the storage account
// https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference?tabs=blob&pivots=programming-language-python#connecting-to-host-storage-with-an-identity
resource storageAccountContributorRole 'Microsoft.Authorization/roleDefinitions@2022-05-01-preview' existing = {
name: roles.storage.storageAccountContributor
}
resource storageBlobDataContributorRole 'Microsoft.Authorization/roleDefinitions@2022-05-01-preview' existing = {
name: roles.storage.storageBlobDataContributor
}
resource storageBlobDataOwnerRole 'Microsoft.Authorization/roleDefinitions@2022-05-01-preview' existing = {
name: roles.storage.storageBlobDataOwner
}
resource storageFileDataPrivilegedContributorRole 'Microsoft.Authorization/roleDefinitions@2022-05-01-preview' existing = {
name: roles.storage.storageFileDataPrivilegedContributor
}
resource storageTableDataContributorRole 'Microsoft.Authorization/roleDefinitions@2022-05-01-preview' existing = {
name: roles.storage.storageTableDataContributor
}
resource storageQueueDataContributorRole 'Microsoft.Authorization/roleDefinitions@2022-05-01-preview' existing = {
name: roles.storage.storageQueueDataContributor
}
module storageAccountIdentityRoleAssignment '../../security/resource-role-assignment.json' = {
name: 'storageAccountIdentityRoleAssignment'
params: {
resourceId: storageAccountRef.id
roleAssignments: [
{
principalId: applicationManagedIdentity.outputs.principalId
roleDefinitionId: storageAccountContributorRole.id
principalType: 'ServicePrincipal'
}
{
principalId: applicationManagedIdentity.outputs.principalId
roleDefinitionId: storageBlobDataContributorRole.id
principalType: 'ServicePrincipal'
}
{
principalId: applicationManagedIdentity.outputs.principalId
roleDefinitionId: storageBlobDataOwnerRole.id
principalType: 'ServicePrincipal'
}
{
principalId: applicationManagedIdentity.outputs.principalId
roleDefinitionId: storageFileDataPrivilegedContributorRole.id
principalType: 'ServicePrincipal'
}
{
principalId: applicationManagedIdentity.outputs.principalId
roleDefinitionId: storageTableDataContributorRole.id
principalType: 'ServicePrincipal'
}
{
principalId: applicationManagedIdentity.outputs.principalId
roleDefinitionId: storageQueueDataContributorRole.id
principalType: 'ServicePrincipal'
}
]
}
}
resource cognitiveServicesUserRole 'Microsoft.Authorization/roleDefinitions@2022-05-01-preview' existing = {
name: roles.ai.cognitiveServicesUser
}
resource cognitiveServicesOpenAIUserRole 'Microsoft.Authorization/roleDefinitions@2022-05-01-preview' existing = {
name: roles.ai.cognitiveServicesOpenAIUser
}
resource appConfigDataOwnerRole 'Microsoft.Authorization/roleDefinitions@2022-05-01-preview' existing = {
name: roles.integration.appConfigurationDataOwner
}
module aiServicesIdentityRoleAssignment '../../security/resource-role-assignment.json' = {
name: 'aiServicesIdentityRoleAssignment'
params: {
resourceId: aiServicesRef.id
roleAssignments: [
{
principalId: applicationManagedIdentity.outputs.principalId
roleDefinitionId: appConfigDataOwnerRole.id
principalType: 'ServicePrincipal'
}
{
principalId: applicationManagedIdentity.outputs.principalId
roleDefinitionId: cognitiveServicesUserRole.id
principalType: 'ServicePrincipal'
}
{
principalId: applicationManagedIdentity.outputs.principalId
roleDefinitionId: cognitiveServicesOpenAIUserRole.id
principalType: 'ServicePrincipal'
}
]
}
}
module documentsQueue '../../storage/storage-queue.bicep' = {
name: '${abbrs.storage.storageAccount}${resourceToken}-${documentsQueueName}'
params: {
name: documentsQueueName
storageAccountName: storageAccountRef.name
}
}
module containerApp '../../containers/container-app.bicep' = {
name: containerAppName
params: {
name: containerAppName
location: location
tags: union(tags, { App: applicationName })
containerAppsEnvironmentId: containerAppsEnvironmentRef.id
containerAppIdentityId: applicationManagedIdentity.outputs.id
imageInContainerRegistry: true
containerRegistryName: containerRegistryRef.name
containerImageName: containerImageName
containerIngress: {
external: true
targetPort: 80
transport: 'auto'
allowInsecure: false
}
containerScale: {
minReplicas: 1
maxReplicas: 3
rules: [
{
name: 'http'
http: {
metadata: {
concurrentRequests: '20'
}
}
}
]
}
secrets: [
{
name: applicationInsightsConnectionStringSecretName
value: applicationInsightsRef.properties.ConnectionString
}
{
name: applicationInsightsKeySecretName
value: applicationInsightsRef.properties.InstrumentationKey
}
]
environmentVariables: [
{
name: 'AzureWebJobsFeatureFlags'
value: 'EnableWorkerIndexing'
}
{
name: 'FUNCTIONS_EXTENSION_VERSION'
value: '~4'
}
{
name: 'FUNCTIONS_WORKER_RUNTIME'
value: 'python'
}
{
name: 'APPLICATIONINSIGHTS_CONNECTION_STRING'
secretRef: applicationInsightsConnectionStringSecretName
}
{
name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
secretRef: applicationInsightsKeySecretName
}
{
name: 'ApplicationInsights_InstrumentationKey'
secretRef: applicationInsightsKeySecretName
}
{
name: 'AZURE_APPCONFIG_URL'
value: 'https://${appConfigStoreRef.name}.azconfig.io'
}
{
name: 'AzureWebJobsStorage__accountName'
value: storageAccountRef.name
}
{
name: 'AzureWebJobsStorage__credential'
value: 'managedidentity'
}
{
name: 'AzureWebJobsStorage__clientId'
value: applicationManagedIdentity.outputs.clientId
}
{
name: 'AZURE_CLIENT_ID'
value: applicationManagedIdentity.outputs.clientId
}
{
name: 'AZURE_TENANT_ID'
value: subscription().tenantId
}
{
name: 'AZURE_AISERVICES_ENDPOINT'
value: aiServicesRef.properties.endpoint
}
{
name: 'AZURE_OPENAI_ENDPOINT'
value: aiServicesRef.properties.endpoint
}
{
name: 'AZURE_OPENAI_CHAT_DEPLOYMENT'
value: chatModelDeployment
}
{
name: 'AZURE_STORAGE_ACCOUNT'
value: storageAccountRef.name
}
{
name: '${documentsConnectionStringVariableName}__accountName'
value: storageAccountRef.name
}
{
name: '${documentsConnectionStringVariableName}__credential'
value: 'managedidentity'
}
{
name: '${documentsConnectionStringVariableName}__clientId'
value: applicationManagedIdentity.outputs.clientId
}
{
name: 'WEBSITE_HOSTNAME'
value: 'localhost'
}
{
name: 'LOGLEVEL'
value: 'INFO'
}
{
name: 'WEBSITE_HTTPLOGGING_RETENTION_DAYS'
value: '7'
}
{
name: 'WEBSITE_VNET_ROUTE_ALL'
value: '0'
}
{
name: 'ALLOW_ENVIRONMENT_VARIABLES'
value: 'true'
}
]
}
}
// Outputs
@description('Details of the deployed Container App resource, including ID, name, FQDN, and URL.')
output appInfo object = {
id: containerApp.outputs.id
name: containerApp.outputs.name
fqdn: containerApp.outputs.fqdn
url: containerApp.outputs.url
latestRevisionFqdn: containerApp.outputs.latestRevisionFqdn
latestRevisionUrl: containerApp.outputs.latestRevisionUrl
}