-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathdeploy.spoke.bicep
465 lines (404 loc) · 17 KB
/
deploy.spoke.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
targetScope = 'resourceGroup'
// reference to the BICEP naming module
param naming object
@description('Azure region where the resources will be deployed in')
param location string = resourceGroup().location
@description('Optional, default is false. Set to true if you want to deploy ASE v3 instead of Multitenant App Service Plan.')
param deployAseV3 bool = false
@description('CIDR of the SPOKE vnet i.e. 192.168.0.0/24')
param vnetSpokeAddressSpace string
@description('CIDR of the subnet that will hold the app services plan')
param subnetSpokeAppSvcAddressSpace string
@description('CIDR of the subnet that will hold devOps agents etc ')
param subnetSpokeDevOpsAddressSpace string
@description('CIDR of the subnet that will hold the private endpoints of the supporting services')
param subnetSpokePrivateEndpointAddressSpace string
@description('Internal IP of the Azure firewall deployed in Hub. Used for creating UDR to route all vnet egress traffic through Firewall. If empty no UDR')
param firewallInternalIp string = ''
@description('if empty, private dns zone will be deployed in the current RG scope')
param vnetHubResourceId string = ''
@description('Resource tags that we might need to add to all resources (i.e. Environment, Cost center, application name etc)')
param tags object
@description('Create (or not) a UDR for the App Service Subnet, to route all egress traffic through Hub Azure Firewall')
param enableEgressLockdown bool
@description('Deploy (or not) a redis cache')
param deployRedis bool
@description('Deploy (or not) an Azure SQL with default database ')
param deployAzureSql bool
@description('Deploy (or not) an Azure app configuration')
param deployAppConfig bool
@description('Deploy (or not) an Azure virtual machine (to be used as jumphost)')
param deployJumpHost bool
@description('Deploy (or not) an Azure OpenAI account. ATTENTION: At the time of writing this, OpenAI is in preview and only available in limited regions: look here: https://learn.microsoft.com/azure/ai-services/openai/chatgpt-quickstart#prerequisites')
param deployOpenAi bool
@description('Deploy (or not) a model on the openAI Account. This is used only as a sample to show how to deploy a model on the OpenAI account.')
param deployOpenAiGptModel bool = false
// post deployment specific parameters for the jumpBox
@description('The URL of the Github repository to use for the Github Actions Runner. This parameter is optional. If not provided, the Github Actions Runner will not be installed. If this parameter is provided, then github_token must also be provided.')
param githubRepository string = ''
@description('The token to use for the Github Actions Runner. This parameter is optional. If not provided, the Github Actions Runner will not be installed. If this parameter is provided, then github_repository must also be provided.')
param githubToken string = ''
@description('The URL of the Azure DevOps organization to use for the Azure DevOps Agent. This parameter is optional. If not provided, the Github Azure DevOps will not be installed. If this parameter is provided, then ado_token must also be provided.')
param adoOrganization string = ''
@description('The PAT token to use for the Azure DevOps Agent. This parameter is optional. If not provided, the Github Azure DevOps will not be installed. If this parameter is provided, then ado_organization must also be provided.')
param adoToken string = ''
@description('A switch to indicate whether or not to install the Azure CLI, AZD CLI and git. This parameter is optional. If not provided, the Azure CLI, AZD CLI and git will not be installed')
param installClis bool = false
@description('A switch to indicate whether or not to install Sql Server Management Studio (SSMS). This parameter is optional. If not provided, SSMS will not be installed.')
param installSsms bool = false
@description('Optional S1 is default. Defines the name, tier, size, family and capacity of the App Service Plan. Plans ending to _AZ, are deploying at least three instances in three Availability Zones. EP* is only for functions')
@allowed([ 'S1', 'S2', 'S3', 'P1V3', 'P2V3', 'P3V3', 'P1V3_AZ', 'P2V3_AZ', 'P3V3_AZ', 'EP1', 'EP2', 'EP3', 'ASE_I1V2_AZ', 'ASE_I2V2_AZ', 'ASE_I3V2_AZ', 'ASE_I1V2', 'ASE_I2V2', 'ASE_I3V2' ])
param webAppPlanSku string
@description('Kind of server OS of the App Service Plan')
@allowed([ 'Windows', 'Linux'])
param webAppBaseOs string
@description('optional, default value is azureuser')
param adminUsername string
@description('mandatory, the password of the admin user')
@secure()
param adminPassword string
@description('Conditional. The Microsoft Entra ID administrator authentication. Required if no `sqlAdminLogin` & `sqlAdminPassword` is provided.')
param sqlServerAdministrators object = {}
@description('Conditional. If sqlServerAdministrators is given, this is not required')
param sqlAdminLogin string = ''
@description('Conditional. If sqlServerAdministrators is given, this is not required')
@secure()
param sqlAdminPassword string = ''
@description('set to true if you want to auto approve the Private Endpoint of the AFD')
param autoApproveAfdPrivateEndpoint bool = true
var resourceNames = {
storageAccount: naming.storageAccount.nameUnique
vnetSpoke: take('${naming.virtualNetwork.name}-spoke', 80)
snetAppSvc: 'snet-appSvc-${naming.virtualNetwork.name}-spoke'
snetDevOps: 'snet-devOps-${naming.virtualNetwork.name}-spoke'
snetPe: 'snet-pe-${naming.virtualNetwork.name}-spoke'
pepNsg: take('${naming.networkSecurityGroup.name}-pep', 80)
aseNsg: take('${naming.networkSecurityGroup.name}-ase', 80)
appSvcUserAssignedManagedIdentity: take('${naming.userAssignedManagedIdentity.name}-appSvc', 128)
vmJumpHostUserAssignedManagedIdentity: take('${naming.userAssignedManagedIdentity.name}-vmJumpHost', 128)
keyvault: naming.keyVault.nameUnique
logAnalyticsWs: naming.logAnalyticsWorkspace.name
appInsights: naming.applicationInsights.name
aseName: naming.appServiceEnvironment.nameUnique
aspName: naming.appServicePlan.name
webApp: naming.appService.nameUnique
vmWindowsJumpbox: take('${naming.windowsVirtualMachine.name}-win-jumpbox', 64)
redisCache: naming.redisCache.nameUnique
sqlServer: naming.mssqlServer.nameUnique
sqlDb:'sample-db'
appConfig: take ('${naming.appConfiguration.nameUnique}-${ take( uniqueString(resourceGroup().id, subscription().id), 6) }', 50)
frontDoor: naming.frontDoor.name
frontDoorEndPoint: 'webAppLza-${ take( uniqueString(resourceGroup().id, subscription().id), 6) }' //globally unique
frontDoorWaf: naming.frontDoorFirewallPolicy.name
routeTable: naming.routeTable.name
routeEgressLockdown: '${naming.route.name}-egress-lockdown'
idAfdApprovePeAutoApprover: take('${naming.userAssignedManagedIdentity.name}-AfdApprovePe', 128)
openAiAccount: naming.cognitiveAccount.nameUnique
openAiDeployment: naming.openAiDeployment.name
}
var udrRoutes = [
{
name: 'defaultEgressLockdown'
properties: {
addressPrefix: '0.0.0.0/0'
nextHopIpAddress: firewallInternalIp
nextHopType: 'VirtualAppliance'
}
}
]
var subnets = [
{
name: resourceNames.snetAppSvc
properties: {
addressPrefix: subnetSpokeAppSvcAddressSpace
privateEndpointNetworkPolicies: !(deployAseV3) ? 'Enabled' : 'Disabled'
delegations: [
{
name: 'delegation'
properties: {
serviceName: !(deployAseV3) ? 'Microsoft.Web/serverfarms' : 'Microsoft.Web/hostingEnvironments'
}
}
]
networkSecurityGroup: {
id: !(deployAseV3) ? nsgPep.outputs.nsgId : nsgAse.outputs.nsgId
}
routeTable: !empty(firewallInternalIp) && (enableEgressLockdown) ? {
id: routeTableToFirewall.outputs.resourceId
} : null
}
}
{
name: resourceNames.snetDevOps
properties: {
addressPrefix: subnetSpokeDevOpsAddressSpace
privateEndpointNetworkPolicies: 'Enabled'
networkSecurityGroup: {
id: nsgPep.outputs.nsgId
}
}
}
{
name: resourceNames.snetPe
properties: {
addressPrefix: subnetSpokePrivateEndpointAddressSpace
privateEndpointNetworkPolicies: 'Disabled'
networkSecurityGroup: {
id: nsgPep.outputs.nsgId
}
}
}
]
var virtualNetworkLinks = [
{
vnetName: vnetSpoke.outputs.vnetName
vnetId: vnetSpoke.outputs.vnetId
registrationEnabled: false
}
{
vnetName: vnetHub.name
vnetId: vnetHub.id
registrationEnabled: false
}
]
var vnetHubSplitTokens = !empty(vnetHubResourceId) ? split(vnetHubResourceId, '/') : array('')
resource vnetHub 'Microsoft.Network/virtualNetworks@2022-07-01' existing = {
scope: resourceGroup(vnetHubSplitTokens[2], vnetHubSplitTokens[4])
name: vnetHubSplitTokens[8]
}
module vnetSpoke '../../shared/bicep/network/vnet.bicep' = {
name: 'vnetSpoke-Deployment'
params: {
name: resourceNames.vnetSpoke
location: location
tags: tags
vnetAddressSpace: vnetSpokeAddressSpace
subnetsInfo: subnets
}
}
module routeTableToFirewall '../../shared/bicep/network/udr.bicep' = if (!empty(firewallInternalIp) && (enableEgressLockdown) ) {
name: 'routeTableToFirewall-Deployment'
params: {
name: resourceNames.routeTable
location: location
tags: tags
routes: udrRoutes
}
}
@description('NSG Rules for the private enpoint subnet.')
module nsgPep '../../shared/bicep/network/nsg.bicep' = {
name: take('nsgPep-${deployment().name}', 64)
params: {
name: resourceNames.pepNsg
location: location
tags: tags
securityRules: []
diagnosticWorkspaceId: logAnalyticsWs.outputs.logAnalyticsWsId
}
}
@description('NSG Rules for the private enpoint subnet.')
module nsgAse '../../shared/bicep/network/nsg.bicep' = {
name: take('nsgAse-${deployment().name}', 64)
params: {
name: resourceNames.aseNsg
location: location
tags: tags
securityRules: [
{
name: 'SSL_WEB_443'
properties: {
direction: 'Inbound'
access: 'Allow'
protocol: 'Tcp'
sourceAddressPrefix: '*'
sourcePortRange: '*'
destinationAddressPrefix: '*'
destinationPortRange: '443'
priority: 100
}
}
]
diagnosticWorkspaceId: logAnalyticsWs.outputs.logAnalyticsWsId
}
}
resource snetAppSvc 'Microsoft.Network/virtualNetworks/subnets@2022-07-01' existing = {
name: '${vnetSpoke.outputs.vnetName}/${resourceNames.snetAppSvc}'
}
resource snetDevOps 'Microsoft.Network/virtualNetworks/subnets@2022-07-01' existing = {
name: '${vnetSpoke.outputs.vnetName}/${resourceNames.snetDevOps}'
}
resource snetPe 'Microsoft.Network/virtualNetworks/subnets@2022-07-01' existing = {
name: '${vnetSpoke.outputs.vnetName}/${resourceNames.snetPe}'
}
module logAnalyticsWs '../../shared/bicep/log-analytics-ws.bicep' = {
name: 'logAnalyticsWs-Deployment'
params: {
name: resourceNames.logAnalyticsWs
location: location
tags: tags
}
}
module keyvault 'modules/keyvault.module.bicep' = {
name: take('${resourceNames.keyvault}-keyvaultModule-Deployment', 64)
params: {
name: resourceNames.keyvault
location: location
tags: tags
vnetHubResourceId: vnetHubResourceId
subnetPrivateEndpointId: snetPe.id
virtualNetworkLinks: virtualNetworkLinks
}
}
module webApp 'modules/app-service.module.bicep' = {
name: 'webAppModule-Deployment'
params: {
deployAseV3: deployAseV3
aseName: resourceNames.aseName
appServicePlanName: resourceNames.aspName
webAppName: resourceNames.webApp
managedIdentityName: resourceNames.appSvcUserAssignedManagedIdentity
location: location
logAnalyticsWsId: logAnalyticsWs.outputs.logAnalyticsWsId
subnetIdForVnetInjection: snetAppSvc.id
tags: tags
vnetHubResourceId: vnetHubResourceId
webAppBaseOs: webAppBaseOs
subnetPrivateEndpointId: snetPe.id
virtualNetworkLinks: virtualNetworkLinks
appConfigurationName: resourceNames.appConfig
sku: webAppPlanSku
keyvaultName: keyvault.outputs.keyvaultName
//docs for envintoment(): > https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-functions-deployment#example-1
sqlDbConnectionString: (deployAzureSql) ? 'Server=tcp:${sqlServerAndDefaultDb.outputs.sqlServerName}${environment().suffixes.sqlServerHostname};Authentication=Active Directory Default;Database=${resourceNames.sqlDb};' : ''
redisConnectionStringSecretName: (deployRedis) ? redisCache.outputs.redisConnectionStringSecretName : ''
deployAppConfig: deployAppConfig
}
}
module asePrivateDnsZone '../../shared/bicep/private-dns-zone.bicep' = if ( deployAseV3 ) {
scope: resourceGroup(vnetHubSplitTokens[2], vnetHubSplitTokens[4]) //let the Private DNS zone in the same spoke network as the ASE v3 - for testing
name: 'asev3-hub-PrivateDnsZone-Deployment'
params: {
name: deployAseV3 ? '${webApp.outputs.aseName}.appserviceenvironment.net' : ''
virtualNetworkLinks: virtualNetworkLinks
tags: tags
aRecords: [
{
name: '*'
ipv4Address: deployAseV3 ? webApp.outputs.internalInboundIpAddress : ''
ttl: 3600
}
{
name: '*.scm'
ipv4Address: deployAseV3 ? webApp.outputs.internalInboundIpAddress : ''
ttl: 3600
}
{
name: '@'
ipv4Address: deployAseV3 ? webApp.outputs.internalInboundIpAddress : ''
ttl: 3600
}
]
}
dependsOn: [
webApp
]
}
module afd '../../shared/bicep/network/front-door.bicep' = {
name: take ('AzureFrontDoor-${resourceNames.frontDoor}-deployment', 64)
params: {
afdName: resourceNames.frontDoor
diagnosticWorkspaceId: logAnalyticsWs.outputs.logAnalyticsWsId
endpointName: resourceNames.frontDoorEndPoint
originGroupName: resourceNames.frontDoorEndPoint
origins: [
{
name: webApp.outputs.webAppName //1-50 Alphanumerics and hyphens
hostname: webApp.outputs.webAppHostName
enabledState: true
privateLinkOrigin: {
privateEndpointResourceId: webApp.outputs.webAppResourceId
privateLinkResourceType: 'sites'
privateEndpointLocation: webApp.outputs.webAppLocation
}
}
]
skuName:'Premium_AzureFrontDoor'
wafPolicyName: resourceNames.frontDoorWaf
}
}
module autoApproveAfdPe 'modules/approve-afd-pe.module.bicep' = if (autoApproveAfdPrivateEndpoint) {
name: take ('autoApproveAfdPe-${resourceNames.frontDoor}-deployment', 64)
params: {
location: location
idAfdPeAutoApproverName: resourceNames.idAfdApprovePeAutoApprover
}
dependsOn: [
afd
]
}
module vmWindowsModule 'modules/vmJumphost.module.bicep' = if (deployJumpHost) {
name: 'vmWindowsModule-Deployment'
params: {
adminPassword: adminPassword
adminUsername: adminUsername
location: location
tags: tags
vmJumpHostUserAssignedManagedIdentityName: resourceNames.vmJumpHostUserAssignedManagedIdentity
vmWindowsJumpboxName: resourceNames.vmWindowsJumpbox
keyvaultName: keyvault.outputs.keyvaultName
appConfigStoreId: webApp.outputs.appConfigStoreId
subnetDevOpsId: snetDevOps.id
githubRepository: githubRepository
githubToken: githubToken
adoOrganization: adoOrganization
adoToken: adoToken
installClis: installClis
installSsms: installSsms
}
}
module redisCache 'modules/redis.module.bicep' = if (deployRedis) {
name: take('${resourceNames.redisCache}-redisModule-Deployment', 64)
params: {
name: resourceNames.redisCache
location: location
tags: tags
logAnalyticsWsId: logAnalyticsWs.outputs.logAnalyticsWsId
vnetHubResourceId: vnetHubResourceId
subnetPrivateEndpointId: snetPe.id
virtualNetworkLinks: virtualNetworkLinks
keyvaultName: keyvault.outputs.keyvaultName
}
}
//TODO: conditional deployment of SQL
module sqlServerAndDefaultDb 'modules/sql-database.module.bicep' = if (deployAzureSql) {
name: take('${resourceNames.sqlServer}-sqlServer-Deployment', 64)
params: {
name: resourceNames.sqlServer
databaseName: resourceNames.sqlDb
location: location
tags: tags
vnetHubResourceId: vnetHubResourceId
subnetPrivateEndpointId: snetPe.id
virtualNetworkLinks: virtualNetworkLinks
administrators: sqlServerAdministrators
sqlAdminLogin: sqlAdminLogin
sqlAdminPassword: sqlAdminPassword
}
}
module openAi 'modules/open-ai.module.bicep'= if(deployOpenAi) {
name: take('${resourceNames.openAiAccount}-openAiModule-Deployment', 64)
params: {
name: resourceNames.openAiAccount
deploymentName: resourceNames.openAiDeployment
location: location
tags: tags
vnetHubResourceId: vnetHubResourceId
subnetPrivateEndpointId: snetPe.id
virtualNetworkLinks: virtualNetworkLinks
logAnalyticsWsId: logAnalyticsWs.outputs.logAnalyticsWsId
deployOpenAiGptModel: deployOpenAiGptModel
}
}
output vnetSpokeName string = vnetSpoke.outputs.vnetName
output vnetSpokeId string = vnetSpoke.outputs.vnetId
//output sampleAppIngress string = webApp.outputs.fqdn