forked from microsoft/azure-redcap-paas
-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.bicep
420 lines (372 loc) · 13 KB
/
main.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
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 = 'subscription'
@description('The Azure region to target for the deployment. Replaces {loc} in namingConvention.')
param location string = 'eastus'
@description('The environment designator for the deployment. Replaces {env} in namingConvention.')
@allowed([
'test'
'demo'
'prod'
])
param environment string = 'demo'
@description('The workload name. Replaces {workloadName} in namingConvention.')
param workloadName string = 'redcap'
@description('The Azure resource naming convention. Include the following placeholders (case-sensitive): {workloadName}, {env}, {rtype}, {loc}, {seq}.')
param namingConvention string = '{workloadName}-{env}-{rtype}-{loc}-{seq}'
@description('A sequence number for the deployment. Used to distinguish multiple deployed versions of the same workload. Replaces {seq} in namingConvention.')
@minValue(1)
@maxValue(99)
param sequence int = 1
@description('A valid Entra ID object ID, which will be assigned RBAC permissions on the deployed resources.')
param identityObjectId string
@description('The address space for the virtual network. Subnets will be carved out. Minimum IPv4 size: /24.')
param vnetAddressSpace string
@description('If available, the public URL to download the REDCap zip file from. Used for debugging purposes. Does not need to be specified when downloading from the REDCap community using a username and password.')
@secure()
param redcapZipUrl string = ''
@description('REDCap Community site username for downloading the REDCap zip file.')
@secure()
param redcapCommunityUsername string
@description('REDCap Community site password for downloading the REDCap zip file.')
@secure()
param redcapCommunityPassword string
@description('Github Repo URL where build scripts are downloaded from')
param scmRepoUrl string = 'https://github.com/microsoft/azure-redcap-paas'
@description('Github Repo Branch where build scripts are downloaded from')
param scmRepoBranch string = 'main'
@description('The command before build to be run on the web app with an elevated privilege. This is used to install the required packages for REDCap operation.')
param prerequisiteCommand string = '/home/startup.sh'
param deploymentTime string = utcNow()
@description('The password to use for the MySQL Flexible Server admin account \'sqladmin\'.')
@secure()
param sqlPassword string
@description('The MySQL Flexible Server admin user account name. Defaults to \'sqladmin\'.')
param sqlAdmin string = 'sqladmin'
@description('The outgoing SMTP server FQDN or IP address.')
param smtpFQDN string = ''
@description('The outgoing SMTP server port.')
param smtpPort string = ''
@description('The email address to use as the sender for outgoing emails.')
param smtpFromEmailAddress string = ''
var sequenceFormatted = format('{0:00}', sequence)
var rgNamingStructure = replace(replace(replace(replace(replace(namingConvention, '{rtype}', 'rg'), '{workloadName}', '${workloadName}-{rgName}'), '{loc}', location), '{seq}', sequenceFormatted), '{env}', environment)
var vnetName = nameModule[0].outputs.shortName
var strgName = nameModule[1].outputs.shortName
var webAppName = nameModule[2].outputs.shortName
var kvName = nameModule[3].outputs.shortName
var sqlName = nameModule[4].outputs.shortName
var planName = nameModule[5].outputs.shortName
var uamiName = nameModule[6].outputs.shortName
var dplscrName = nameModule[7].outputs.shortName
var lawName = nameModule[8].outputs.shortName
var deploymentNameStructure = '${workloadName}-${environment}-${sequenceFormatted}-{rtype}-${deploymentTime}'
var subnets = {
// TODO: Define securityRules
PrivateLinkSubnet: {
addressPrefix: cidrSubnet(vnetAddressSpace, 27, 0)
serviceEndpoints: [
{
service: 'Microsoft.KeyVault'
locations: [
location
]
}
{
service: 'Microsoft.Storage'
locations: [
location
]
}
]
}
ComputeSubnet: {
addressPrefix: cidrSubnet(vnetAddressSpace, 27, 1)
serviceEndpoints: [
{
service: 'Microsoft.KeyVault'
locations: [
location
]
}
{
service: 'Microsoft.Storage'
locations: [
location
]
}
{
service: 'Microsoft.Web'
locations: [
location
]
}
]
}
IntegrationSubnet: {
// Two /27 have already been created, which add up to a /26. This the second /26 (index = 1).
addressPrefix: cidrSubnet(vnetAddressSpace, 26, 1)
serviceEndpoints: [
{
service: 'Microsoft.KeyVault'
locations: [
location
]
}
{
service: 'Microsoft.Storage'
locations: [
location
]
}
{
service: 'Microsoft.Web'
locations: [
location
]
}
]
delegation: 'Microsoft.Web/serverFarms'
}
MySQLFlexSubnet: {
// TODO: /29 seems very small
// Two /26 have been allocated; that's equivalent to sixteen /29s.
addressPrefix: cidrSubnet(vnetAddressSpace, 29, 16)
serviceEndpoints: [
{
service: 'Microsoft.KeyVault'
locations: [
location
]
}
{
service: 'Microsoft.Storage'
locations: [
location
]
}
]
delegation: 'Microsoft.DBforMySQL/flexibleServers'
}
}
var tags = {
workload: workloadName
environment: environment
}
var secrets = {
sqlAdminName: mySqlModule.outputs.sqlAdmin
sqlPassword: sqlPassword
redcapCommunityUsername: redcapCommunityUsername
redcapCommunityPassword: redcapCommunityPassword
}
var resourceTypes = [
'vnet'
'st'
'app'
'kv'
'mysql'
'plan'
'uami'
'dplscr'
'law'
]
@batchSize(1)
module nameModule 'modules/common/createValidAzResourceName.bicep' = [for workload in resourceTypes: {
name: take(replace(deploymentNameStructure, '{rtype}', 'nameGen-${workload}'), 64)
params: {
location: location
environment: environment
namingConvention: namingConvention
resourceType: workload
sequence: sequence
workloadName: workloadName
addRandomChars: 4
}
}]
module rolesModule './modules/common/roles.bicep' = {
name: take(replace(deploymentNameStructure, '{rtype}', 'roles'), 64)
}
var storageAccountKeySecretName = 'storageKey'
// The secrets object is converted to an array using the items() function, which alphabetically sorts it
var defaultSecretNames = map(items(secrets), s => s.key)
var additionalSecretNames = [ storageAccountKeySecretName ]
var secretNames = concat(defaultSecretNames, additionalSecretNames)
// The output will be in alphabetical order
// LATER: Output an object instead
module kvSecretReferencesModule './modules/common/appSvcKeyVaultRefs.bicep' = {
name: take(replace(deploymentNameStructure, '{rtype}', 'kv-secrets'), 64)
params: {
keyVaultName: kvName
secretNames: secretNames
}
}
module virtualNetworkModule './modules/networking/main.bicep' = {
name: take(replace(deploymentNameStructure, '{rtype}', 'network'), 64)
params: {
resourceGroupName: replace(rgNamingStructure, '{rgName}', 'network')
virtualNetworkName: vnetName
vnetAddressPrefix: vnetAddressSpace
location: location
subnets: subnets
customDnsIPs: []
tags: tags
customTags: {
workloadType: 'networking'
}
deploymentNameStructure: deploymentNameStructure
}
}
module monitoring './modules/monitoring/main.bicep' = {
name: take(replace(deploymentNameStructure, '{rtype}', 'monitoring'), 64)
params: {
resourceGroupName: replace(rgNamingStructure, '{rgName}', 'monitoring')
appInsightsName: 'appInsights-${webAppName}'
logAnalyticsWorkspaceName: lawName
logAnalyticsWorkspaceSku: 'PerGB2018'
retentionInDays: 30
location: location
tags: tags
customTags: {
workloadType: 'monitoring'
}
deploymentNameStructure: deploymentNameStructure
}
}
module storageAccountModule './modules/storage/main.bicep' = {
name: take(replace(deploymentNameStructure, '{rtype}', 'storage'), 64)
params: {
resourceGroupName: replace(rgNamingStructure, '{rgName}', 'storage')
location: location
storageAccountName: strgName
peSubnetId: virtualNetworkModule.outputs.subnets.PrivateLinkSubnet.id
storageContainerName: 'redcap'
kind: 'StorageV2'
storageAccountSku: 'Standard_LRS'
virtualNetworkId: virtualNetworkModule.outputs.virtualNetworkId
privateDnsZoneName: 'privatelink.blob.${az.environment().suffixes.storage}'
tags: tags
customTags: {
workloadType: 'storageAccount'
}
deploymentNameStructure: deploymentNameStructure
keyVaultSecretName: storageAccountKeySecretName
keyVaultId: keyVaultModule.outputs.id
}
}
module keyVaultModule './modules/kv/main.bicep' = {
name: take(replace(deploymentNameStructure, '{rtype}', 'keyVault'), 64)
params: {
resourceGroupName: replace(rgNamingStructure, '{rgName}', 'keyVault')
keyVaultName: kvName
location: location
tags: tags
customTags: {
workloadType: 'keyVault'
}
peSubnetId: virtualNetworkModule.outputs.subnets.PrivateLinkSubnet.id
virtualNetworkId: virtualNetworkModule.outputs.virtualNetworkId
roleAssignments: [
{
RoleDefinitionId: rolesModule.outputs.roles['Key Vault Administrator']
objectId: identityObjectId
}
{
RoleDefinitionId: rolesModule.outputs.roles['Key Vault Secrets User']
objectId: uamiModule.outputs.principalId
}
]
privateDnsZoneName: 'privatelink.vaultcore.azure.net'
secrets: secrets
deploymentNameStructure: deploymentNameStructure
}
}
module mySqlModule './modules/sql/main.bicep' = {
name: take(replace(deploymentNameStructure, '{rtype}', 'mysql'), 64)
params: {
resourceGroupName: replace(rgNamingStructure, '{rgName}', 'database')
flexibleSqlServerName: sqlName
location: location
tags: tags
customTags: {
workloadType: 'mySqlFlexibleServer'
}
skuName: 'Standard_B1s'
SkuTier: 'Burstable'
StorageSizeGB: 20
StorageIops: 396
peSubnetId: virtualNetworkModule.outputs.subnets.MySQLFlexSubnet.id
privateDnsZoneName: 'privatelink.mysql.database.azure.com'
sqlAdminUser: sqlAdmin
sqlAdminPasword: sqlPassword
mysqlVersion: '8.0.21'
// TODO: Consider using workloadname + 'db'
databaseName: 'redcapdb'
roles: rolesModule.outputs.roles
uamiId: uamiModule.outputs.id
uamiPrincipalId: uamiModule.outputs.principalId
deploymentScriptName: dplscrName
// Required charset and collation for REDCap
database_charset: 'utf8'
database_collation: 'utf8_general_ci'
virtualNetworkId: virtualNetworkModule.outputs.virtualNetworkId
deploymentNameStructure: deploymentNameStructure
}
}
resource webAppResourceGroup 'Microsoft.Resources/resourceGroups@2023-07-01' = {
name: replace(rgNamingStructure, '{rgName}', 'web')
location: location
tags: union(tags, {
workloadType: 'web'
})
}
module webAppModule './modules/webapp/main.bicep' = {
name: take(replace(deploymentNameStructure, '{rtype}', 'appService'), 64)
scope: webAppResourceGroup
params: {
webAppName: webAppName
appServicePlanName: planName
location: location
// TODO: Consider deploying as P0V3 to ensure the deployment runs on a scale unit that supports P_v3 for future upgrades. GH issue #50
skuName: 'S1'
skuTier: 'Standard'
peSubnetId: virtualNetworkModule.outputs.subnets.PrivateLinkSubnet.id
appInsights_connectionString: monitoring.outputs.appInsightsResourceId
appInsights_instrumentationKey: monitoring.outputs.appInsightsInstrumentationKey
linuxFxVersion: 'php|8.2'
tags: tags
customTags: {
workloadType: 'webApp'
}
privateDnsZoneName: 'privatelink.azurewebsites.net'
virtualNetworkId: virtualNetworkModule.outputs.virtualNetworkId
redcapZipUrl: redcapZipUrl
dbHostName: mySqlModule.outputs.fqdn
dbName: mySqlModule.outputs.databaseName
dbUserNameSecretRef: kvSecretReferencesModule.outputs.keyVaultRefs[2]
dbPasswordSecretRef: kvSecretReferencesModule.outputs.keyVaultRefs[3]
redcapCommunityUsernameSecretRef: kvSecretReferencesModule.outputs.keyVaultRefs[1]
redcapCommunityPasswordSecretRef: kvSecretReferencesModule.outputs.keyVaultRefs[0]
storageAccountKeySecretRef: kvSecretReferencesModule.outputs.keyVaultRefs[4]
storageAccountContainerName: storageAccountModule.outputs.containerName
storageAccountName: storageAccountModule.outputs.name
// Enable VNet integration
integrationSubnetId: virtualNetworkModule.outputs.subnets.IntegrationSubnet.id
scmRepoUrl: scmRepoUrl
scmRepoBranch: scmRepoBranch
prerequisiteCommand: prerequisiteCommand
smtpFQDN: smtpFQDN
smtpFromEmailAddress: smtpFromEmailAddress
smtpPort: smtpPort
deploymentNameStructure: deploymentNameStructure
uamiId: uamiModule.outputs.id
}
}
module uamiModule 'modules/uami/main.bicep' = {
name: take(replace(deploymentNameStructure, '{rtype}', 'uami'), 64)
scope: webAppResourceGroup
params: {
tags: tags
location: location
uamiName: uamiName
}
}
// The web app URL
output webAppUrl string = webAppModule.outputs.webAppUrl