This topic describes how to use Terraform to create Auto Scaling resources, such as scaling groups, scaling configurations, and scaling rules.

Prerequisites

Before you use Terraform, make sure that the following requirements are met:

Background information

  • Terraform is an open source infrastructure-as-code tool that you can use to preview, configure, and manage cloud infrastructure and resources in a secure and efficient manner. Terraform allows you to write code to create, delete, query, and modify resources. For more information, see Terraform overview.
  • Terraform provides commands that are used to manage resources, such as terraform apply and terraform show, commands that are used to manage the status of resources, and other common commands. For more information, see Common commands for resource management.
    • terraform apply: creates and modifies resources. To ensure the security of resources, manual interaction is required when the command is being run. You must manually confirm whether to continue the command.
    • terraform show: displays all managed resources and their property values in the Terraform state.

Procedure

The following section describes how to use Terraform to create a scaling group, a scaling configuration, and a scaling rule. If you want to use Terraform to manage other types of Auto Scaling resources, you can repeat the following steps.

  1. Create a virtual private cloud (VPC) and a vSwitch.
    In this example, a VPC and a vSwitch are created in the cn-hangzhou-b zone.
    1. Create the terraform.tf file, add the following code to the file, and then save the file to the current working directory.
      In the following code, the values of the availability_zone and cidr_block parameters are for reference only. You can configure the parameters based on your business requirements. For information about how to select a zone where a vSwitch is available, see Regions and zones. Before you create a VPC and a vSwitch, you must create a networking plan. For more information, see Plan networks.
      resource "alicloud_vpc" "vpc" {
        name       = "tf_test_vpc"
        cidr_block = "172.16.0.0/12"         // Specify a private CIDR block for the VPC. 
      }
      
      resource "alicloud_vswitch" "vsw" {
        vpc_id            = alicloud_vpc.vpc.id
        cidr_block        = "172.16.0.0/21"  // Specify a private CIDR block for the vSwitch. 
        availability_zone = "cn-hangzhou-b"  // Specify a zone for the vSwitch. 
      }
    2. Run the terraform apply command to create the VPC and the vSwitch.
    3. Run the terraform show command to view the VPC and the vSwitch.
      You can also view the properties of the VPC and the vSwitch in the VPC console.
  2. In the VPC that you created in the previous step, create a security group. Then, add a security group rule to allow access from all IP addresses.
    1. Add the following code to the terraform.tf file:
      resource "alicloud_security_group" "security" {
        name = "tf_test_security"
        vpc_id = alicloud_vpc.vpc.id
      }
      
      resource "alicloud_security_group_rule" "allow_all_tcp" {
        type              = "ingress"
        ip_protocol       = "tcp"
        nic_type          = "intranet"
        policy            = "accept"
        port_range        = "1/65535"
        priority          = 1
        security_group_id = alicloud_security_group.security.id
        cidr_ip           = "0.0.0.0/0"
      }
    2. Run the terraform apply command to create the security group and add the security group rule.
    3. Run the terraform show command to view the security group and the security group rule.
      You can also view the security group and the security group rule in the Elastic Compute Service (ECS) console.
  3. Create a scaling group.
    In this example, a scaling group that can contain up to 100 instances is created.
    1. Add the following code to the terraform.tf file:
      resource "alicloud_ess_scaling_group" "group" {
           scaling_group_name="tf_test_scalinggroup"
           min_size=0
           max_size=100
           vswitch_ids=[alicloud_vswitch.vsw.id]
      }
    2. Run the terraform apply command to create the scaling group.
    3. Run the terraform show command to view the scaling group.
      You can also view the scaling group in the Auto Scaling console.
  4. Create a scaling configuration.
    1. Add the following code to the terraform.tf file:
      In this example, a scaling configuration that can be used to create ECS instances is created.
      resource "alicloud_ess_scaling_configuration" "configuration" {
          scaling_group_id = alicloud_ess_scaling_group.group.id
          instance_type = "ecs.hfc7.xlarge"
          image_id = "aliyun_2_1903_x64_20G_alibase_20210120.vhd"
          security_group_id = alicloud_security_group.security.id
          scaling_configuration_name = "tf_test_scalingconfiguration"
          system_disk_category = "cloud_essd"
          spot_strategy = "SpotWithPriceLimit"
          force_delete = true
      }
    2. Run the terraform apply command to create the scaling configuration.
    3. Run the terraform show command to view the scaling configuration.
      You can also view the scaling configuration in the Auto Scaling console.
  5. Create a scaling rule.
    1. Add the following code to the terraform.tf file:
      resource "alicloud_ess_scaling_rule" "rule" {
           scaling_group_id = alicloud_ess_scaling_group.group.id
           adjustment_type = "QuantityChangeInCapacity"
           adjustment_value = 1
      }
    2. Run the terraform apply command to create the scaling rule.
    3. Run the terraform show command to view the scaling rule.
      You can also view the scaling rule in the Auto Scaling console.

Examples

The following sample code provides an example on how to use Terraform to create a scaling group, a scaling configuration, and a scaling rule:

resource "alicloud_vpc" "vpc" {
  name       = "tf_test_vpc"
  cidr_block = "172.16.0.0/12"
}

resource "alicloud_vswitch" "vsw" {
  vpc_id            = alicloud_vpc.vpc.id
  cidr_block        = "172.16.0.0/21"
  availability_zone = "cn-hangzhou-b"
}
resource "alicloud_security_group" "security" {
  name = "tf_test_security"
  vpc_id = alicloud_vpc.vpc.id
}

resource "alicloud_security_group_rule" "allow_all_tcp" {
  type              = "ingress"
  ip_protocol       = "tcp"
  nic_type          = "intranet"
  policy            = "accept"
  port_range        = "1/65535"
  priority          = 1
  security_group_id = alicloud_security_group.security.id
  cidr_ip           = "0.0.0.0/0"
}

resource "alicloud_ess_scaling_group" "group" {
     scaling_group_name="tf_test_scalinggroup"
     min_size=0
     max_size=100
     vswitch_ids=[alicloud_vswitch.vsw.id]
}

resource "alicloud_ess_scaling_configuration" "configuration" {
    scaling_group_id = alicloud_ess_scaling_group.group.id
    instance_type = "ecs.hfc7.xlarge"
    image_id = "aliyun_2_1903_x64_20G_alibase_20210120.vhd"
    security_group_id = alicloud_security_group.security.id
    scaling_configuration_name = "tf_test_scalingconfiguration"
    system_disk_category = "cloud_essd"
    spot_strategy = "SpotWithPriceLimit"
    force_delete = true
}
resource "alicloud_ess_scaling_rule" "rule" {
     scaling_group_id = alicloud_ess_scaling_group.group.id
     adjustment_type = "QuantityChangeInCapacity"
     adjustment_value = 1
}