-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into release-please--branches--main
- Loading branch information
Showing
11 changed files
with
185 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// This function generates a unique string based on the subscription ID and resource group ID | ||
@export() | ||
func uniqueStringBySubscriptionAndResourceGroup() string => uniqueString('${subscription().id}${resourceGroup().id}') | ||
|
||
// This function generates a unique resource name by appending a unique string to the given name, ensuring the total length does not exceed the specified limit. | ||
// It also ensures that the name is always postfixed with the full length of the unique string, which is 13 characters plus a dash. | ||
// Example: | ||
// uniqueResourceName(name: 'my-resource', limit: 50) => 'my-resource-1234567890123' | ||
// Example: | ||
// uniqueResourceName(name: 'my-resource', limit: 20) => 'my-res-1234567890123' | ||
@export() | ||
func uniqueResourceName(name string, limit int) string => | ||
'${take(name, limit - 13 - 1)}-${uniqueStringBySubscriptionAndResourceGroup()}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,138 +1,178 @@ | ||
import { uniqueStringBySubscriptionAndResourceGroup, uniqueResourceName } from '../../functions/resourceName.bicep' | ||
|
||
param location string | ||
param applicationInsightsName string | ||
param namePrefix string | ||
param keyVaultName string | ||
|
||
@export() | ||
type Sku = { | ||
storageAccountName: 'Standard_LRS' | 'Standard_GRS' | 'Standard_RAGRS' | 'Standard_ZRS' | 'Premium_LRS' | 'Premium_ZRS' | ||
applicationServicePlanName: 'F1' | 'D1' | 'B1' | 'B2' | 'B3' | 'S1' | 'S2' | 'S3' | 'P1' | 'P2' | 'P3' | 'P1V2' | 'P2V2' | 'P3V2' | 'I1' | 'I2' | 'I3' | 'Y1' | 'Y2' | 'Y3' | 'Y1v2' | 'Y2v2' | 'Y3v2' | 'Y1v2Isolated' | 'Y2v2Isolated' | 'Y3v2Isolated' | ||
applicationServicePlanTier: 'Free' | 'Shared' | 'Basic' | 'Dynamic' | 'Standard' | 'Premium' | 'Isolated' | ||
storageAccountName: | ||
| 'Standard_LRS' | ||
| 'Standard_GRS' | ||
| 'Standard_RAGRS' | ||
| 'Standard_ZRS' | ||
| 'Premium_LRS' | ||
| 'Premium_ZRS' | ||
applicationServicePlanName: | ||
| 'F1' | ||
| 'D1' | ||
| 'B1' | ||
| 'B2' | ||
| 'B3' | ||
| 'S1' | ||
| 'S2' | ||
| 'S3' | ||
| 'P1' | ||
| 'P2' | ||
| 'P3' | ||
| 'P1V2' | ||
| 'P2V2' | ||
| 'P3V2' | ||
| 'I1' | ||
| 'I2' | ||
| 'I3' | ||
| 'Y1' | ||
| 'Y2' | ||
| 'Y3' | ||
| 'Y1v2' | ||
| 'Y2v2' | ||
| 'Y3v2' | ||
| 'Y1v2Isolated' | ||
| 'Y2v2Isolated' | ||
| 'Y3v2Isolated' | ||
applicationServicePlanTier: 'Free' | 'Shared' | 'Basic' | 'Dynamic' | 'Standard' | 'Premium' | 'Isolated' | ||
} | ||
param sku Sku | ||
|
||
// Storage account names only supports lower case and numbers | ||
// todo: add name of function as param and turn this into a reusable module | ||
var storageAccountName = '${replace(namePrefix, '-', '')}${substring('slacknotifier', 0, 10)}sa' | ||
// We use uniqueStringBySubscriptionAndResourceGroup directly here to avoid having too short storage account name. | ||
// This should be refactored to use one common storage account. Or one storage account for all app functions. | ||
var storageAccountName = take( | ||
replace('${'${namePrefix}-sn'}-${uniqueStringBySubscriptionAndResourceGroup()}', '-', ''), | ||
24 | ||
) | ||
|
||
resource storageAccount 'Microsoft.Storage/storageAccounts@2023-04-01' = { | ||
name: storageAccountName | ||
location: location | ||
sku: { | ||
name: sku.storageAccountName | ||
} | ||
kind: 'Storage' | ||
properties: { | ||
supportsHttpsTrafficOnly: true | ||
defaultToOAuthAuthentication: true | ||
minimumTlsVersion: 'TLS1_2' | ||
} | ||
name: storageAccountName | ||
location: location | ||
sku: { | ||
name: sku.storageAccountName | ||
} | ||
kind: 'Storage' | ||
properties: { | ||
supportsHttpsTrafficOnly: true | ||
defaultToOAuthAuthentication: true | ||
minimumTlsVersion: 'TLS1_2' | ||
} | ||
} | ||
|
||
resource applicationServicePlan 'Microsoft.Web/serverfarms@2023-12-01' = { | ||
name: '${namePrefix}-slacknotifier-asp' | ||
location: location | ||
sku: { | ||
name: sku.applicationServicePlanName | ||
tier: sku.applicationServicePlanTier | ||
} | ||
properties: {} | ||
name: '${namePrefix}-slacknotifier-asp' | ||
location: location | ||
sku: { | ||
name: sku.applicationServicePlanName | ||
tier: sku.applicationServicePlanTier | ||
} | ||
properties: {} | ||
} | ||
|
||
resource applicationInsights 'Microsoft.Insights/components@2020-02-02' existing = { | ||
name: applicationInsightsName | ||
name: applicationInsightsName | ||
} | ||
|
||
var functionAppName = '${namePrefix}-slacknotifier-fa' | ||
var functionAppNameMaxLength = 40 | ||
var functionAppName = uniqueResourceName('${namePrefix}-slacknotifier-fa', functionAppNameMaxLength) | ||
resource functionApp 'Microsoft.Web/sites@2023-12-01' = { | ||
name: functionAppName | ||
location: location | ||
kind: 'functionapp' | ||
identity: { | ||
type: 'SystemAssigned' | ||
} | ||
properties: { | ||
serverFarmId: applicationServicePlan.id | ||
publicNetworkAccess: 'Enabled' | ||
siteConfig: { | ||
// Setting/updating appSettings in separate module in order to not delete already deployed functions, see below | ||
} | ||
httpsOnly: true | ||
name: functionAppName | ||
location: location | ||
kind: 'functionapp' | ||
identity: { | ||
type: 'SystemAssigned' | ||
} | ||
properties: { | ||
serverFarmId: applicationServicePlan.id | ||
publicNetworkAccess: 'Enabled' | ||
siteConfig: { | ||
// Setting/updating appSettings in separate module in order to not delete already deployed functions, see below | ||
} | ||
httpsOnly: true | ||
} | ||
} | ||
|
||
var appSettings = { | ||
AzureWebJobsStorage: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}' | ||
WEBSITE_CONTENTAZUREFILECONNECTIONSTRING: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}' | ||
WEBSITE_CONTENTSHARE: toLower(functionAppName) | ||
FUNCTIONS_EXTENSION_VERSION: '~4' | ||
APPINSIGHTS_INSTRUMENTATIONKEY: applicationInsights.properties.InstrumentationKey | ||
Slack__WebhookUrl: '@Microsoft.KeyVault(VaultName=${keyVaultName};SecretName=Slack--Webhook--Url)' | ||
FUNCTIONS_WORKER_RUNTIME: 'dotnet-isolated' | ||
AzureWebJobsStorage: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}' | ||
WEBSITE_CONTENTAZUREFILECONNECTIONSTRING: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}' | ||
WEBSITE_CONTENTSHARE: toLower(functionAppName) | ||
FUNCTIONS_EXTENSION_VERSION: '~4' | ||
APPINSIGHTS_INSTRUMENTATIONKEY: applicationInsights.properties.InstrumentationKey | ||
Slack__WebhookUrl: '@Microsoft.KeyVault(VaultName=${keyVaultName};SecretName=Slack--Webhook--Url)' | ||
FUNCTIONS_WORKER_RUNTIME: 'dotnet-isolated' | ||
} | ||
|
||
module updateAppSettings 'appSettings.bicep' = { | ||
name: '${functionAppName}-appsettings' | ||
params: { | ||
webAppName: functionAppName | ||
currentAppSettings: list(resourceId('Microsoft.Web/sites/config', functionAppName, 'appsettings'), '2023-01-01').properties | ||
appSettings: appSettings | ||
} | ||
name: '${functionAppName}-appsettings' | ||
params: { | ||
webAppName: functionAppName | ||
currentAppSettings: list(resourceId('Microsoft.Web/sites/config', functionAppName, 'appsettings'), '2023-01-01').properties | ||
appSettings: appSettings | ||
} | ||
} | ||
|
||
var defaultFunctionKey = listkeys('${functionApp.id}/host/default', '2023-01-01').functionKeys.default | ||
var forwardAlertToSlackTriggerUrl = 'https://${functionApp.properties.defaultHostName}/api/forwardalerttoslack?code=${defaultFunctionKey}' | ||
resource notifyDevTeam 'Microsoft.Insights/actionGroups@2023-01-01' = { | ||
name: '${namePrefix}-notify-devteam-ag' | ||
location: 'Global' | ||
properties: { | ||
enabled: true | ||
groupShortName: 'DevNotify' | ||
azureFunctionReceivers: [ | ||
{ | ||
name: functionApp.properties.defaultHostName | ||
functionName: 'ForwardAlertToSlack' | ||
functionAppResourceId: functionApp.id | ||
httpTriggerUrl: forwardAlertToSlackTriggerUrl | ||
useCommonAlertSchema: true | ||
} | ||
] | ||
} | ||
name: '${namePrefix}-notify-devteam-ag' | ||
location: 'Global' | ||
properties: { | ||
enabled: true | ||
groupShortName: 'DevNotify' | ||
azureFunctionReceivers: [ | ||
{ | ||
name: functionApp.properties.defaultHostName | ||
functionName: 'ForwardAlertToSlack' | ||
functionAppResourceId: functionApp.id | ||
httpTriggerUrl: forwardAlertToSlackTriggerUrl | ||
useCommonAlertSchema: true | ||
} | ||
] | ||
} | ||
} | ||
|
||
resource exceptionOccuredAlertRule 'Microsoft.Insights/scheduledQueryRules@2023-03-15-preview' = { | ||
name: '${namePrefix}-exception-occured-sqr' | ||
location: location | ||
properties: { | ||
enabled: true | ||
severity: 1 | ||
evaluationFrequency: 'PT5M' | ||
windowSize: 'PT5M' | ||
scopes: [ applicationInsights.id ] | ||
autoMitigate: false | ||
targetResourceTypes: [ | ||
'microsoft.insights/components' | ||
] | ||
criteria: { | ||
allOf: [ | ||
{ | ||
query: 'exceptions | summarize count = count() by environment = tostring(customDimensions.AspNetCoreEnvironment), problemId' | ||
operator: 'GreaterThan' | ||
threshold: 0 | ||
timeAggregation: 'Count' | ||
failingPeriods: { | ||
numberOfEvaluationPeriods: 1 | ||
minFailingPeriodsToAlert: 1 | ||
} | ||
} | ||
] | ||
} | ||
actions: { | ||
actionGroups: [ | ||
notifyDevTeam.id | ||
] | ||
name: '${namePrefix}-exception-occured-sqr' | ||
location: location | ||
properties: { | ||
enabled: true | ||
severity: 1 | ||
evaluationFrequency: 'PT5M' | ||
windowSize: 'PT5M' | ||
scopes: [applicationInsights.id] | ||
autoMitigate: false | ||
targetResourceTypes: [ | ||
'microsoft.insights/components' | ||
] | ||
criteria: { | ||
allOf: [ | ||
{ | ||
query: 'exceptions | summarize count = count() by environment = tostring(customDimensions.AspNetCoreEnvironment), problemId' | ||
operator: 'GreaterThan' | ||
threshold: 0 | ||
timeAggregation: 'Count' | ||
failingPeriods: { | ||
numberOfEvaluationPeriods: 1 | ||
minFailingPeriodsToAlert: 1 | ||
} | ||
} | ||
] | ||
} | ||
actions: { | ||
actionGroups: [ | ||
notifyDevTeam.id | ||
] | ||
} | ||
} | ||
} | ||
|
||
output functionAppPrincipalId string = functionApp.identity.principalId |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.