Skip to content

Commit

Permalink
Added auto scaling for Azure Service Bus (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
mburumaxwell authored Sep 8, 2023
1 parent d4bcfb6 commit 678011c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 33 deletions.
41 changes: 33 additions & 8 deletions main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ resource serviceBusNamespace 'Microsoft.ServiceBus/namespaces@2021-11-01' = if (
disableLocalAuth: false
zoneRedundant: false
}
sku: {
name: 'Basic'
}
sku: { name: 'Basic' }

resource authorizationRule 'AuthorizationRules' existing = { name: 'RootManageSharedAccessKey' }
}
resource providedServiceBusNamespace 'Microsoft.ServiceBus/namespaces@2021-11-01' existing = if (eventBusTransport == 'ServiceBus' && hasProvidedServiceBusNamespace) {
// Inspired by https://github.com/Azure/bicep/issues/1722#issuecomment-952118402
Expand All @@ -90,6 +90,8 @@ resource providedServiceBusNamespace 'Microsoft.ServiceBus/namespaces@2021-11-01
// 8 -> 'fabrikam'
name: split(serviceBusNamespaceId, '/')[8]
scope: resourceGroup(split(serviceBusNamespaceId, '/')[2], split(serviceBusNamespaceId, '/')[4])

resource authorizationRule 'AuthorizationRules' existing = { name: 'RootManageSharedAccessKey' }
}

/* Storage Account */
Expand Down Expand Up @@ -184,11 +186,18 @@ resource app 'Microsoft.App/containerApps@2022-10-01' = {
server: dockerImageRegistry
}
] : []
secrets: [
{ name: 'connection-strings-application-insights', value: appInsights.properties.ConnectionString }
{ name: 'notifications-password', value: notificationsPassword }
{ name: 'project-and-token-0', value: '${azureDevOpsProjectUrl};${azureDevOpsProjectToken}' }
]
secrets: concat(
[
{ name: 'connection-strings-application-insights', value: appInsights.properties.ConnectionString }
{ name: 'notifications-password', value: notificationsPassword }
{ name: 'project-and-token-0', value: '${azureDevOpsProjectUrl};${azureDevOpsProjectToken}' }
],
eventBusTransport == 'ServiceBus' ? [
{
name: 'connection-strings-asb-scaler'
value: hasProvidedServiceBusNamespace ? providedServiceBusNamespace::authorizationRule.listKeys().primaryConnectionString : serviceBusNamespace::authorizationRule.listKeys().primaryConnectionString
}
] : [])
}
template: {
containers: [
Expand Down Expand Up @@ -229,6 +238,22 @@ resource app 'Microsoft.App/containerApps@2022-10-01' = {
scale: {
minReplicas: minReplicas
maxReplicas: maxReplicas
rules: concat(
[ { name: 'http', http: { metadata: { concurrentRequests: '1000' } } } ],
eventBusTransport == 'ServiceBus' ? [
{
name: 'azure-servicebus-${name}'
custom: {
type: 'azure-servicebus'
metadata: {
namespace: hasProvidedServiceBusNamespace ? providedServiceBusNamespace.name : serviceBusNamespace.name // Name of the Azure Service Bus namespace that contains your queue or topic.
queueName: 'azdo-cleanup' // Name of the Azure Service Bus queue to scale on.
messageCount: '100' // Amount of active messages in your Azure Service Bus queue or topic to scale on.
}
auth: [ { secretRef: 'connection-strings-asb-scaler', triggerParameter: 'connection' } ]
}
}
] : [])
}
}
}
Expand Down
38 changes: 13 additions & 25 deletions main.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@
"eventBusTransport": {
"type": "string",
"defaultValue": "ServiceBus",
"metadata": {
"description": "Merge strategy to use when setting auto complete on created pull requests."
},
"allowedValues": [
"InMemory",
"ServiceBus",
"QueueStorage"
]
],
"metadata": {
"description": "Merge strategy to use when setting auto complete on created pull requests."
}
},
"serviceBusNamespaceId": {
"type": "string",
Expand Down Expand Up @@ -98,20 +98,20 @@
"minReplicas": {
"type": "int",
"defaultValue": 0,
"minValue": 0,
"maxValue": 2,
"metadata": {
"description": "The minimum number of replicas"
},
"maxValue": 2,
"minValue": 0
}
},
"maxReplicas": {
"type": "int",
"defaultValue": 1,
"minValue": 1,
"maxValue": 5,
"metadata": {
"description": "The maximum number of replicas"
},
"maxValue": 5,
"minValue": 1
}
}
},
"variables": {
Expand Down Expand Up @@ -220,20 +220,7 @@
]
},
"registries": "[if(variables('isAcrServer'), createArray(createObject('identity', resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', parameters('name')), 'server', parameters('dockerImageRegistry'))), createArray())]",
"secrets": [
{
"name": "connection-strings-application-insights",
"value": "[reference(resourceId('Microsoft.Insights/components', parameters('name')), '2020-02-02').ConnectionString]"
},
{
"name": "notifications-password",
"value": "[parameters('notificationsPassword')]"
},
{
"name": "project-and-token-0",
"value": "[format('{0};{1}', parameters('azureDevOpsProjectUrl'), parameters('azureDevOpsProjectToken'))]"
}
]
"secrets": "[concat(createArray(createObject('name', 'connection-strings-application-insights', 'value', reference(resourceId('Microsoft.Insights/components', parameters('name')), '2020-02-02').ConnectionString), createObject('name', 'notifications-password', 'value', parameters('notificationsPassword')), createObject('name', 'project-and-token-0', 'value', format('{0};{1}', parameters('azureDevOpsProjectUrl'), parameters('azureDevOpsProjectToken')))), if(equals(parameters('eventBusTransport'), 'ServiceBus'), createArray(createObject('name', 'connection-strings-asb-scaler', 'value', if(variables('hasProvidedServiceBusNamespace'), listKeys(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', split(parameters('serviceBusNamespaceId'), '/')[2], split(parameters('serviceBusNamespaceId'), '/')[4]), 'Microsoft.ServiceBus/namespaces/AuthorizationRules', split(parameters('serviceBusNamespaceId'), '/')[8], 'RootManageSharedAccessKey'), '2021-11-01').primaryConnectionString, listKeys(resourceId('Microsoft.ServiceBus/namespaces/AuthorizationRules', format('{0}-{1}', parameters('name'), variables('collisionSuffix')), 'RootManageSharedAccessKey'), '2021-11-01').primaryConnectionString))), createArray()))]"
},
"template": {
"containers": [
Expand Down Expand Up @@ -302,7 +289,8 @@
],
"scale": {
"minReplicas": "[parameters('minReplicas')]",
"maxReplicas": "[parameters('maxReplicas')]"
"maxReplicas": "[parameters('maxReplicas')]",
"rules": "[concat(createArray(createObject('name', 'http', 'http', createObject('metadata', createObject('concurrentRequests', '1000')))), if(equals(parameters('eventBusTransport'), 'ServiceBus'), createArray(createObject('name', format('azure-servicebus-{0}', parameters('name')), 'custom', createObject('type', 'azure-servicebus', 'metadata', createObject('namespace', if(variables('hasProvidedServiceBusNamespace'), split(parameters('serviceBusNamespaceId'), '/')[8], format('{0}-{1}', parameters('name'), variables('collisionSuffix'))), 'queueName', 'azdo-cleanup', 'messageCount', '100'), 'auth', createArray(createObject('secretRef', 'connection-strings-asb-scaler', 'triggerParameter', 'connection'))))), createArray()))]"
}
}
},
Expand Down

0 comments on commit 678011c

Please sign in to comment.