Photo by Manuel Geissinger

Automatically scale your APIM secondary instance in a multi-region mode

Stop manual scaling

Posted by Damien Aicheh on 09/16/2024 · 6 mins

In this tutorial, you will discover how to automatically scale an APIM instance in a secondary region. At the time I am writing this article the only way to do it is manual.

Context

In a scenario of an APIM deployed in multi-regions you have the ability to enable auto scaling to the primary region but this feature is not available yet on secondary region, so this is something that you need to do on your own manually or find a way to automate this process.

In this tutorial you will see a scenario where you have an APIM instance deployed in france central for the primary region and west europe for the secondary region. The idea of this tutorial is to discover a way to automatically scale the secondary region.

Metrics

The first thing you need to do is to find the right metrics to scale your APIM secondary region. In APIM you have a lot of metrics available, you can find them in the Azure portal in the Metrics section of your APIM instance.

The capacity metric is the one that you need to use to scale your APIM instance. This metric is emitted per minute and reflects the gateway capacity. The metric range from 0-100 which is calculated against CPU and memory utilization.

Metrics

So the idea will be to listen to this metric and based on it, you will scale your APIM instance. To do this you will need to use Azure Monitor and create an alert based on the capacity metric.

Architecture

The idea is to create an alert based on the capacity metric and when the alert is triggered you will scale your APIM instance. So you will need to create a resource to trigger the scaling of your APIM instance. For this tutorial, I will use an Azure Function.

Architecture

So, when the APIM capacity metric is above a certain threshold, an alert will be triggered and the Azure Function will be called to scale the APIM instance. The Azure Function will use the Azure CLI to scale the APIM instance. Of course base on the traffic of your APIM instance in a secondary region, you will increase or decrease the capacity of your APIM instance.

Alert

To create an alert you need to go to the Alerts section of your APIM instance and Select + Create > Alert rule.

Alert

As you can see above, in the Condition tab, you need to select the capacity metric and target the secondary region of your APIM instance. You will also have to specifiy the threshold and the period of time. So you can imagine a scenario where if the average of the threshold is above 60% during 30 minutes, you will trigger the alert to scale your APIM instance.

Based on those metrics you will trigger the Azure Function, so inside the Actions tab you need to select Azure Function and select your Azure Function.

Select Function

Make sure also to create another alert to do the inverse operation, so if the capacity metric is below a certain threshold you will decrease the capacity of your APIM instance to avoid over-provisioning.

Azure Function

To keep things simple, you can use the Azure Functions with PowerShell mode so you can use the Azure CLI directly.

First you will need to get the current number of instances of your APIM instance in the secondary region. You can do this by using the Azure CLI:

$CURRENT_CAPACITY = az apim show --name <your-apim-name> --resource-group <your-resource-group> --query "additionalLocations[0].sku.capacity"

This command will return the current capacity of your APIM instance in the secondary region. If you have multiple instances you will need to target the right one with the additionalLocations parameter and the index of the instance. More details about the az apim show command can be found here.

Next, you will need to scale your APIM instance based on the current capacity:

az apim update --name <your-apim-name> --resource-group <your-resource-group> --set additionalLocations[0].sku.capacity = $CURRENT_CAPACITY++

This command will increase the capacity of your APIM instance by one. Of course if you want to decrease the capacity you will need to use the --set additionalLocations[0].sku.capacity = $CURRENT_CAPACITY-- command.

More details about the az apim update command can be found here.

Finally, if you want to have an update of the provisioning status your APIM instance you can use the following command:

az apim wait --name <your-apim-name> --resource-group <your-resource-group> --updated

More details about the az apim wait command can be found here.

Final touch

Now you have all the pieces to automatically scale your APIM instance in the secondary region. You can now test your alert and see if the Azure Function is triggered and if the capacity of your APIM secondary region instance is increased or decreased.

Do not hesitate to follow me on to not miss my next tutorial!