CLI integration example

更新时间:
复制 MD 格式

Alibaba Cloud Command Line Interface (Alibaba Cloud CLI) is a command-line tool built on OpenAPI that you can use to automate the management of ApsaraMQ for Kafka. This topic uses the GetAllInstanceIdList API operation as an example to show how to call ApsaraMQ for Kafka OpenAPI operations to query the IDs of all ApsaraMQ for Kafka instances under your account.

Before you start

If you are new to Alibaba Cloud CLI, see What is Alibaba Cloud CLI.

Step 1: Install Alibaba Cloud CLI

You must install Alibaba Cloud CLI before you can use it. Alibaba Cloud CLI provides installation packages for Windows, Linux, and macOS. Select the installation package for your operating system.

Alternatively, you can debug Alibaba Cloud CLI commands in Cloud Shell. Cloud Shell comes with Alibaba Cloud CLI pre-installed and is updated regularly. For more information, see What is Cloud Shell?.

Step 2: Configure Alibaba Cloud CLI

Important

An Alibaba Cloud account has full permissions to manage and access all OpenAPI operations, which poses a high security risk. We strongly recommend that you create a RAM identity, grant permissions based on the principle of least privilege, and use the RAM identity to access OpenAPI operations. For information about the policies supported by ApsaraMQ for Kafka, see System policies for ApsaraMQ for Kafka and Custom policies for ApsaraMQ for Kafka.

Before you can use Alibaba Cloud CLI, you must configure your credentials and region ID. Alibaba Cloud CLI supports multiple types of credentials. For more information, see Configure and manage credentials. This topic uses AccessKey credentials as an example. Perform the following steps:

  1. Create a RAM user and an AccessKey. You will use this AccessKey to configure your credentials. For more information, see Create a RAM user and Create an AccessKey.

  2. Grant permissions to the RAM user. For this example, attach the AliyunKafkaReadOnlyAccess policy to grant the RAM user read-only permissions on ApsaraMQ for Kafka. For more information, see Manage RAM user permissions.

  3. Obtain an available region ID for credential configuration. Alibaba Cloud CLI uses the region that you specify to initiate OpenAPI calls. To see the regions where ApsaraMQ for Kafka is available, see Endpoints.

    Note

    When you use Alibaba Cloud CLI, you can specify a region by using the --region option. If you specify this option, Alibaba Cloud CLI ignores the region information in the default credential configuration and environment variables. For more information, see Command-line options.

  4. Use the RAM user's AccessKey to configure AccessKey credentials and name the profile AkProfile. For more information, see Configure and manage credentials.

Step 3: Generate a CLI example

  1. In OpenAPI Explorer, go to the debugging page for GetAllInstanceIdList.

  2. On the Parameter Settings tab, enter the request parameters. Then, click the CLI Example tab to view the generated sample command.

  3. Copy the sample command or run it directly in Cloud Shell:

    • Click the Run Command image icon to open Cloud Shell and debug the command.

    • Click the Copy image icon to copy the sample command to your clipboard. You can then paste it into your local shell to run.

    Note

    If you copy the sample command to a local shell tool for debugging, make sure that the parameter format is correct. For more information about the parameter format of Alibaba Cloud CLI commands, see Command parameters.

    By default, the sample command generated in OpenAPI Explorer includes the --region option. If you copy the command to run it locally, Alibaba Cloud CLI uses the specified region and ignores the region information in the default credential configuration and environment variables. You can remove or keep this option based on your needs.

Step 4: Call ApsaraMQ for Kafka OpenAPI

Example 1: List ApsaraMQ for Kafka operations

Use the --help option to list the ApsaraMQ for Kafka OpenAPI operations that Alibaba Cloud CLI supports. You can also view the supported OpenAPI operations in API overview.

  1. Run the command.

    aliyun alikafka --help
  2. Output:

    Product: alikafka (ApsaraMQ for Kafka)
    Version: 2019-09-16
    Available Api List:
      ChangeResourceGroup       Changes the resource group of a resource.
      ConvertPostPayOrder       Converts a post-pay instance to a pre-pay instance.
      CreateAcl                 Creates an access control list (ACL).
      CreateConsumerGroup       Creates a consumer group.
      CreatePostPayOrder        Creates a post-pay instance. Post-pay instances are suitable for testing or short-term scenarios with unpredictable traffic peaks.
      CreatePrePayOrder         Creates a pre-pay instance. Pre-pay instances are suitable for long-term, stable business scenarios.
      CreateSaslUser            Creates a Simple Authentication and Security Layer (SASL) user.

Example 2: Query instances

This example shows how to use Alibaba Cloud CLI to call the GetAllInstanceIdList operation of ApsaraMQ for Kafka and query the IDs of all ApsaraMQ for Kafka instances under your account.

  1. Run the command.

    aliyun alikafka GetAllInstanceIdList --RegionId cn-hangzhou
  2. Output:

    {
      "Code": 200,
      "Message": "operation success.",
      "InstanceIds": [
        {
          "cn-shenzhen": [
            "alikafka_post-cn-7pp2btvo****"
          ],
          "us-west-1": [
            "alikafka_pre-cn-i7m2lxid****"
          ],
          "cn-hangzhou": [
            "alikafka_pre-cn-i7m2hflj****",
            "alikafka_pre-cn-zvp2hsje****",
            "alikafka_pre-cn-zvp2kvc9****"
          ]
        }
      ],
      "RequestId": "ABA4A7FD-E10F-45C7-9774-A5236015****",
      "Success": true
    }
    Note

    If you receive an error when calling an ApsaraMQ for Kafka OpenAPI operation, use the error code to verify that your request parameters and values are valid.

    Alternatively, you can record the returned RequestID or SDK error message and use OpenAPI problem diagnosis to diagnose the problem.