Help Center> Terraform> User Guide> Auto Scaling (AS)
Updated on 2023-12-21 GMT+08:00

Auto Scaling (AS)

Application Scenarios

AS automatically adjusts service resources to keep up with your demand based on pre-configured AS policies. With automatic resource adjustment, you can enjoy reduced costs, improved availability, and high fault tolerance. AS applies to the following scenarios:

  • Heavy-traffic forums: Service load changes of a heavy-traffic forum website are difficult to predict. AS dynamically adjusts the number of cloud servers based on monitored ECS metrics, such as vCPU Usage and Memory Usage.
  • E-commerce: Large-scale e-commerce promotions can attract visits that may break your website. AS automatically adds ECSs and increases bandwidth to ensure that promotions will go smoothly.
  • Live streaming: A live streaming website broadcasts popular programs from 14:00 to 16:00 every day. AS automatically adds ECSs and increases bandwidth during this period to ensure smooth viewer experience.

Procedure

  1. Create an AS configuration.

    Create the main.tf file, enter the following information, and save the file:
    data "huaweicloud_availability_zones" "myaz" {}
    
    data "huaweicloud_compute_flavors" "myflavor" {
      availability_zone = data.huaweicloud_availability_zones.myaz.names[0]
      performance_type  = "normal"
      cpu_core_count    = 2
      memory_size       = 4
    }
    
    data "huaweicloud_images_image" "myimage" {
      name        = "Ubuntu 18.04 server 64bit"
      most_recent = true
    }
    
    resource "huaweicloud_as_configuration" "my_as_config" {
      scaling_configuration_name = "my_as_config"
    
      instance_config {
        flavor   = data.huaweicloud_compute_flavors.myflavor.ids[0]
        image    = data.huaweicloud_images_image.myimage.id
        key_name = var.my_keypair
        disk {
          size        = 40
          volume_type = "SSD"
          disk_type   = "SYS"
        }
      }
    }

  2. Create an AS group.

    Add the following information to the main.tf file:

    data "huaweicloud_vpc" "vpc_1" {
      name = var.vpc_name
    }
    
    data "huaweicloud_vpc_subnet" "subnet_1" {
      name   = var.subnet_name
      vpc_id = data.huaweicloud_vpc.vpc_1.id
    }
    
    data "huaweicloud_networking_secgroup" "secgroup_1" {
      name = var.secgroup_name
    }
    
    resource "huaweicloud_as_group" "my_as_group" {
      scaling_group_name       = "my_as_group"
      scaling_configuration_id = huaweicloud_as_configuration.my_as_config.id
      desire_instance_number   = 2
      min_instance_number      = 0
      max_instance_number      = 10
      vpc_id                   = data.huaweicloud_vpc.vpc_1.id
      delete_publicip          = true
      delete_instances         = "yes"
      networks {
        id = data.huaweicloud_vpc_subnet.subnet_1.id
      }
      security_groups {
        id = data.huaweicloud_networking_secgroup.secgroup_1.id
      }
      tags = {
        owner = "AutoScaling"
      }
    }

  3. Add a scale-out policy.

    In this example, add a metric-based policy. The following content that you will add to the main.tf file indicates that when the average CPU usage is greater than or equal to 80%, an ECS is automatically added.

    resource "huaweicloud_ces_alarmrule" "scaling_up_rule" {
      alarm_name = "scaling_up_rule"
      metric {
        namespace   = "SYS.AS"
        metric_name = "cpu_util"
        dimensions {
          name  = "AutoScalingGroup"
          value = huaweicloud_as_group.my_as_group.id
        }
      }
      condition {
        period              = 300
        filter              = "average"
        comparison_operator = ">="
        value               = 80
        unit                = "%"
        count               = 1
      }
      alarm_actions {
        type              = "autoscaling"
        notification_list = []
      }
    }
    resource "huaweicloud_as_policy" "scaling_up_policy" {
      scaling_policy_name = "scaling_up_policy"
      scaling_policy_type = "ALARM"
      scaling_group_id    = huaweicloud_as_group.my_as_group.id  
      alarm_id            = huaweicloud_ces_alarmrule.scaling_up_rule.id
      cool_down_time      = 300
      scaling_policy_action {
        operation       = "ADD"
        instance_number = 1
      }
    }

  4. Add a scale-in policy.

    In this example, add a metric-based policy. The following content that you will add to the main.tf file indicates that when the average CPU usage is equal to or lower than 20%, an ECS is automatically reduced.

    resource "huaweicloud_ces_alarmrule" "scaling_down_rule" {
      alarm_name = "scaling_down_rule"
      metric {
        namespace   = "SYS.AS"
        metric_name = "cpu_util"
        dimensions {
          name  = "AutoScalingGroup"
          value = huaweicloud_as_group.my_as_group.id
        }
      }
      condition {
        period              = 300
        filter              = "average"
        comparison_operator = "<="
        value               = 20
        unit                = "%"
        count               = 1
      }
      alarm_actions {
        type              = "autoscaling"
        notification_list = []
      }
    }
    resource "huaweicloud_as_policy" "scaling_down_policy" {
      scaling_policy_name = "scaling_down_policy"
      scaling_policy_type = "ALARM"
      scaling_group_id    = huaweicloud_as_group.my_as_group.id  
      alarm_id            = huaweicloud_ces_alarmrule.scaling_down_rule.id
      cool_down_time      = 300
      scaling_policy_action {
        operation       = "REMOVE"
        instance_number = 1
      }
    }

  5. Configure variables.

    Create the variables.tf file, enter the following information, and save the file. You can change the variable values based on your needs.

    variable "my_keypair" {
      default = "default"
    }
    variable "vpc_name" {
      default = "vpc-default"
    }
    variable "subnet_name" {
      default = "subnet-default"
    }
    variable "secgroup_name" {
      default = "default"
    }

  6. Create resources.

    1. Run terraform init to initialize the environment.
    2. Run terraform plan to view resources.
    3. After you confirm that the resource information is correct, run terraform apply to start resource creation.
    4. Run terraform show to view the created resources.