Automate Scaling of Azure Container Apps

Managing costs while ensuring optimal performance is a critical aspect of running applications in the cloud. Azure Container Apps provide a flexible and scalable way to deploy containerized applications, but without proper automation, costs can quickly spiral out of control.

In this guide, we will walk you through the process of creating an automation script to dynamically scale your Azure Container Apps based on business hours. By automating the scaling process, you can ensure that your container apps run with a minimum number of instances during business hours and scale down to zero after hours, thereby optimizing costs without compromising on performance.

Whether you are a cloud engineer, a DevOps professional, or a developer looking to streamline your cloud operations, this guide will provide you with the necessary steps to implement cost-effective scaling for your Azure Container Apps. Let’s dive in and start automating!

Step 1: Create an Azure Automation Account

  1. Go to the Azure portal.
  2. Search for “Automation Accounts” and create a new automation account.

Step 2: Install or Update the Azure CLI Extension for Container Apps

Before you can use the az containerapp commands, you need to ensure that the Azure CLI extension for Container Apps is installed and up to date.

  1. Open your terminal.

  2. Run the following commands to install or update the extension:

    # Install the Azure CLI extension for Container Apps
    az extension add --name containerapp
    
    # Update the Azure CLI extension for Container Apps
    az extension update --name containerapp
    

Step 3: Create Runbooks

Runbook 1: Scale Up During Business Hours

  1. In the Automation Account, go to “Runbooks” and create a new runbook.

  2. Choose “PowerShell” as the runbook type.

  3. Name the runbook ScaleUpRunbook.

  4. Add the following PowerShell script:

    # PowerShell script to scale Azure Container Apps to min 1 and max 1
    
    # Connect to Azure
    Connect-AzAccount -Identity
    
    # Define the resource group and container app names
    $resourceGroupName = "your-resource-group-name"
    $containerAppNames = @("container-app-1", "container-app-2") # Add your container app names here
    
    # Scale each container app to min 1 and max 1
    foreach ($appName in $containerAppNames) {
        Write-Output "Scaling container app to min 1 and max 1: $appName"
        az containerapp update --name $appName --resource-group $resourceGroupName --set properties.template.scale.minReplicas=1 properties.template.scale.maxReplicas=1
        Write-Output "Scaled container app to min 1 and max 1: $appName"
    }
    
  5. Save and publish the runbook.

Runbook 2: Scale Down After Hours

  1. In the Automation Account, go to “Runbooks” and create a new runbook.

  2. Choose “PowerShell” as the runbook type.

  3. Name the runbook ScaleDownRunbook.

  4. Add the following PowerShell script:

    # PowerShell script to scale Azure Container Apps to zero
    
    # Connect to Azure
    Connect-AzAccount -Identity
    
    # Define the resource group and container app names
    $resourceGroupName = "your-resource-group-name"
    $containerAppNames = @("container-app-1", "container-app-2") # Add your container app names here
    
    # Scale each container app to zero
    foreach ($appName in $containerAppNames) {
        Write-Output "Scaling container app to zero: $appName"
        az containerapp update --name $appName --resource-group $resourceGroupName --set properties.template.scale.minReplicas=0
        Write-Output "Scaled container app to zero: $appName"
    }
    
  5. Save and publish the runbook.

Step 4: Create Schedules

  1. In the Automation Account, go to “Schedules” and create two new schedules:
    • Business Hours Schedule: Set this schedule to run at the start of business hours (e.g., 8:00 AM).
    • After Hours Schedule: Set this schedule to run at the end of business hours (e.g., 6:00 PM).

Example Commands for Scheduling

# Log in to Azure
az login

# Create a schedule for business hours
az automation schedule create --automation-account-name <automation-account-name> --resource-group <resource-group-name> --name "BusinessHoursSchedule" --start-time "2025-02-14T08:00:00Z" --recurrence "Day" --interval 1

# Create a schedule for after hours
az automation schedule create --automation-account-name <automation-account-name> --resource-group <resource-group-name> --name "AfterHoursSchedule" --start-time "2025-02-14T18:00:00Z" --recurrence "Day" --interval 1

# Link the schedules to the runbooks
az automation runbook start --automation-account-name <automation-account-name> --resource-group <resource-group-name> --name "ScaleUpRunbook" --schedule-name "BusinessHoursSchedule"
az automation runbook start --automation-account-name <automation-account-name> --resource-group <resource-group-name> --name "ScaleDownRunbook" --schedule-name "AfterHoursSchedule"

Conclusion

By following this guide, you have successfully set up automation to dynamically scale your Azure Container Apps based on business hours. This automation helps you optimize costs by ensuring that your container apps run with the necessary resources during peak hours and scale down to zero during off-hours. Implementing such automation not only saves costs but also ensures that your applications remain performant and responsive to user demands.

I hope this guide has been helpful in streamlining your cloud operations. Happy automating and saving :) !