-
Notifications
You must be signed in to change notification settings - Fork 20
/
src.bicep
266 lines (238 loc) · 7.5 KB
/
src.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
param name string
param location string = resourceGroup().location
param tags object = {}
param identityName string
param containerRegistryName string
param containerAppsEnvironmentName string
param applicationInsightsName string
param exists bool
param azureOpenaiResourceName string = 'dream'
param azureOpenaiDeploymentName string = 'gpt-4o'
param azureOpenaiDeploymentNameMini string = 'gpt-4o-mini'
param dailyRateLimit int = 1000000 // Set your daily rate limit here
@description('Custom subdomain name for the OpenAI resource (must be unique in the region)')
param customSubDomainName string
@secure()
param appDefinition object
@description('Principal ID of the user executing the deployment')
param userPrincipalId string
var appSettingsArray = filter(array(appDefinition.settings), i => i.name != '')
var secrets = map(filter(appSettingsArray, i => i.?secret != null), i => {
name: i.name
value: i.value
secretRef: i.?secretRef ?? take(replace(replace(toLower(i.name), '_', '-'), '.', '-'), 32)
})
var env = map(filter(appSettingsArray, i => i.?secret == null), i => {
name: i.name
value: i.value
})
resource identity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
name: identityName
location: location
}
resource containerRegistry 'Microsoft.ContainerRegistry/registries@2023-01-01-preview' existing = {
name: containerRegistryName
}
resource containerAppsEnvironment 'Microsoft.App/managedEnvironments@2023-05-01' existing = {
name: containerAppsEnvironmentName
}
resource applicationInsights 'Microsoft.Insights/components@2020-02-02' existing = {
name: applicationInsightsName
}
resource acrPullRole 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
scope: containerRegistry
name: guid(subscription().id, resourceGroup().id, identity.id, 'acrPullRole')
properties: {
roleDefinitionId: subscriptionResourceId(
'Microsoft.Authorization/roleDefinitions', '7f951dda-4ed3-4680-a7ca-43fe172d538d')
principalType: 'ServicePrincipal'
principalId: identity.properties.principalId
}
}
module fetchLatestImage '../modules/fetch-container-image.bicep' = {
name: '${name}-fetch-image'
params: {
exists: exists
name: name
}
}
resource app 'Microsoft.App/containerApps@2023-05-02-preview' = {
name: name
location: location
tags: union(tags, {'azd-service-name': 'src' })
dependsOn: [ acrPullRole ]
identity: {
type: 'UserAssigned'
userAssignedIdentities: { '${identity.id}': {} }
}
properties: {
managedEnvironmentId: containerAppsEnvironment.id
configuration: {
ingress: {
external: true
targetPort: 80
transport: 'auto'
}
registries: [
{
server: '${containerRegistryName}.azurecr.io'
identity: identity.id
}
]
secrets: union([
],
map(secrets, secret => {
name: secret.secretRef
value: secret.value
}))
}
template: {
containers: [
{
image: fetchLatestImage.outputs.?containers[?0].?image ?? 'mcr.microsoft.com/azuredocs/containerapps-helloworld:latest'
name: 'main'
env: union([
{
name: 'APPLICATIONINSIGHTS_CONNECTION_STRING'
value: applicationInsights.properties.ConnectionString
}
{
name: 'AZURE_OPENAI_ENDPOINT'
value: openai.properties.endpoint
}
{
name: 'POOL_MANAGEMENT_ENDPOINT'
value: dynamicsession.properties.poolManagementEndpoint
}
{
name: 'AZURE_CLIENT_ID'
value: identity.properties.clientId
}
{
name: 'PORT'
value: '80'
}
],
env,
map(secrets, secret => {
name: secret.name
secretRef: secret.secretRef
}))
resources: {
cpu: json('2.0')
memory: '4.0Gi'
}
}
]
scale: {
minReplicas: 1
maxReplicas: 10
}
}
}
}
resource openai 'Microsoft.CognitiveServices/accounts@2024-10-01' = {
name: azureOpenaiResourceName
location: location
sku: {
name: 'S0'
}
kind: 'OpenAI'
properties: {
customSubDomainName: customSubDomainName
dailyRateLimit: dailyRateLimit
}
}
// Define the OpenAI deployment
resource openaideployment 'Microsoft.CognitiveServices/accounts/deployments@2024-10-01' = {
name: azureOpenaiDeploymentName
parent: openai
sku: {
name: 'GlobalStandard'
capacity: 30
}
properties: {
model: {
name: 'gpt-4o'
format: 'OpenAI'
version: '2024-11-20'
}
versionUpgradeOption: 'OnceCurrentVersionExpired'
}
}
resource openaideploymentmini 'Microsoft.CognitiveServices/accounts/deployments@2024-10-01' = {
name: azureOpenaiDeploymentNameMini
parent: openai
sku: {
name: 'GlobalStandard'
capacity: 30
}
properties: {
model: {
name: 'gpt-4o-mini'
format: 'OpenAI'
version: '2024-07-18'
}
versionUpgradeOption: 'OnceCurrentVersionExpired'
}
dependsOn: [openaideployment]
}
resource dynamicsession 'Microsoft.App/sessionPools@2024-02-02-preview' = {
name: 'sessionPool'
location: location
tags: {
tagName1: 'tagValue1'
}
properties: {
containerType: 'PythonLTS'
dynamicPoolConfiguration: {
cooldownPeriodInSeconds: 300
executionType: 'Timed'
}
poolManagementType: 'Dynamic'
scaleConfiguration: {
maxConcurrentSessions: 20
readySessionInstances: 2
}
}
}
resource userSessionPoolRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(dynamicsession.id, userPrincipalId, 'Azure Container Apps Session Executor')
scope: dynamicsession
properties: {
principalId: userPrincipalId
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '0fb8eba5-a2bb-4abe-b1c1-49dfad359bb0')
}
}
resource appSessionPoolRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(dynamicsession.id, identity.id, 'Azure Container Apps Session Executor')
scope: dynamicsession
properties: {
principalId: identity.properties.principalId
principalType: 'ServicePrincipal'
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '0fb8eba5-a2bb-4abe-b1c1-49dfad359bb0')
}
}
resource userOpenaiRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(openai.id, userPrincipalId, 'Cognitive Services OpenAI User')
scope: openai
properties: {
principalId: userPrincipalId
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '5e0bd9bd-7b93-4f28-af87-19fc36ad61bd')
}
}
resource appOpenaiRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(openai.id, identity.id, 'Cognitive Services OpenAI User')
scope: openai
properties: {
principalId: identity.properties.principalId
principalType: 'ServicePrincipal'
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '5e0bd9bd-7b93-4f28-af87-19fc36ad61bd')
}
}
output defaultDomain string = containerAppsEnvironment.properties.defaultDomain
output name string = app.name
output uri string = 'https://${app.properties.configuration.ingress.fqdn}'
output id string = app.id
output azure_endpoint string = openai.properties.endpoint
output pool_endpoint string = dynamicsession.properties.poolManagementEndpoint