FREE STUDY NOTES · AI-200

Custom KEDA scale rules in Azure Container Apps

How to define KEDA scalers for queues, topics and other event sources in Container Apps.

From Ultra Transcenders AI-200 by Tony Rough (publishing soon)

Custom rules let a container app scale on anything a KEDA ScaledObject scaler can measure, which is how queue- and stream-driven AI pipelines are scaled.

A custom scale rule is based on any ScaledObject-based KEDA scaler. The conversion from a KEDA specification is mechanical:

KEDA ScaledObject trigger Container Apps scale rule CLI parameter
type (for example azure-servicebus) custom.type --scale-rule-type
metadata key/value pairs custom.metadata --scale-rule-metadata "key=value" ...
TriggerAuthentication secretTargetRef custom.auth entries (secretRef, triggerParameter) --scale-rule-auth "parameter=secret-name"
Managed identity (pod identity) custom.identity (system or a user-assigned resource ID) --scale-rule-identity

Event-driven jobs use the same approach but with ScaledJob-based scalers (see the section on jobs).

Common scalers for AI workloads

Source KEDA type Key metadata (from the Learn examples) Scales on
Azure Service Bus queue azure-servicebus queueName, namespace, messageCount Messages waiting per replica target
Azure Queue Storage azure-queue accountName, queueName, queueLength Queue length per replica target
Azure Event Hubs KEDA Event Hubs scaler As defined in the KEDA scaler specification Unprocessed events
CPU cpu type=Utilization, value Average CPU utilisation across replicas
Memory memory type=Utilization, value Average memory utilisation across replicas (includes sidecars)
Apache Kafka, Redis KEDA Kafka and Redis scalers Per KEDA specification Lag or list length

For the Service Bus scaler, messageCount is the number of messages one replica is expected to handle. With messageCount 30 and 150 messages waiting, KEDA targets five replicas, never exceeding maxReplicas. Microsoft Learn documents Event Hubs, Kafka and Redis as supported event sources but defers their metadata to the KEDA scaler documentation, so check the exact parameter names there.

The following ARM excerpt shows a Service Bus queue rule authenticated with the app’s system-assigned managed identity:

"scale": {
  "minReplicas": 0,
  "maxReplicas": 20,
  "rules": [{
    "name": "orders-queue",
    "custom": {
      "type": "azure-servicebus",
      "metadata": {
        "queueName": "orders",
        "namespace": "<SERVICE_BUS_NAMESPACE>",
        "messageCount": "5"
      },
      "identity": "system"
    }
  }]
}

As with any managed identity call, the target resource must grant the identity a suitable role assignment, or requests are rejected even with a valid token; secret-based authentication is the alternative (see the next section).

Multiple rules

You can define several rules on one revision. The app begins to scale as soon as the first condition of any rule is met. With the Azure CLI, adding a rule with --scale-rule-* parameters replaces any existing rule, so to keep several rules you must export the app to YAML (az containerapp show --output yaml), add entries under properties.template.scale.rules, and apply it with az containerapp update --yaml.

Common trap: Running az containerapp update --scale-rule-name ... twice to add a second rule - each CLI rule definition replaces the existing rule; multiple rules require a YAML (or ARM/Bicep) definition.

Revision mode with event rules

The Learn examples for HTTP rules carry a note to set properties.configuration.activeRevisionsMode to single when using non-HTTP event scale rules. Traffic splitting weights apply to ingress traffic, so they offer no way to divide messages that replicas pull from a queue.

Azure Functions on Container Apps

Azure Functions hosted in a Container Apps environment automatically generate KEDA rules from function triggers and bindings, so the portal disables Add scale rules for these apps. You can still set minimum and maximum replicas, and allowScalingRuleOverride lets you replace the generated rules. Function hosting choices are covered in “Azure Functions: serverless APIs, triggers and bindings”.

Get the whole book

This note is one section of Ultra Transcenders AI-200: Developing AI Cloud Solutions on Azure, an independent study guide that explains every topic the exam covers by technology, with comparison tables, diagrams and the common traps, plus a glossary linked to Microsoft Learn.

Amazon.co.ukKindle: coming soonPaperback: coming soon
Amazon.comKindle: coming soonPaperback: coming soon

Publishing soon on Amazon in Kindle and paperback editions.

About the book · AI-200 terms in the glossary · All AI-200 study notes

More AI-200 study notes