All Products
Search
Document Center

Alibaba Cloud CLI:Generate and run CLI commands

最終更新日:Dec 18, 2024

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.

Prerequisites

Important

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.

Generate a command

Note

OpenAPI Explorer can generate all Alibaba Cloud CLI commands online. We recommend that you perform the following steps to generate a CLI command.

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

Important

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.

image

Search by service code

  1. 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.

image

  1. 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.

  2. 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, 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.

image

  • Click the image icon to run and debug the command in Cloud Shell. For more information, see What is Cloud Shell?

  • Click the image 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 parameter formats required by Alibaba Cloud CLI, 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.

  1. 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
  2. Generate the results.

    {
      "RequestId": "473469C7-AA6F-4DC5-B3DB-A3DC0DE3****",
      "InstanceId": "i-bp67acfmxazb4p****",
      "OrderId": "1234567890",
      "TradePrice": 0.165
    }
  3. Check whether an ECS instance is created by using API operations, SDKs, or the Elastic Compute Service console.