|
| 1 | +// Parameters |
| 2 | +param stackName string |
| 3 | + |
| 4 | +param storageAccountName string |
| 5 | +param storageKeySecretName string |
| 6 | +param sasDefinitionName string |
| 7 | + |
| 8 | +param keyVaultName string |
| 9 | + |
| 10 | +param websitePlanId string |
| 11 | +param websiteAadClientId string |
| 12 | +param websiteConfig array |
| 13 | + |
| 14 | +param workerConfig array |
| 15 | +@allowed([ |
| 16 | + 'Warning' |
| 17 | + 'Information' |
| 18 | +]) |
| 19 | +param workerLogLevel string = 'Warning' |
| 20 | +@minValue(1) |
| 21 | +param workerCount int |
| 22 | +param existingWorkerCount int |
| 23 | + |
| 24 | +// Shared resources |
| 25 | +resource insights 'Microsoft.Insights/components@2015-05-01' = { |
| 26 | + name: 'ExplorePackages-${stackName}' |
| 27 | + location: resourceGroup().location |
| 28 | + kind: 'web' |
| 29 | + properties: { |
| 30 | + Application_Type: 'web' |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +resource storageAccount 'Microsoft.Storage/storageAccounts@2019-06-01' existing = { |
| 35 | + name: storageAccountName |
| 36 | +} |
| 37 | + |
| 38 | +// Cannot use a KeyVault reference for initial deployment. |
| 39 | +// https://github.com/Azure/azure-functions-host/issues/7094 |
| 40 | +var storageSecretValue = 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};AccountKey=${listkeys(storageAccount.id, storageAccount.apiVersion).keys[0].value};EndpointSuffix=core.windows.net' |
| 41 | +var storageSecretReference = '@Microsoft.KeyVault(VaultName=${keyVaultName};SecretName=${storageKeySecretName})' |
| 42 | +var workerSecret = existingWorkerCount >= workerCount ? storageSecretReference : storageSecretValue |
| 43 | + |
| 44 | +output needsAnotherDeploy bool = workerSecret != storageSecretReference |
| 45 | +output websiteDefaultHostName string = website.properties.defaultHostName |
| 46 | +output websiteHostNames array = website.properties.hostNames |
| 47 | +output websiteId string = website.id |
| 48 | +output workerIds array = [for i in range(0, workerCount): workers[i].id] |
| 49 | + |
| 50 | +var sharedConfig = [ |
| 51 | + { |
| 52 | + name: 'APPINSIGHTS_INSTRUMENTATIONKEY' |
| 53 | + value: insights.properties.InstrumentationKey |
| 54 | + } |
| 55 | + { |
| 56 | + name: 'APPLICATIONINSIGHTS_CONNECTION_STRING' |
| 57 | + value: insights.properties.ConnectionString |
| 58 | + } |
| 59 | + { |
| 60 | + name: 'ApplicationInsightsAgent_EXTENSION_VERSION' |
| 61 | + value: '~2' |
| 62 | + } |
| 63 | + { |
| 64 | + name: 'Knapcode.ExplorePackages:StorageAccountName' |
| 65 | + value: storageAccountName |
| 66 | + } |
| 67 | + { |
| 68 | + name: 'Knapcode.ExplorePackages:StorageSharedAccessSignature' |
| 69 | + value: '@Microsoft.KeyVault(VaultName=${keyVaultName};SecretName=${storageAccountName}-${sasDefinitionName})' |
| 70 | + } |
| 71 | + { |
| 72 | + name: 'WEBSITE_RUN_FROM_PACKAGE' |
| 73 | + value: '1' |
| 74 | + } |
| 75 | +] |
| 76 | + |
| 77 | +resource keyVault 'Microsoft.KeyVault/vaults@2019-09-01' = { |
| 78 | + name: keyVaultName |
| 79 | + location: resourceGroup().location |
| 80 | + properties: { |
| 81 | + tenantId: subscription().tenantId |
| 82 | + sku: { |
| 83 | + family: 'A' |
| 84 | + name: 'standard' |
| 85 | + } |
| 86 | + accessPolicies: [for i in range(0, workerCount + 1): { |
| 87 | + tenantId: i == 0 ? website.identity.tenantId : workers[i - 1].identity.tenantId |
| 88 | + objectId: i == 0 ? website.identity.principalId : workers[i - 1].identity.principalId |
| 89 | + permissions: { |
| 90 | + secrets: [ |
| 91 | + 'get' |
| 92 | + ] |
| 93 | + } |
| 94 | + }] |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +resource keyVaultDiagnostics 'microsoft.insights/diagnosticSettings@2017-05-01-preview' = { |
| 99 | + scope: keyVault |
| 100 | + name: '${keyVaultName}-diagnostics' |
| 101 | + properties: { |
| 102 | + storageAccountId: storageAccount.id |
| 103 | + logs: [ |
| 104 | + { |
| 105 | + category: 'AuditEvent' |
| 106 | + enabled: true |
| 107 | + retentionPolicy: { |
| 108 | + enabled: true |
| 109 | + days: 30 |
| 110 | + } |
| 111 | + } |
| 112 | + ] |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// Website |
| 117 | +resource website 'Microsoft.Web/sites@2020-09-01' = { |
| 118 | + name: 'ExplorePackages-${stackName}' |
| 119 | + location: resourceGroup().location |
| 120 | + identity: { |
| 121 | + type: 'SystemAssigned' |
| 122 | + } |
| 123 | + properties: { |
| 124 | + serverFarmId: websitePlanId |
| 125 | + clientAffinityEnabled: false |
| 126 | + httpsOnly: true |
| 127 | + siteConfig: { |
| 128 | + webSocketsEnabled: true |
| 129 | + minTlsVersion: '1.2' |
| 130 | + netFrameworkVersion: 'v5.0' |
| 131 | + appSettings: concat([ |
| 132 | + { |
| 133 | + name: 'AzureAd:Instance' |
| 134 | + value: 'https://login.microsoftonline.com/' |
| 135 | + } |
| 136 | + { |
| 137 | + name: 'AzureAd:ClientId' |
| 138 | + value: websiteAadClientId |
| 139 | + } |
| 140 | + { |
| 141 | + name: 'AzureAd:TenantId' |
| 142 | + value: 'common' |
| 143 | + } |
| 144 | + ], sharedConfig, websiteConfig) |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// Workers |
| 150 | +resource workerPlan 'Microsoft.Web/serverfarms@2020-09-01' = { |
| 151 | + name: 'ExplorePackages-${stackName}-WorkerPlan' |
| 152 | + location: resourceGroup().location |
| 153 | + sku: { |
| 154 | + name: 'Y1' |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +resource workers 'Microsoft.Web/sites@2020-09-01' = [for i in range(0, workerCount): { |
| 159 | + name: 'ExplorePackages-${stackName}-Worker-${i}' |
| 160 | + location: resourceGroup().location |
| 161 | + kind: 'FunctionApp' |
| 162 | + identity: { |
| 163 | + type: 'SystemAssigned' |
| 164 | + } |
| 165 | + properties: { |
| 166 | + serverFarmId: workerPlan.id |
| 167 | + clientAffinityEnabled: false |
| 168 | + httpsOnly: true |
| 169 | + siteConfig: { |
| 170 | + minTlsVersion: '1.2' |
| 171 | + appSettings: concat([ |
| 172 | + { |
| 173 | + name: 'AzureFunctionsJobHost__logging__LogLevel__Default' |
| 174 | + value: workerLogLevel |
| 175 | + } |
| 176 | + { |
| 177 | + name: 'AzureWebJobsFeatureFlags' |
| 178 | + value: 'EnableEnhancedScopes' |
| 179 | + } |
| 180 | + { |
| 181 | + name: 'AzureWebJobsStorage' |
| 182 | + value: workerSecret |
| 183 | + } |
| 184 | + { |
| 185 | + name: 'FUNCTIONS_EXTENSION_VERSION' |
| 186 | + value: '~3' |
| 187 | + } |
| 188 | + { |
| 189 | + name: 'FUNCTIONS_WORKER_RUNTIME' |
| 190 | + value: 'dotnet' |
| 191 | + } |
| 192 | + { |
| 193 | + name: 'SCM_DO_BUILD_DURING_DEPLOYMENT' |
| 194 | + value: 'false' |
| 195 | + } |
| 196 | + { |
| 197 | + name: 'WEBSITE_CONTENTAZUREFILECONNECTIONSTRING' |
| 198 | + value: workerSecret |
| 199 | + } |
| 200 | + ], sharedConfig, workerConfig) |
| 201 | + } |
| 202 | + } |
| 203 | +}] |
0 commit comments