-
Notifications
You must be signed in to change notification settings - Fork 170
/
host-containerapp.bicept
205 lines (195 loc) · 5.42 KB
/
host-containerapp.bicept
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
{{define "host-containerapp.bicep" -}}
param name string
param location string = resourceGroup().location
param tags object = {}
param identityName string
param containerRegistryName string
param containerAppsEnvironmentName string
param applicationInsightsName string
{{- if .DbCosmosMongo}}
@secure()
param cosmosDbConnectionString string
{{- end}}
{{- if .DbPostgres}}
param databaseHost string
param databaseUser string
param databaseName string
@secure()
param databasePassword string
{{- end}}
{{- if (and .Frontend .Frontend.Backends)}}
param apiUrls array
{{- end}}
{{- if (and .Backend .Backend.Frontends)}}
param allowedOrigins array
{{- end}}
param exists bool
@secure()
param appDefinition object
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@2022-02-01-preview' existing = {
name: containerRegistryName
}
resource containerAppsEnvironment 'Microsoft.App/managedEnvironments@2023-04-01-preview' 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-04-01-preview' = {
name: name
location: location
tags: union(tags, {'azd-service-name': '{{.Name}}' })
dependsOn: [ acrPullRole ]
identity: {
type: 'UserAssigned'
userAssignedIdentities: { '${identity.id}': {} }
}
properties: {
managedEnvironmentId: containerAppsEnvironment.id
configuration: {
{{- if ne .Port 0}}
ingress: {
external: true
targetPort: {{.Port}}
transport: 'auto'
{{- if (and .Backend .Backend.Frontends)}}
corsPolicy: {
allowedOrigins: union(allowedOrigins, [
// define additional allowed origins here
])
}
{{- end}}
}
{{- end}}
registries: [
{
server: '${containerRegistryName}.azurecr.io'
identity: identity.id
}
]
secrets: union([
{{- if .DbCosmosMongo}}
{
name: 'azure-cosmos-connection-string'
value: cosmosDbConnectionString
}
{{- end}}
{{- if .DbPostgres}}
{
name: 'db-pass'
value: databasePassword
}
{{- end}}
],
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
}
{{- if .DbCosmosMongo}}
{
name: 'AZURE_COSMOS_MONGODB_CONNECTION_STRING'
secretRef: 'azure-cosmos-connection-string'
}
{{- end}}
{{- if .DbPostgres}}
{
name: 'DB_HOST'
value: databaseHost
}
{
name: 'DB_USER'
value: databaseUser
}
{
name: 'DB_NAME'
value: databaseName
}
{
name: 'DB_PASS'
secretRef: 'db-pass'
}
{
name: 'DB_PORT'
value: '5432'
}
{{- end}}
{{- if .Frontend}}
{{- range $i, $e := .Frontend.Backends}}
{
name: '{{upper .Name}}_BASE_URL'
value: apiUrls[{{$i}}]
}
{{- end}}
{{- end}}
{{- if ne .Port 0}}
{
name: 'PORT'
value: '{{ .Port }}'
}
{{- end}}
],
env,
map(secrets, secret => {
name: secret.name
secretRef: secret.secretRef
}))
resources: {
cpu: json('1.0')
memory: '2.0Gi'
}
}
]
scale: {
minReplicas: 1
maxReplicas: 10
}
}
}
}
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
{{ end}}