Terraform - Autoscale an App Service

May 08, 2021

I’ve recently been writing about Terraform - mainly because I’m learning it from scratch, and playing about with tech and then writing about it is basically my way of learning.

In this post, I’m going to build on this previous post on creating an App Service, by adding a Scale Out feature to it.

This is the App Service that we created in the referenced post:

terraform 4 1

In the image, you’ll see Scale Out. Note that it says (App Service Plan): in fact, this is just a link to the App Service Plan Scale Out. We can access it from here - let’s see what that looks like:

terraform 4 2

As we can see, there’s a single instance of the App Service, and it’s managed manually. What we’re going to do is change that so that the App Service is auto-scaled.

The Terraform script here is broadly taken from the example here. However, that applies to a VM Scale Set, whereas we’re applying it to an App Service Plan.



resource "azurerm\_monitor\_autoscale\_setting" "example" {
  name                = "myAutoscaleSetting"
  resource\_group\_name = azurerm\_resource\_group.rg.name
  location            = azurerm\_resource\_group.rg.location
  target\_resource\_id  = azurerm\_app\_service\_plan.app-service-plan.id
  profile {
    name = "default"
    capacity {
      default = 1
      minimum = 1
      maximum = 10
    }
    rule {
      metric\_trigger {
        metric\_name        = "CpuPercentage"
        metric\_resource\_id = azurerm\_app\_service\_plan.app-service-plan.id
        time\_grain         = "PT1M"
        statistic          = "Average"
        time\_window        = "PT5M"
        time\_aggregation   = "Average"
        operator           = "GreaterThan"
        threshold          = 90
      }
      scale\_action {
        direction = "Increase"
        type      = "ChangeCount"
        value     = "1"
        cooldown  = "PT1M"
      }
    }
    rule {
      metric\_trigger {
        metric\_name        = "CpuPercentage"
        metric\_resource\_id = azurerm\_app\_service\_plan.app-service-plan.id
        time\_grain         = "PT1M"
        statistic          = "Average"
        time\_window        = "PT5M"
        time\_aggregation   = "Average"
        operator           = "LessThan"
        threshold          = 10
      }
      scale\_action {
        direction = "Decrease"
        type      = "ChangeCount"
        value     = "1"
        cooldown  = "PT1M"
      }
    }
  }  
}


Some key points:

- The example uses "Percentage CPU", whereas for an App Service, this gets switched to [CpuPercentage](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/monitor_autoscale_setting#metric_name).
- The resource IDs that are referred to are that of the **App Service Plan**.

Finally, if we apply that, we can see the autoscale:

terraform 4 3

References

https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/monitor_autoscale_setting

https://stackoverflow.com/questions/58657096/error-creating-auto-scaling-rule-for-app-service-using-terraform-in-azure



Profile picture

A blog about one man's journey through code… and some pictures of the Peak District
Twitter

© Paul Michaels 2024