To provide scaling services that are more efficient and flexible, Auto Scaling allows you to configure user data in scaling configurations to customize ECS instances. You can use this feature to automatically configure ECS instances, for example, to automatically install applications on ECS instances. This allows you to scale applications in a more secure and quick manner.


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.

To verify the effect of user data, you must log on to ECS instances. We recommend that you use Secure Shell (SSH) key pairs to log on to Linux instances. For more information, see Use an SSH key pair and Connect to a Linux instance by using an SSH key pair.

Background information

This topic uses an example to describe how to use user data in Auto Scaling. You can customize user data based on your business requirements.

For more information about user data, 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.

In addition to user data, you can use SSH key pairs, Resource Access Management (RAM) roles, and tags to customize ECS instances efficiently and flexibly. For more information, see Set parameters of scaling configurations to customize ECS instances.


Follow these steps to configure user data in a scaling configuration:
  1. Step 1: Prepare user data
  2. Step 2: Create and enable a scaling group
  3. Step 3: Verify the user data

Step 1: Prepare user data

You can configure a custom Shell script in user data and enable the script to run when ECS instances start. When you customize a Shell script, note that:
  • Format: The first line must start with #!, such as #!/bin/sh.
  • Limit: The script size cannot exceed 16 KB before the script is Base64 encoded.
  • Frequency: The script is run only when instances are started for the first time.
  1. Customize a Shell script to configure the Domain Name System (DNS), yum, and Network Time Protocol (NTP) services when an ECS instance starts.
    The Shell script is as follows:
    # Modify DNS
    echo "nameserver" | tee /etc/resolv.conf
    # Modify yum repo and update
    rm -rf /etc/yum.repos.d/*
    touch myrepo.repo
    echo "[base]" | tee /etc/yum.repos.d/myrepo.repo
    echo "name=myrepo" | tee -a /etc/yum.repos.d/myrepo.repo
    echo "baseurl=" | tee -a /etc/yum.repos.d/myrepo.repo
    echo "gpgcheck=0" | tee -a /etc/yum.repos.d/myrepo.repo
    echo "enabled=1" | tee -a /etc/yum.repos.d/myrepo.repo
    yum update -y
    # Modify NTP Server
    echo "server" | tee /etc/ntp.conf
    systemctl restart ntpd.service
  2. Encode the Shell script in Base64.
    The Base64-encoded Shell script is as follows:

Step 2: Create and enable a scaling group

  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.
  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 user data prepared in Step 1 and select an existing SSH key pair for logging on to ECS instances created based on this scaling configuration.
  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 user data

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 scaling activity.
    For more information, see View scaling activities.
  2. Log on to the instance.
  3. Check the status of services on the instance.
    The following figure shows that the DNS, yum, and NTP services are enabled on the ECS instance. This indicates that the user data configured in the scaling configuration takes effect.