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.
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.
- 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 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.
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 Base64 encoded.
- 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), yum, and Network
Time Protocol (NTP) services when an ECS instance starts.
The Shell script is as follows:
#!/bin/sh # Modify DNS echo "nameserver 184.108.40.206" | 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 Base64-encoded Shell script is as follows:
Step 2: Create and enable a scaling group
- 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.
- In the The scaling group has been created dialog box that appears, click Create Now to create a scaling configuration.
- 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.
- In the Created dialog box, click Enable Configuration.
- In the Apply Scaling Configuration dialog box, click OK.
- 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.
- View the scaling activity.
For more information, see View scaling activities.
- Log on to the 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 takes effect.