Alibaba Cloud CLI supports multiple Alibaba Cloud services. This topic describes how to use Alibaba Cloud CLI commands to call commonly used API operations and how to generate CLI commands in OpenAPI Explorer.
Usage notes
You may be charged for calling some API operations. Before you debug commands, make sure that you have activated the required Alibaba Cloud services. You must also learn the billing rules of these services and how to call the API operations of these services.
Before you debug commands locally, make sure that you have installed Alibaba Cloud CLI and correctly configured identity credentials. For more information, see Installation Guide and Configure identity credentials.
You can obtain the API reference of an Alibaba Cloud service and the sample code of corresponding Alibaba Cloud CLI commands in OpenAPI Explorer based on the service name. For more information, see the Generate a command section of this topic.
Syntax
In Alibaba Cloud CLI, you can use the following syntax to run commands:
aliyun <command> <subcommand> [options and parameters]
command
: the top-level command.You can specify the code of an Alibaba Cloud service supported by Alibaba Cloud CLI, such as ecs or rds.
You can also specify an Alibaba Cloud CLI command, such as help or configure.
subcommand
: the operation that you want to perform.For more information about the subcommands supported if the
command
parameter is set to configure, see Use configure subcommands.If the
command
parameter is set to the code of an Alibaba Cloud service, the subcommands supported are the API operations of the Alibaba Cloud service. For more information about the API operations of Alibaba Cloud services, see the Search by API operation name section of this topic.
options and parameters
: the options or API parameters that are used in Alibaba Cloud CLI commands. The value of an option or a parameter can be a number, string, or JSON array. For more information, see Parameter formats.The APIs of Alibaba Cloud services are classified into two styles: remote procedure call (RPC) and RESTful. Most services use the RPC style. APIs of different styles are called in different ways. For more information, see Call API in the RPC and RESTful styles.
Generate a command
OpenAPI Explorer can generate all Alibaba Cloud CLI commands online. To generate a CLI command, perform the following steps.
Step 1: Log on to OpenAPI Explorer
Log on to OpenAPI Explorer.
Step 2: Search for an API operation for which you want to generate a CLI command
You can use one of the following methods to search for an API operation in OpenAPI Explorer:
Search by API operation name
Different Alibaba Cloud services may use API operations with the same name. Carefully identify the names of API operations to prevent losses caused by accidental operations.
In OpenAPI Explorer, enter the name of the API operation that you want to call in the top or middle search box, move the pointer over the API operation, and then click Debug.
Search by service code
In OpenAPI Explorer, enter the code of an Alibaba Cloud service whose API operation you want to call in the top or middle search box, move the pointer over the service, and then click View API to go to the homepage of the service.
You can also move the pointer over Cloud Products in the top navigation bar of OpenAPI Explorer and search for the Alibaba Cloud service to which the API operation belongs to go to the homepage of the service.
On the homepage of the service, click Debug in the upper-right corner.
Step 3: Generate a CLI command
In the left-side search box of the online debugging page, search for the API operation that you want to call. In the configuration pane of the API operation that you selected, click the Parameters tab and configure the parameters based on the API reference. Then, a CLI command with the specified parameters is displayed on the CLI Example tab of the right-side pane.
Click the icon to run and debug the command in Cloud Shell. For more information, see What is Cloud Shell?
Click the icon to copy the CLI command to the clipboard and paste it to the local shell.
Pay attention to the parameter formats when you copy and paste the CLI command to the local shell. For more information about the formats of parameters in Alibaba Cloud CLI commands, see Parameter formats.
By default, OpenAPI Explorer adds the
--region
option to the generated CLI command. When you copy the command to your local shell, Alibaba Cloud CLI ignores the region information in the default identity credential configurations and environment variable settings, and preferentially uses the specified region to run the command. You can delete or retain this option based on your business requirements.
Example
The following sample code calls the CreateInstance
operation of Elastic Compute Service by using Alibaba Cloud CLI to create a pay-as-you-go ECS instance. For more information about Alibaba Cloud CLI commands, see the Generate a command section of this topic.
Run the command.
aliyun ecs CreateInstance --InstanceName myvm1 --ImageId centos_7_03_64_40G_alibase_20170625.vhd --InstanceType ecs.n4.small --SecurityGroupId sg-xxxxxx123 --VSwitchId vsw-xxxxxx456 --InternetChargeType PayByTraffic --Password xxx
Generate the results.
{ "RequestId": "473469C7-AA6F-4DC5-B3DB-A3DC0DE3****", "InstanceId": "i-bp67acfmxazb4p****", "OrderId": "1234567890", "TradePrice": 0.165 }
Check whether an ECS instance is created by using API operations, SDKs, or the Elastic Compute Service console.