To deliver more efficient and flexible scaling services, Auto Scaling now supports instance user data. Use instance user data to automatically configure ECS instances and securely scale your applications in or out.
Background information
Compared with open source infrastructure management tools such as Terraform, using native instance user data in Auto Scaling is faster and more secure. Prepare your instance user data script, encode it in Base64, and include it in the scaling configuration. Automatically created ECS instances will run the script at startup to enable application-level scaling.
This topic demonstrates how to use instance user data in a real-world scenario. For more information about instance user data, see Customize instance initialization configuration. Both Windows and Linux instances support instance user data. Key capabilities include the following:
-
Customization: Run a custom script when the instance starts to define its startup behavior.
-
Data reference: Pass plain data into the instance and reference it from within the instance.
Considerations
-
When using instance user data to automatically configure ECS instances, note the following:
-
The scaling group must use a virtual private cloud (VPC) network type.
-
Instance user data must be Base64-encoded.
-
Instance user data is passed to the instance without encryption. Do not pass sensitive information (such as passwords or private keys) in plaintext. If you must pass sensitive data, encrypt it first, Base64-encode the encrypted data, pass it into the instance, and then decrypt it inside the instance using the same method.
-
-
When creating a scaling configuration using an API, use the UserData parameter to pass instance user data. For more information, see CreateScalingConfiguration.
-
In addition to instance user data, SSH key pairs, RAM role names, and tags can help you manage ECS instances more efficiently and flexibly. For more information, see Use scaling configuration features to implement automatic scaling.
Step 1: Prepare instance user data
You can use instance user data to automatically run a custom shell script when an ECS instance starts. When writing the shell script, follow these guidelines:
|
Type |
Description |
|
Script format |
The first line must start with |
|
Script limit |
The script content must not exceed 32 KB before Base64 encoding. |
|
Script execution frequency |
The script runs only once during the first instance startup. |
-
Create a shell script that configures yum and NTP services when the ECS instance starts.
The following example shows a sample shell script:
#!/bin/sh # 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 -
Base64-encode the shell script.
The Base64-encoded script appears as follows:
IyEvYmluL3NoCiMgTW9kaWZ5IHl1bSByZXBvIGFuZCB1cGRhdGUKcm0gLXJmIC9ldGMveXVtLnJlcG9zLmQvKgp0b3VjaCBteXJlcG8ucmVwbwplY2hvICJbYmFzZV0iIHwgdGVlIC9ldGMveXVtLnJlcG9zLmQvbXlyZXBvLnJlcG8KZWNobyAibmFtZT1teXJlcG8iIHwgdGVlIC1hIC9ldGMveXVtLnJlcG9zLmQvbXlyZXBvLnJlcG8KZWNobyAiYmFzZXVybD1odHRwOi8vbWlycm9yLmNlbnRvcy5vcmcvY2VudG9zIiB8IHRlZSAtYSAvZXRjL3l1bS5yZXBvcy5kL215cmVwby5yZXBvCmVjaG8gImdwZ2NoZWNrPTAiIHwgdGVlIC1hIC9ldGMveXVtLnJlcG9zLmQvbXlyZXBvLnJlcG8KZWNobyAiZW5hYmxlZD0xIiB8IHRlZSAtYSAvZXRjL3l1bS5yZXBvcy5kL215cmVwby5yZXBvCnl1bSB1cGRhdGUgLXkKIyBNb2RpZnkgTlRQIFNlcnZlcgplY2hvICJzZXJ2ZXIgbnRwMS5hbGl5dW4uY29tIiB8IHRlZSAvZXRjL250cC5jb25mCnN5c3RlbWN0bCByZXN0YXJ0IG50cGQuc2VydmljZQ==
Step 2: Create and enable a scaling group
-
Create a scaling group and view its details after creation.
For detailed instructions, see Configure a scaling group.
When creating the scaling group, pay attention to the following settings:
Configuration item
Description
Minimum Number of Instances
Set to 1. This ensures one ECS instance is automatically created after enabling the scaling group.
Instance Configuration Source
Select Create from Scratch.
Network Type
Select VPC.
VPC
Select an existing VPC ID.
vSwitch
Select the corresponding virtual switch.
The new scaling group appears in the scaling group list in the Disabled state. Click View Details in the Actions column to view more information.
-
Create a scaling configuration and enable it after creation.
For detailed instructions, see Create a scaling configuration (ECS instance).
When creating the scaling configuration, pay attention to the following settings:
Configuration item
Description
Instance and image
Image
Choose CentOS 7.9 64-bit from Public Image.
Management configuration
Logon Credential
Select Key Pair.
Key Pair
Select an existing key pair. For instructions on creating a key pair, see Create an SSH key pair.
Advanced Settings
The user data of the instance.
Enter the instance user data script prepared in Step 1: Prepare instance user data.
-
Enable the scaling group.
For detailed instructions, see Enable a scaling group.
Step 3: Verify the effect of instance user data
Because the Minimum Number of Instances was set to 1, the system automatically creates one ECS instance after enabling the scaling group to meet this requirement.
-
View scaling activities.
For detailed instructions, see View scaling activity details. You will see that the system has automatically created one ECS instance.
-
Log on to the ECS instance.
To verify the effect of instance user data, log on to the ECS instance. For Linux instances, we recommend using a key pair. For instructions, see Create an SSH key pair and Use OpenSSH/XShell to remotely connect to a Linux instance.
NoteIf the instance already has a logon password or an attached key pair, you can also connect using Workbench. For instructions, see Use Workbench to log on to a Linux instance.
-
Check service status.
The following output confirms that the yum and NTP services are running, which means the instance user data in the scaling configuration took effect.
[root@ixxxZ ~]# cat /etc/yum.repos.d/myrepo.repo [base] name=myrepo baseurl=http://mirror.centos.org/centos gpgcheck=0 enabled=1 [root@ixxxZ ~]# cat /etc/ntp.conf server ntp1.aliyun.com [root@ixxxZ ~]#