1. Configuring Auto-scaling rules for Azure App Service Plan

    TypeScript

    Auto-scaling is a feature that allows your application to handle increases in load without administrator intervention. It is particularly useful for scenarios where the workload is variable, such as a retail app that experiences more traffic during the holidays or a web service that has peak hours of operation.

    With Azure, you can configure auto-scaling rules and criteria for an App Service Plan, which provides infrastructure for hosting your web apps. Auto-scaling can be achieved by adjusting the number of instances or by changing the compute resources for the instances based on the load.

    To set up auto-scaling rules for an Azure App Service Plan using Pulumi, you will primarily use the azure-native provider. This provider is made by Azure and provides classes that map directly to Azure resources.

    The two resources relevant to configuring auto-scaling are:

    1. AppServicePlan: This represents the App Service Plan itself, where you define the specifics such as the pricing tier and the number of instances you want to start with.

      • The AppServicePlan documentation is available here.
    2. AutoscaleSetting: This represents the rules that define when and how the App Service Plan should scale.

      • The AutoscaleSetting documentation is available here.

    Now, let's look at a TypeScript program that configures an Azure App Service Plan with auto-scaling rules. The following program will:

    • Create an App Service Plan.
    • Define an auto-scaling setting for that plan.
    • Define rules when scaling should occur, including the metric to scale on (CPU usage, in this example), the direction of scale (up or down), and the amount to scale.

    Here is the Pulumi program that accomplishes this:

    import * as pulumi from "@pulumi/pulumi"; import * as azure from "@pulumi/azure-native"; // Create an Azure Resource Group const resourceGroup = new azure.resources.ResourceGroup("resourceGroup"); // Create an Azure App Service Plan const appServicePlan = new azure.web.AppServicePlan("appServicePlan", { resourceGroupName: resourceGroup.name, kind: "App", sku: { name: "P1v2", // Pricing tier tier: "PremiumV2", size: "P1v2", family: "P", capacity: 1, // Start with 1 instance }, location: resourceGroup.location, }); // Auto-scale settings for the App Service Plan const autoScaleSetting = new azure.insights.AutoscaleSetting("autoScaleSetting", { targetResourceUri: appServicePlan.id, resourceGroupName: resourceGroup.name, location: resourceGroup.location, profiles: [{ name: "autoScaleProfile", capacity: { default: "1", maximum: "10", minimum: "1", }, rules: [ // Scale out rule { metricTrigger: { metricName: "CpuPercentage", metricResourceUri: appServicePlan.id, timeGrain: "PT1M", statistic: "Average", timeWindow: "PT5M", timeAggregation: "Average", operator: "GreaterThan", threshold: 70, }, scaleAction: { direction: "Increase", type: "ChangeCount", value: "1", // Increase instance count by 1 cooldown: "PT5M", }, }, // Scale in rule { metricTrigger: { metricName: "CpuPercentage", metricResourceUri: appServicePlan.id, timeGrain: "PT1M", statistic: "Average", timeWindow: "PT5M", timeAggregation: "Average", operator: "LessThan", threshold: 30, }, scaleAction: { direction: "Decrease", type: "ChangeCount", value: "1", // Decrease instance count by 1 cooldown: "PT5M", }, }, ], }], }); // Export the App Service Plan name export const appServicePlanName = appServicePlan.name;

    In this program:

    • We create a new resource group via azure.resources.ResourceGroup.
    • An azure.web.AppServicePlan is instantiated to host our services. We specify the SKU details and an initial instance count.
    • The azure.insights.AutoscaleSetting is where we define the auto-scaling logic. The profiles section allows us to create scaling rules. In this example, two rules are configured:
      • The first rule triggers scaling out (adding more instances) when the average CPU percentage over the last 5 minutes is greater than 70%.
      • The second rule triggers scaling in (removing instances) when the average CPU percentage over the same duration falls below 30%.

    This setup helps ensure that your App Service Plan automatically scales in response to load, which can improve performance and cost efficiency.

    Remember to replace the SKU details in the AppServicePlan resource with the tier and size that fits your application's needs. The CPU percentage thresholds and scale values are also configurable based on your requirements.

    With this program, you now have a scalable Azure App Service Plan that adjusts its instance count based on CPU load.