Here are the steps to set up Azure App Service autoscale with CLI commands:
Step 1: Create an App Service Plan
First, you need to create an App Service plan. You can create it using the Azure CLI command az appservice plan create
. Here’s an example:
az appservice plan create --name MyPlan --resource-group MyResourceGroup --sku B1 --is-linux --location eastus
This command creates an App Service plan named “MyPlan” in the “MyResourceGroup” resource group, with the “B1” SKU (Basic tier) and in the “eastus” location. The --is-linux
parameter specifies that the plan is for a Linux app.
Step 2: Create an App Service
Next, you need to create an App Service within the App Service plan. You can create it using the Azure CLI command az webapp create
. Here’s an example:
az webapp create --name MyWebApp --resource-group MyResourceGroup --plan MyPlan --runtime "NODE|14-lts" --deployment-local-git
This command creates an App Service named “MyWebApp” in the “MyResourceGroup” resource group, within the “MyPlan” App Service plan, with the “NODE|14-lts” runtime (Node.js version 14 LTS) and enables deployment via local Git.
Step 3: Configure Autoscaling
To configure autoscaling, you need to create a scaling rule. You can create it using the Azure CLI command az monitor autoscale rule create
. Here’s an example:
az monitor autoscale rule create --resource-group MyResourceGroup --name MyScaleRule --resource-id /subscriptions/<subscription-id>/resourceGroups/MyResourceGroup/providers/Microsoft.Web/sites/MyWebApp --condition "CpuPercentage > 70 avg 5m" --scale out 2 --scale in 1 --cooldown 5m
This command creates a scaling rule named “MyScaleRule” in the “MyResourceGroup” resource group for the App Service “MyWebApp”. The rule states that if the CPU percentage is greater than 70% on average for 5 minutes, the scale-out action should be triggered to increase the instance count by 2. Similarly, if the CPU percentage drops below the threshold, the scale-in action should be triggered to decrease the instance count by 1. The cooldown period between scale-out/in actions is set to 5 minutes.
Step 4: Verify Autoscaling
To verify that autoscaling is working as expected, you can monitor the instance count using the Azure CLI command az monitor metrics list
. Here’s an example:
az monitor metrics list --resource /subscriptions/<subscription-id>/resourceGroups/MyResourceGroup/providers/Microsoft.Web/sites/MyWebApp --metric Instances --aggregation Average --interval PT1M
This command lists the average number of instances for the App Service “MyWebApp” every 1 minute. If autoscaling is working as expected, you should see the instance count increase or decrease based on the CPU usage.
That’s it! You have successfully set up Azure App Service autoscale with CLI commands.