-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.bicep
304 lines (275 loc) · 9.81 KB
/
app.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
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'
var abbrs = loadJsonContent('../../abbreviations.json')
var roles = loadJsonContent('../../roles.json')
var resourceToken = toLower(uniqueString(subscription().id, workloadName, location))
var appResourceToken = toLower(uniqueString(subscription().id, workloadName, location, applicationName))
var containerRegistryName = '${abbrs.containers.containerRegistry}${resourceToken}'
resource containerRegistryRef 'Microsoft.ContainerRegistry/registries@2023-11-01-preview' existing = {
name: containerRegistryName
}
var applicationInsightsName = '${abbrs.managementGovernance.applicationInsights}${resourceToken}'
resource applicationInsightsRef 'Microsoft.Insights/components@2020-02-02' existing = {
name: applicationInsightsName
}
var storageAccountName = '${abbrs.storage.storageAccount}${resourceToken}'
resource storageAccountRef 'Microsoft.Storage/storageAccounts@2023-05-01' existing = {
name: storageAccountName
}
var aiServicesName = '${abbrs.ai.aiServices}${resourceToken}'
resource aiServicesRef 'Microsoft.CognitiveServices/accounts@2024-04-01-preview' existing = {
name: aiServicesName
}
var containerAppsEnvironmentName = '${abbrs.containers.containerAppsEnvironment}${resourceToken}'
resource containerAppsEnvironmentRef 'Microsoft.App/managedEnvironments@2024-03-01' existing = {
name: containerAppsEnvironmentName
}
var functionsWebJobStorageVariableName = 'AzureWebJobsStorage'
var documentsConnectionStringVariableName = 'AZURE_STORAGE_QUEUES_CONNECTION_STRING'
var applicationInsightsConnectionStringSecretName = 'applicationinsightsconnectionstring'
var applicationManagedIdentityName = '${abbrs.security.managedIdentity}${appResourceToken}'
module applicationManagedIdentity '../../security/managed-identity.bicep' = {
name: applicationManagedIdentityName
params: {
name: applicationManagedIdentityName
location: location
tags: union(tags, {})
}
}
resource acrPullRole 'Microsoft.Authorization/roleDefinitions@2022-04-01' 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-04-01' 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-04-01' 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
}
module aiServicesIdentityRoleAssignment '../../security/resource-role-assignment.json' = {
name: 'aiServicesIdentityRoleAssignment'
params: {
resourceId: aiServicesRef.id
roleAssignments: [
{
principalId: applicationManagedIdentity.outputs.principalId
roleDefinitionId: cognitiveServicesUserRole.id
principalType: 'ServicePrincipal'
}
{
principalId: applicationManagedIdentity.outputs.principalId
roleDefinitionId: cognitiveServicesOpenAIUserRole.id
principalType: 'ServicePrincipal'
}
]
}
}
var documentsQueueName = 'documents'
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: '${abbrs.containers.containerApp}${appResourceToken}'
params: {
name: '${abbrs.containers.containerApp}${appResourceToken}'
location: location
tags: union(tags, { App: 'ai-document-pipeline' })
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
}
]
environmentVariables: [
{
name: 'AzureWebJobsFeatureFlags'
value: 'EnableWorkerIndexing'
}
{
name: 'FUNCTIONS_EXTENSION_VERSION'
value: '~4'
}
{
name: 'FUNCTIONS_WORKER_RUNTIME'
value: 'python'
}
{
name: 'APPLICATIONINSIGHTS_CONNECTION_STRING'
secretRef: applicationInsightsConnectionStringSecretName
}
{
name: '${functionsWebJobStorageVariableName}__accountName'
value: storageAccountRef.name
}
{
name: '${functionsWebJobStorageVariableName}__credential'
value: 'managedidentity'
}
{
name: '${functionsWebJobStorageVariableName}__clientId'
value: applicationManagedIdentity.outputs.clientId
}
{
name: 'AZURE_CLIENT_ID'
value: applicationManagedIdentity.outputs.clientId
}
{
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'
}
]
}
}
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
}