Auto Scaling automatically adds and removes Elastic Compute Service (ECS) instances based on your business requirements. To provide scaling services that are more flexible, Auto Scaling allows you to configure the following settings in scaling configurations to customize ECS instances: tags, Secure Shell (SSH) key pairs, Resource Access Management (RAM) roles, and user data. This topic describes tags, SSH key pairs, RAM roles, and user data, and how to configure them in scaling configurations.

Prerequisites

Before performing the instructions provided in this tutorial, make sure that you have an Alibaba Cloud account. To create an Alibaba Cloud account, click Create a new Alibaba Cloud account.

Background information

Auto Scaling not only can scale ECS instances during peak or off-peak traffic hours, but also can customize the ECS instances that are created. Auto Scaling allows you to set various parameters of scaling configurations to customize ECS instances efficiently and flexibly based on your business requirements.

  • Tag

    For more information about tags, see Tag overview. Tags can be used to identify resources and user groups. With tags, enterprises and individuals can categorize their ECS resources to make it easier to search for and aggregate resources. When you create a scaling configuration, you can select the tags to be attached to the ECS instances created based on the scaling configuration.

    If you call an API operation to create a scaling configuration, you can specify the tags in the Tags parameter. For more information, see CreateScalingConfiguration.

  • SSH key pair

    For more information about SSH key pairs, see SSH key pair overview. Alibaba Cloud only supports 2048-bit RSA key pairs. SSH key pairs only apply to Linux instances. After an SSH key pair is created, Alibaba Cloud holds the public key and offers you the private key.

    Compared with using a password, using an SSH key pair to log on to a Linux instance is more efficient and secure. You can specify an SSH key pair when creating a scaling configuration. After Auto Scaling creates an ECS instance by using the scaling configuration, the instance stores the public key of the specified SSH key pair. You can use the private key to log on to the ECS instance from your local device.

    If you call an API operation to create a scaling configuration, you can specify an SSH key pair in the KeyPairName parameter. For more information, see CreateScalingConfiguration.

  • RAM role

    Resource Access Management (RAM) is a service provided by Alibaba Cloud for managing user identities and resource access permissions. RAM allows you to create different roles and grant different permissions on Alibaba Cloud services to each role.

    For more information about RAM roles, see What is the RAM role of an instance. ECS instances can assume a RAM role to obtain the permissions granted to the RAM role. When you specify a RAM role in a scaling configuration, make sure that ECS has been set to the trusted entity of the RAM role. Otherwise, Auto Scaling cannot create ECS instances based on the scaling configuration.

    If you call an API operation to create a scaling configuration, you can specify a RAM role in the RamRoleName parameter. For more information, see CreateScalingConfiguration.

  • User data
    For more information about user data of ECS instances, see User data. Both Windows and Linux instances support user data. You can use user data in the following ways:
    • Configure a script that is run when an ECS instance starts. In this way, you can customize the startup behavior of the ECS instance.
    • Pass data to an ECS instance. You can reference the data on the ECS instance.
    Compared with using open-source IT infrastructure management tools such as Terraform, using user data that is supported natively by Auto Scaling to manage the infrastructure is more efficient and secure. You only need to configure a Base64-encoded custom script and pass the script to a scaling configuration as user data. ECS instances created based on the scaling configuration can run the script during startup to automatically deploy applications. In this way, you can scale applications. When you use user data, note that:
    • The network type of the scaling group must be Virtual Private Cloud (VPC).
    • The user data must be Base64-encoded.
    • We recommend that you do not configure confidential information, such as passwords and keys, in user data because user data is passed to instances in plaintext. If you must pass confidential information, we recommend that you encrypt the confidential information before encoding it in Base64 and decrypt it on the instance.

    If you call an API operation to create a scaling configuration, you can configure user data in the UserData parameter. For more information, see CreateScalingConfiguration.

Proper use of Auto Scaling can reduce your server cost, service management cost, and O&M cost. To help you understand and properly use Auto Scaling, this topic demonstrates how to configure the preceding settings in a scaling configuration for Auto Scaling to automatically scale and customize ECS instances. Specifically, this topic demonstrates how to configure tags, an SSH key pair, a RAM role, and user data containing a custom script in a scaling configuration. When an ECS instance is created based on the scaling configuration, the tags are attached to the ECS instance, and the ECS instance assumes the RAM role. You can use the SSH key pair to log on the ECS instance. The custom script is automatically run when the ECS instance starts.

Procedure

Follow these steps to configure custom settings, including tags, an SSH key pair, a RAM role, and user data, in a scaling configuration:
  1. Step 1: Prepare custom settings
  2. Step 2: Apply the preceding settings
  3. Step 3: Verify the preceding settings

Step 1: Prepare custom settings

Follow these steps to create tags, an SSH key pair, a RAM role, and user data:

  1. Create tags.
    For more information, see Add a tag to resources.
  2. Create an SSH key pair.
    For more information, see Use an SSH key pair.
  3. Create a RAM role.
    For more information, see Create a RAM role for a trusted Alibaba Cloud service. You can also use an existing RAM role. When you specify a RAM role in a scaling configuration, make sure that ECS has been set to the trusted entity of the RAM role. Otherwise, Auto Scaling cannot create ECS instances based on the scaling configuration. For example, the RAM role AliyunECSImageExportDefaultRole grants the permission of exporting images. The policy of the RAM role allows all ECS instances under the current account to assume this RAM role. The policy content is as follows:
    {
        "Statement": [
            {
                "Action": "sts:AssumeRole",
                "Effect": "Allow",
                "Principal": {
                    "Service": [
                        "ecs.aliyuncs.com"
                    ]
                }
            }
        ],
        "Version": "1"
    }
    Note In the policy content, ecs.aliyuncs.com indicates that all ECS instances under the current account can assume this RAM role.
  4. Prepare user data.
    For more information, see User data. In this example, a Shell script is provided in user data to write the following string to the /root/output10.txt file when an ECS instance starts for the first time: Hello World. The time is now {Current time}. The script is as follows:
    #!/bin/sh
    echo "Hello World.  The time is now $(date -R)!" | tee /root/output10.txt
    The Base64-encoded string of the script is as follows:
    IyEvYmluL3NoDQplY2hvICJIZWxsbyBXb3JsZC4gIFRoZSB0aW1lIGlzIG5vdyAkKGRhdGUgLVIpISIgfCB0ZWUgL3Jvb3Qvb3V0cHV0MTAudHh0 

Step 2: Apply the preceding settings

Follow these steps to create a scaling group and a scaling configuration and apply the preceding settings in the scaling configuration:

  1. Create a scaling group.
    For more information, see Use custom scaling configurations to create scaling groups. Note that:
    • Minimum Instances: Set this parameter to 1. An ECS instance is created after the scaling group is enabled.
    • Instance Configuration Source: Select Custom Scaling Configuration for this parameter.
    • Network Type: Select VPC for this parameter and set the VPC ID and VSwitch parameters.
    Create a scaling group
  2. In the The scaling group has been created dialog box that appears, click Create Now to create a scaling configuration.
  3. Create a scaling configuration.
    For more information, see Create a scaling configuration. Note that:
    • On the Basic Configurations page, select Ubuntu 16.04 64-bit as the image.
    • On the System Configurations page, select the tags, SSH key pair, RAM role, and user data that are created in Step 1.
  4. In the Created dialog box, click Enable Configuration.
  5. In the Apply Scaling Configuration dialog box, click OK.
  6. In the Enable Scaling Group dialog box, click OK.

Step 3: Verify the preceding settings

In Step 2, the minimum number of instances in the scaling group is set to 1. Therefore, the scaling group automatically creates an ECS instance after it is enabled.

  1. View the automatically created ECS instance.
    For more information, see Query ECS instances.View the ECS instance
  2. In the Instance Name/ID column, click the target instance ID to view details of the instance.
    The following figure shows details of the instance. You can find that the instance has assumed the RAM role and the tags have been attached to the instance.View instance details
  3. Use the SSH key pair to log on to the instance.
    For more information, see Connect to a Linux instance by using an SSH key pair. The following figure shows a successful logon. This indicates that the SSH key pair takes effect.Use the SSH key pair to log on to the instance
  4. Run the following command to view the content of the /root/output10.txt file:
    cat /root/output10.txt
    The following figure shows that the file contains the specified string. This indicates that the user data configured in the scaling configuration takes effect.User data that takes effect
    Note This topic uses a simple Shell script as an example. You can create a script based on your requirements to customize more startup behaviors.