To provide more efficient and flexible scaling services, Auto Scaling allows you to configure user data in scaling configurations to customize ECS instances. You can pass in user data to perform automated configuration tasks on ECS instances, such as installing applications on ECS instances. This allows you to scale applications in a more secure and efficient manner.
Before you perform the operations provided in the tutorial, you must have registered 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 Create an SSH key pair and Connect to a Linux instance by using an SSH key pair.
An example is used in this topic to describe how to use user data in Auto Scaling. You can customize user data based on your business requirements.
- 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.
- 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 private 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 based on Base64 and decrypt the information on the instance.
If you call an API operation to create a scaling configuration, you can use the UserData parameter to configure user data. 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.
Step 1: Prepare user data
- Format: The first line must start with
#!, such as
- Limit: The script size cannot exceed 16 KB before the script is encoded in Base64.
- Frequency: The script is run only when instances are started for the first time.
- Customize a shell script to configure the Domain Name System (DNS), Yellowdog Updater,
Modified (YUM), and Network Time Protocol (NTP) services when an ECS instance starts.
The following section shows the shell script:
#! /bin/sh # Modify DNS echo "nameserver 22.214.171.124" | 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=http://mirror.centos.org/centos" | 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 ntp1.aliyun.com" | tee /etc/ntp.conf systemctl restart ntpd.service
- Encode the shell script in Base64.
The following section shows the Base64-encoded shell script:
Step 2: Create and enable a scaling group
- Create a scaling group and view details of the scaling group after it is created.
For more information, see Create a scaling group. Take note of the following items:
- Minimum Number of Instances: Set this parameter to 1. An ECS instance is automatically created after the scaling group is enabled.
- Instance Template Source: Set this parameter to Create from Scratch.
- Network Type: Set this parameter to VPC and specify a VPC and a VSwitch.
- Create and enable a scaling configuration after it is created.
For more information, see Create a scaling configuration. Take note of the following items:
- In the Basic Configurations step, set Image to Ubuntu 16.04 64-bit.
- In the System Configurations (Optional) step, pass in the user data that were created in Step 1 and select an existing SSH key pair.
- Enable the scaling group.
For more information, see Enable a scaling group.
Step 3: Verify the user data
The minimum number of instances in the scaling group is set to 1. Therefore, an ECS instance is automatically created after the scaling group is enabled.
- View the scaling activity.
For more information, see View the details of a scaling activity.
- Log on to the ECS instance.
For more information, see Connect to a Linux instance by using an SSH key pair.
- 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 is in effect.