Cloud CLI (Command Line Interface) Tools are them direct and simple way to interact with a new Cloud platform, It is the perfect tool for executing simple and repeatable tasks that you don’t need to keep track of. Alibaba Cloud CLI tool which also manages Cloud resources (or stacks) is no exception. In this blog post, we will install AliCloud CLI and try a few API requests. I do that every time I explore a cloud platform (see previous posts AWS, OCI, Azure, GCP).
As usual, this post will be followed by another article about provisioning a VM in Ali Cloud through Terraforming.
The Requirements
Whether on windows or on Linux the basic installation of Aliyun CLI will always require 2 elements:
Aliyun (translated as “Ali Cloud”) was the original name of the company until 2017. Therefore you will install Aliyun CLI not Alibaba Cloud CLI ;).
-- Option 1: from the GUI
C:\> SystemPropertiesAdvanced
Note: Once the window is displayed –> click environment variables
--- Option 2: using pathed
C:\> PathEd.exe add "C:\DATA\cloud\alibaba”
C:\> aliyun version
3.0.124
Note: I used the handy pathed tool to append the %PATH% environment variable permanently
brokedba~$ curl -sL https://github.com/aliyun/aliyun-cli/releases/download/v3.0.123/aliyun-cli-linux-3.0.123-amd64.tgz | sudo tar xzC /usr/local/bin
brokedba~$ aliyun version
3.0.123
Once your Alibaba Cloud Free Tier account is created and aliyun CLI installed. You will need API credentials including region, and language. There is a list of credentials type available to use in order to authenticate.
The 2 categories are: 1. Key/StsToken based 2. Role/Instance principal based
Obtain your Access key
We will choose AccessKey authentication for our example.
Hover onto the profile picture in the upper-right corner, and click AccessKey management.
On the Access Key pair section click create AccessKey to generate AccessKey ID /AccessKey and copy them.
Now that we obtained our key pair we can finally configure our aliyun CLI profile (find all region IDs here)
1. Interactive Configuration
brokedba~$ aliyun configure --mode AK -–profile default
Configuring profile 'default' in '' authenticate mode...
Access Key Id []: AccessKey ID
Access Key Secret []: AccessKey Secret
Default Region Id []: us-east-1
Default Output Format [json]: json (Only support json)
Default Language [zh|en] en: Saving profile[default] ...Done.
Configure Done!!!
..............888888888888888888888 ........=8888888888888888888D=..............
...........88888888888888888888888 ..........D8888888888888888888888I...........
.........,8888888888888ZI: ...........................=Z88D8888888888D..........
.........+88888888 ..........................................88888888D..........
.........+88888888 .......Welcome to use Alibaba Cloud.......O8888888D..........
.........+88888888 ............. ************* ..............O8888888D..........
.........+88888888 .... Command Line Interface(Reloaded) ....O8888888D..........
.........+88888888...........................................88888888D..........
..........D888888888888DO+. ..........................?ND888888888888D..........
...........O8888888888888888888888...........D8888888888888888888888=...........
............ .:D8888888888888888888.........78888888888888888888O ..............
2. static configuration
brokedba~$ aliyun configure set \ --profile akProfile \ --mode AK \ --region cn-hangzhou \ --access-key-id AccessKeyID \ --access-key-secret AccessKeySecret
$ aliyun configure list
Profile | Credential | Valid | Region | Language
--------- | --------------- | ------- | --------- | --------
default * | AK:****6 | Valid | us-east-1 | en
aliyun cli configuration is typically stored in the below directory
$ more $HOME/.aliyun/config.json
{
"current": "default",
"profiles": [
{
"name": "default",
"mode": "AK",
"access_key_id": xxxxxxxx
"access_key_secret":
…
Few notions worth reminding before hitting the terminal with aliyun requests :
A. Command structure: is based on the below components
$ aliyun <product> <operation> [--parameter1 value1 --parameter2 value2 ...]
Parameters:
Each is followed by values. It will always depend on the product & operation value in the command.
B. Output :
The —output option allows specifying either a field, rows (using JMESpath), or num(row number).
The best way to learn is to fetch all the output and then locate the rows(path) and columns to display.
To demonstrate the principle here’s an example based on the ECS regions list command
--- Original command
$ aliyun ecs DescribeRegions | head
{
"Regions": {
"Region": [ ----> path/rows is Regions.Region[]
{ "LocalName": "华北1(青岛)",
"RegionEndpoint": "ecs.cn-qingdao.aliyuncs.com",
"RegionId": "cn-qingdao"
},
...
--- Fileterd output based on RegionId and RegionEndPoint
$ aliyun ecs DescribeRegions --output cols=RegionId,RegionEndpoint rows=Regions.Region[]
RegionId | RegionEndpoint
-------- | --------------
us-east-1 | ecs.us-east-1.aliyuncs.com
us-west-1 | ecs.us-west-1.aliyuncs.com
eu-west-1 | ecs.eu-west-1.aliyuncs.com
me-east-1 | ecs.me-east-1.aliyuncs.com
eu-central-1 | ecs.eu-central-1.aliyuncs.com
...
Examples
You will have to rely on the help command first because there is no Alyun CLI command reference available. This is the first time I see a CSP that doesn’t have a dedicated reference for its CLI tool.
Needless to say, you’re on your own as all I found was this pdf.
•You can get help by using the following commands:
○ aliyun help
: get the product list
○ aliyun help <product>
: get the API information of a specific product
$ aliyun Ecs StopInstance help
Alibaba Cloud Command Line Interface Version 3.0.123
Product: Ecs (Elastic Compute Service)
Parameters:
--InstanceId String Required
--ConfirmStop Boolean Optional
--DryRun Boolean Optional
--ForceStop Boolean Optional
--Hibernate Boolean Optional
--StoppedMode String Optional
...
$ aliyun ecs DescribeImages --output cols=OSNameEn,Architecture,OSType,IsPublic rows=Images.Image[]
OSNameEn | Architecture | OSType | IsPublic -------- | ------------ | ------ | --------
CentOS 7.9 64 bit for SCC | x86_64 | linux | true
CentOS 8.4 64 bit for SCC | x86_64 | linux | true
AlmaLinux 9.0 64 bit | x86_64 | linux | true
Ubuntu 22.04 64 bit | x86_64 | linux | true
CentOS Stream 9 64 bit | x86_64 | linux | true
Fedora 35 64 bit | x86_64 | linux | true
Alibaba Cloud Linux 3.2104 LTS 64 bit UEFI Edition | x86_64 | linux | true Alibaba Cloud Linux 3.2104 LTS 64 bit ARM Edition | arm64 | linux | true Alibaba Cloud Linux 3.2104 LTS 64 bit | x86_64 | linux | true
Object Storage Tool:
Download and install ossutil
$ wget http://gosspublic.alicdn.com/ossutil/1.7.7/ossutil64
$ chmod 755 ossutil64
./ossutil64 config
Create a private bucket with Zonal redundancy in us-east-1 region
$ ./ossutil64 mb oss://brokedbabucket -e oss-us-east-1.aliyuncs.com --acl private --
storage-class IA --redundancy-type ZRS
Auto-Completion
This command is supposed to command to enable auto-completion in zsh/bash. But it doesn’t autocomplete operation commands as available in AWS CLI for example.
$ aliyun auto-completion
In this tutorial, we learned how to install and configure Aliyun CLI. We also described the command syntax and tried a few requests using output options.