Scalability is an important feature that measures the O&M capabilities of distributed applications. This feature can automatically increase or decrease the number of instances to scale in or scale out system capacity. This improves resource utilization and reduces resource usage costs. This topic describes how to use Terraform to enable and disable an auto scaling policy for an SAE application.

Prerequisites

  • Terraform is installed.
  • Your account information is configured.

    You can select an Alibaba Cloud authentication method to provide the authentication information required by Terraform. The following example shows how to use environment variables to perform authentication:

    export ALICLOUD_ACCESS_KEY="************"
    export ALICLOUD_SECRET_KEY="************"
    export ALICLOUD_REGION="cn-hangzhou"
    Note To ensure data security, we recommend that you grant a RAM user the permissions to manage SAE resources based on your business requirements. For more information, see Grant permissions to a RAM user.

Background information

SAE auto scaling policies are classified into scheduled auto scaling policies, metric-based auto scaling policies, and hybrid auto scaling policies. If you want to use Terraform to enable an auto scaling policy for an SAE application, you must configure the alicloud_sae_application_scaling_rule resource when you create the application. If you want to disable the auto scaling policy, you must delete both the auto scaling policy and the application. For information about how to specify scheduled periods of time, see Use a crontab expression.

For more information, see alicloud_sae_application_scaling_rule.

Scenarios

  • Scheduled auto scaling policies are suitable for scenarios in which an application needs to use resources within a specific period of time. Scheduled auto scaling policies are commonly used in industries such as securities, healthcare, public administration, and education.
  • Metric-based auto scaling policies are suitable for scenarios in which burst traffic and periodic traffic occur when an application uses resources. Metric-based auto scaling policies are commonly used in industries such as Internet, gaming, and social media.
  • Hybrid auto scaling policies are suitable for scenarios in which an application needs to use resources within a specific period of time and burst traffic and periodic traffic occur when the application uses the resources. Hybrid auto scaling policies are commonly used in industries such as Internet, education, and catering.

For more information, see Configure an auto scaling policy.

Enable a scheduled auto scaling policy

This example shows how to create an application in the China (Hangzhou) region, deploy the application in image mode and configure a scheduled auto scaling policy for the application.

In this example, the scheduled auto scaling policy is executed at an interval of one day. The policy starts to be executed the first time for five instances at 19:35 of a day. The policy starts to be executed the second time for two instances at 20:35 of the same day. SAE retains five instances of the application from 19:35 to 20:35 of the day and retains two instances of the application from 20:35 of the day to 19:35 of the next day based on the scheduled auto scaling policy.

  1. Create a project folder named terraform for storing Terraform resources.
  2. Run the following command to go to the project directory:
    cd terraform
  3. Create a configuration file named main.tf.
    The following code shows the content of the main.tf file:
    terraform {
      required_providers {
        alicloud = {
          source  = "hashicorp/alicloud"
          version = "~> 1.167.0"
        }
      }
    }
    
    # The namespace.
    resource "alicloud_sae_namespace" "default" {
      namespace_description = var.namespace_description
      namespace_id          = var.namespace_id
      namespace_name        = var.namespace_name
    }
    
    # The virtual private cloud (VPC) and security group.
    resource "alicloud_security_group" "sg" {
      name        = var.name
      description = var.description
      vpc_id      = module.vpc.VPC_ID
    }
    
    resource "alicloud_security_group_rule" "sg_rule" {
      type              = "ingress"
      ip_protocol       = "tcp"
      nic_type          = "intranet"
      policy            = "accept"
      port_range        = var.port_range
      priority          = 1
      security_group_id = alicloud_security_group.sg.id
      cidr_ip           = var.cidr_ip
    }
    
    module "vpc" {
      source  = "git::github.com/kubevela-contrib/terraform-modules.git//alibaba/vswitch"
      zone_id = var.zone_id
    }
    
    
    # The configurations of the application.
    resource "alicloud_sae_application" "default" {
      app_name          = var.app_name
      app_description   = var.app_description
      deploy            = true
      image_url         = var.image_url
      namespace_id      = alicloud_sae_namespace.default.id
      vswitch_id        = module.vpc.VSWITCH_ID
      vpc_id            = module.vpc.VPC_ID
      security_group_id = alicloud_security_group.sg.id
      package_type      = var.package_type
      timezone          = "Asia/Beijing"
      replicas          = var.replicas
      cpu               = var.cpu
      memory            = var.memory
    }
    
    # The auto scaling policy.
    resource "alicloud_sae_application_scaling_rule" "example" {
      app_id              = alicloud_sae_application.default.id
      scaling_rule_name   = "example-value"
      scaling_rule_enable = true
      scaling_rule_type   = "timing"
      scaling_rule_timer {
        begin_date = "2022-04-20"
        end_date   = "2022-05-31"
        period     = "* * *"
        schedules {
          at_time      = "19:35"
          target_replicas = 5
        }
        schedules {
          at_time      = "20:35"
          target_replicas = 2
        }
      }
    }
    
    
    # The description of the namespace.
    variable "namespace_description" {
      description = "Namespace Description"
      default     = "a namespace"
    }
    # The name of the namespace.
    variable "namespace_name" {
      description = "Namespace Name"
      type = string
    }
    # The ID of the namespace.
    variable "namespace_id" {
      description = "Namespace ID"
      type = string
    }
    
    output "namespace_id" {
      value = var.namespace_id
      description = "Namespace ID"
    }
    # The name of the security group.
    variable "name" {
      default     = "tf"
      description = "The name of the security group rule"
      type        = string
    }
    
    # The description of the security group.
    variable "description" {
      default     = "The description of the security group rule"
      description = "The description of the security group rule"
      type        = string
    }
    
    # The port range.
    variable "port_range" {
      default     = "1/65535"
      description = "The port range of the security group rule"
      type        = string
    }
    
    # The Classless Inter-Domain Routing (CIDR) block.
    variable "cidr_ip" {
      description = "cidr blocks used to create a new security group rule"
      type        = string
      default     = "0.0.0.0/0"
    }
    
    # The ID of the available zone in the region.
    variable "zone_id" {
      description = "Availability Zone ID"
      type        = string
      default     = "cn-hongkong-b"
    }
    
    # The name of the application.
    variable "app_name" {
      description = "The name of the application"
      type        = string
    }
    
    # The description of the application.
    variable "app_description" {
      default     = "description created by Terraform"
      description = "The description of the application"
      type        = string
    }
    
    # The deployment method of the application.
    variable "package_type" {
      default     = "Image"
      description = "The package type of the application"
      type        = string
    }
    
    # The number of CPU cores of the instance.
    variable "cpu" {
      default     = "500"
      description = "The cpu of the application, in unit of millicore"
      type        = string
    }
    
    # The memory size of the instance.
    variable "memory" {
      default     = "1024"
      description = "The memory of the application, in unit of MB"
      type        = string
    }
    
    # The number of application instances.
    variable "replicas" {
      default     = "1"
      description = "The replicas of the application"
      type        = string
    }
    
    # The port of Server Load Balancer (SLB) instance.
    variable "port" {
      description = "The port of SLB"
      type        = string
      default = "8000"
    }
    
    # The address of the image.
    variable "image_url" {
      description = "The image url of the application, like `registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-slim:0.9`"
      type        = string
    }
    
    output "app_id" {
      description = "The id of the application"
      value       = alicloud_sae_application.default.id
    }
    
    output "app_name" {
      description = "The name of the application"
      value       = var.app_name
    }
  4. Run the following command to initialize the configurations:
    terraform init
  5. Run the following commands in sequence to create an application.
    1. Run the following command to deploy the application:
      terraform apply
    2. Enter the following information of the application in sequence as prompted:
      • app_name: the name of the application. Enter app-scaling.
      • image_url: the address of the image. Enter registry.cn-hangzhou.aliyuncs.com/****/****:01.
        You can log on to the Container Registry console and view the image addresses on the Details page of the image repository that belongs to your instance. Format:
        registry.<regionId>.aliyuncs.com/<Namespace name>/<Repository name>:<Image version>
      • namespace_name: the name of the namespace. Enter demo.
      • namespace_id: the ID of the namespace. Enter cn-hangzhou:demo.
      Expected output:
      ...
      
      Plan: 7 to add, 0 to change, 0 to destroy.
      
      Changes to Outputs:
        + app_id       = (known after apply)
        + app_name     = "app-scaling"
        + namespace_id = "cn-hangzhou:demo"
      alicloud_sae_namespace.default: Creating...
      module.vpc.alicloud_vpc.vpc[0]: Creating...
      alicloud_sae_namespace.default: Creation complete after 4s [id=cn-hangzhou:demo]
      module.vpc.alicloud_vpc.vpc[0]: Creation complete after 6s [id=vpc-bp1e2c4vcopwnmnef****]
      ...
      alicloud_sae_application_scaling_rule.example: Creating...
      alicloud_sae_application_scaling_rule.example: Creation complete after 1s [id=f8982c19-0f91-4510-a26a-9644e97c****:example-value]
      
      Apply complete! Resources: 7 added, 0 changed, 0 destroyed.
      
      Outputs:
      
      app_id = "f8982c19-0f91-4510-a26a-9644e97c****"
      app_name = "app-scaling"
      namespace_id = "cn-hangzhou:demo"
    The app-scaling application is created and a scheduled auto scaling policy is enabled for the application. You can log on to the SAE console and view the auto scaling policy and instance status on the Instance Information tab of the Basic Information page of the application.

Enable a metric-based auto scaling policy

This example shows how to enable a metric-based auto scaling policy based on the main.tf file that is created to configure a scheduled auto scaling policy. The setting of the alicloud_sae_application_scaling_rule resource is replaced by the configurations of the metric-based auto scaling policy. Other settings remain unchanged. For more information, see Enable a scheduled auto scaling policy.

Sample code:
  • Configurations of the scheduled auto scaling policy before the update
    resource "alicloud_sae_application_scaling_rule" "example" {
      app_id              = alicloud_sae_application.default.id
      scaling_rule_name   = "example-value"
      scaling_rule_enable = true
      scaling_rule_type   = "timing"
      scaling_rule_timer {
        begin_date = "2022-04-20"
        end_date   = "2022-05-31"
        period     = "* * *"
        schedules {
          at_time      = "19:35"
          target_replicas = 5
        }
        schedules {
          at_time      = "20:35"
          target_replicas = 2
        }
      }
    }
  • Configurations of the metric-based auto scaling policy after the update
    resource "alicloud_sae_application_scaling_rule" "metrics" {
      app_id              = alicloud_sae_application.default.id
      scaling_rule_name   = "metric-dev"
      scaling_rule_enable = true
      scaling_rule_type   = "metric"
      scaling_rule_metric {
        max_replicas = 50
        min_replicas = 3
        metrics {
          metric_type                       = "CPU"
          metric_target_average_utilization = 1
        }
        scale_up_rules {
          step                         = 10
          disabled                     = false
          stabilization_window_seconds = 0
        }
        scale_down_rules {
          step                         = 10
          disabled                     = false
          stabilization_window_seconds = 10
        }
      }
    }
    In this example, if the CPU utilization exceeds 1%, the application is automatically scaled out and a maximum of 50 instances can be used. If the CPU utilization drops below 1%, the application is automatically scaled in and a minimum of three instances can be used.
    Note A low CPU utilization is specified in the sample code to show the test result. You can specify parameter values based on your business requirements.

After you run the required commands to apply the modified Terraform resource, the metric-based auto scaling policy is enabled for the application. You can log on to the SAE console and view the auto scaling policy and instance status on the Instance Information tab of the Basic Information page of the application.

Enable a hybrid auto scaling policy

This example shows how to enable a hybrid auto scaling policy based on the main.tf file that is created to configure a scheduled auto scaling policy. The setting of the alicloud_sae_application_scaling_rule resource is replaced by the configurations of the hybrid auto scaling policy. Other settings remain unchanged. For more information, see Enable a scheduled auto scaling policy.

Sample code:
  • Configurations of the scheduled auto scaling policy before the update
    resource "alicloud_sae_application_scaling_rule" "example" {
      app_id              = alicloud_sae_application.default.id
      scaling_rule_name   = "example-value"
      scaling_rule_enable = true
      scaling_rule_type   = "timing"
      scaling_rule_timer {
        begin_date = "2022-04-20"
        end_date   = "2022-05-31"
        period     = "* * *"
        schedules {
          at_time      = "19:35"
          target_replicas = 5
        }
        schedules {
          at_time      = "20:35"
          target_replicas = 2
        }
      }
    }
  • Configurations of the hybrid auto scaling policy after the update
    resource "alicloud_sae_application_scaling_rule" "metrics" {
      app_id              = alicloud_sae_application.default.id
      scaling_rule_name   = "metric-dev"
      scaling_rule_enable = true
      scaling_rule_type   = "mix"
    
      scaling_rule_timer {
        begin_date = "2022-04-20"
        end_date   = "2022-05-31"
        period     = "* * *"
        schedules {
          at_time      = "19:45"
          max_replicas = 50
          min_replicas = 10
        }
        schedules {
          at_time      = "20:45"
          max_replicas = 40
          min_replicas = 3
        }
    
      }
      scaling_rule_metric {
        max_replicas = 40
        min_replicas = 3
        metrics {
          metric_type                       = "CPU"
          metric_target_average_utilization = 1
        }
        scale_up_rules {
          step                         = 10
          disabled                     = false
          stabilization_window_seconds = 0
        }
        scale_down_rules {
          step                         = 10
          disabled                     = false
          stabilization_window_seconds = 10
        }
      }
    }
    The following information describes the content of the hybrid auto scaling policy:
    • Common period of time: If the CPU utilization exceeds 1%, the application is automatically scaled out and a maximum of 40 instances can be used. If the CPU utilization drops below 1%, the application is automatically scaled in and a minimum of three instances can be used.
    • Specific period of time: From April 20, 2022 to May 31, 2022, the scheduled auto scaling policy is executed based on the CPU utilization threshold.
      • From 19:45 to 20:45 of a day within the specified period of time, a minimum of 10 instances and a maximum of 50 instances can be used in the application.
      • From 20:45 of a day to 20:45 of the next day within the specified period of time, a minimum of three instances and a maximum of 40 instances can be used in the application.
    Note A low CPU utilization is specified in the sample code to show the test result. You can specify parameter values based on your business requirements.

After you run the required commands to apply the modified Terraform resource, the hybrid auto scaling policy is enabled for the application. You can log on to the SAE console and view the auto scaling policy and instance status on the Instance Information tab of the Basic Information page of the application.

Disable an auto scaling policy and delete an application

This section provides an example on how to disable an auto scaling policy and delete the application for which the auto scaling policy is enabled. In this example, the app-scaling application that resides in the China (Hangzhou) region is deleted.

  1. Run the following command in the project directory to execute the configuration file:
    terraform destroy
  2. To delete the application, enter the following information of the application in sequence as prompted:
    • app_name: Enter app-scaling.
    • image_url: Enter registry.cn-hangzhou.aliyuncs.com/****/****:01.
      Format:
      registry.<regionId>.aliyuncs.com/<Namespace name>/<Repository name>:<Image version>

      You can log on to the Container Registry console and view the image addresses on the Details page of the image repository that belongs to your instance.

    • namespace_id: the ID of the namespace. Enter cn-hangzhou:demo.
    • namespace_name: the name of the namespace. Enter demo.
    Expected output:
    ...
    
    module.vpc.alicloud_vpc.vpc[0]: Refreshing state... [id=vpc-bp1e2c4vcopwnmnef****]
    alicloud_sae_namespace.default: Refreshing state... [id=cn-hangzhou:demo]
    ...
    alicloud_sae_application_scaling_rule.metrics: Refreshing state... [id=f8982c19-0f91-4510-a26a-9644e97c****:metric-dev]
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      - destroy
    
    ...
    
    Plan: 0 to add, 0 to change, 7 to destroy.
    
    Changes to Outputs:
      - app_id       = "f8982c19-0f91-4510-a26a-9644e97c****" -> null
      - app_name     = "app-scaling" -> null
      - namespace_id = "cn-hangzhou:demo" -> null
    alicloud_security_group_rule.sg_rule: Destroying... [id=sg-bp13cgwyi5o2h0g8****:ingress:tcp:1/65535:intranet:0.0.0.0/0:accept:1]
    
    module.vpc.alicloud_vpc.vpc[0]: Destroying... [id=vpc-bp1e2c4vcopwnmnef****]
    module.vpc.alicloud_vpc.vpc[0]: Destruction complete after 6s
    
    Destroy complete! Resources: 7 destroyed.
    The auto scaling policy is disabled and the app-scaling application is deleted.