Alibaba Cloud International Agent API and CLI Automated Script Library Practical Experience
Over the past few years working in cloud operations and backend development, my biggest takeaway has been:
If a machine can do the job, definitely don’t do it manually.
I still remember when I first started to contact cloud services, the first thing I did at work every day was to open my browser, log on to ariyun console, look at the long list of ECS instances, SLB load balancing, OSS storage bucket and RDS database, and check the status one by one. When you need to create servers in batches, modify security group rules, or expand capacity in the middle of the night, you are even more in a hurry, for fear of clicking a wrong button on the webpage and changing the configuration of the production environment.
One day, I accidentally deleted the gateway for the test environment in the console due to a typo, so I stayed up all night and wrote my first automation script. At that moment, it suddenly dawned on me: the console is for beginners and for occasional troubleshooting, whereas truly efficient cloud-native operations must rely on...
API and CLI (Command-Line Interface)
.
In today's article, I 'd like to talk to you about how I built my own Alibaba Cloud automated script library. By the way, I 'd like to share some high-frequency scripts that run every day in the actual production environment, hoping to help you get rid of the bitterness of "mouse click to hand acid.
Alibaba Cloud international reseller
1. Why must you have your own automation script library?
Many people wonder: with cloud vendor consoles now so sleek and highly visual, why bother learning APIs and the CLI?
The reason is actually very simple:
Efficiency, accuracy, and replicability.
Say goodbye to the mental internal friction of repetitive work: when you need to manage 2 servers, the console is very fast; But when this number becomes 20, 200, or even a multi-zone cluster across regions, manual operation is a disaster.
Reduce Fat-finger errors (Fat-finger errors): Late-night operation and maintenance are the most prone to drowsiness, and scripts will not be tired. It is strictly executed according to the logic you have written. After passing several tests, each operation will be 100 percent accurate.
Achieve a closed loop for Infrastructure as Code (IaC): By managing resources through scripts and code, you can ensure version control, traceability of changes, and enable second-level elastic scaling and disaster recovery for your business.
2. Tool selection: To do a good job, one must first sharpen one's tools.
Alibaba Cloud international reseller
Before we start writing the script, we need to choose the right tools. Alibaba Cloud officially provides a highly comprehensive developer ecosystem, with the following core tools:
Alibaba Cloud CLI (aliyun): A command-line tool developed in Go, particularly well-suited for use in shell scripts on Linux operations servers to quickly query status and perform simple lifecycle management.
Official SDKs (Python / Go / Node.js): If your automation requirements involve complex logic, database interactions, or front-end and back-end integration, we strongly recommend using the SDK directly. What I use most often personally is
Python (alibabacloud_ecs20140526, etc.) is clean to write and has comprehensive library support.
OpenAPI Developer Portal: This is an artifact (api.aliyun.com). You can debug each API online above, and it will directly generate sample code for each language, greatly reducing the time to consult the documentation.
Tips for Basic Environment Configuration
Before you start writing the script, don’t forget to go through
aliyun configure
Configure your AccessKey ID and AccessKey Secret. Recommended to use
RAM sub-account
And give the minimum permissions, do not directly hard-code the AK/SK of the main account in the script, security is always the first.
3. actual combat exercise: four high-frequency automation scripts that make me call "really sweet"
In my daily script library, there are four scripts that are most frequently used, covering almost 80% of daily operation and maintenance scenarios.
1. Production environment snapshot automatic backup and expiration clean-up script
The data is priceless, but if the cloud disk snapshot is manually typed and deleted, it is not only time-consuming, but also easy to overcharge.
Core logic: Use the Python SDK to traverse all cloud disks with a specific tag (Tag), automatically create a snapshot of the current date, calculate the retention period, and automatically clean up the old snapshots that are older than 7 days.
Avoidance guide: Remember to add exception capture to the script. Sometimes, because the cloud disk is being mounted or other snapshot tasks are running, the creation request will report an error, and the log record can be made to sleep soundly.
2. Multi-security group whitelist automatic synchronization script
The company often has colleagues who are assigned to develop or work in other places who need to temporarily access the test server. It is a nightmare to manually change the IP address of the security group to the console.
Core logic: I wrote a small web hook paired with a CLI. When the dynamic public network IP of a colleague changes, a request is sent to the internal system, and the back-end script automatically calls the security group API of Aliyun to remove the old IP and add the new IP. The whole process takes only 3 seconds, which is safe and convenient.
3. Resource expiration inspection and alarm notification script
Business interruptions caused by expired cloud servers, domain names, and SSL certificates are often catastrophic.
Core logic: regularly (for example, every morning) query all cloud resources that are about to expire (for example, within 7 days or 3 days) under the account through CLI, and send card notifications through DingTalk or enterprise WeChat robots.
Effect: Since running this script, there has never been an embarrassing situation where the website cannot be opened due to forgetting to renew the fee.
4. Batch Inspection of Resource Status Across Availability Zones
When managing hundreds of microservice nodes, the naked eye cannot quickly determine which instance CPUs are too high for a long time and which bandwidth is full.
Core logic: Number of metrics to be pulled CloudMonitor (CloudMonitor) in batches using APIs
According to, filter out abnormal nodes and generate health reports in Markdown formats, which are thrown directly into the work group.
4. Advanced Thinking: Global Business Architecture and Cost Optimization
As the business goes overseas, many developers and overseas enterprises begin to contact with multi-account management, cross-regional network access and overseas node deployment.
Alibaba Cloud international reseller
At this stage, how to reasonably plan cloud costs and enjoy more flexible bill management and technical support has become a common concern.
If you choose to connect with a reliable one when dealing with cross-border account architecture, multi-cloud collaboration or seeking more flexible settlement and localization support.
Alibaba Cloud international reseller
It often brings a lot of convenience. Through the agent channel, enterprises can not only obtain account solutions that are more in line with their own pace of going out to sea, but also gain more flexible space in multi-account centralized management, financial compliance and resource procurement. Of course, no matter how the underlying business relationship is adjusted, the upper-level automated operation and maintenance and API scripted management are still the core of cost reduction and efficiency enhancement-after all, using code to control complex cloud resources is the hardest romance for engineers.
5. written at the end: automation has no end
Scripting the daily operation and maintenance work is not only a technical activity, but also a change in the way of thinking. Every time I see batch changes that originally required tedious clicking for half an hour, now I can type a line of command, enter the car and drink water in a few seconds. The sense of control is incomparable.
What are the immortal scripts in your automated script library? Welcome to leave a message in the comment area, let's carry out the "laziness" to the end, use the code to free your hands!
