CLI integration examples

更新时间:
复制 MD 格式

Alibaba Cloud Command Line Interface (Alibaba Cloud CLI) is a general-purpose command line tool built on OpenAPI. You can use Alibaba Cloud CLI to automate the management of ActionTrail. This topic provides examples of using Alibaba Cloud CLI to call ActionTrail APIs.

Prerequisites

For an introduction to Alibaba Cloud CLI, see What is Alibaba Cloud CLI?.

CLI installation

To use Alibaba Cloud CLI, you must first install it. Alibaba Cloud CLI provides installation methods for Windows, Linux, and macOS. Choose the installation method for your operating system.

You can also use Cloud Shell provided by Alibaba Cloud to debug Alibaba Cloud CLI commands. For more information about Cloud Shell, see What is Cloud Shell?.

CLI configuration

Important

An Alibaba Cloud account has full permissions to all APIs for your services, posing a high security risk. We strongly recommend that you create and use a RAM user for API access or daily operations.

Procedure

To use Alibaba Cloud CLI, you must configure your credentials and a region ID. Alibaba Cloud CLI supports multiple types of credentials. For more information, see Configure and manage credentials. This topic uses AccessKey (AK) credentials as an example. Follow these steps:

  1. Create a RAM user and grant the required permissions to manage the relevant services. For more information, see Create a RAM user and Manage permissions.

  2. After you create and authorize the RAM user, create an AccessKey for the user. Record the AccessKey ID and AccessKey Secret. You will need them in a later step. For more information, see Create an AccessKey.

  3. When you configure credentials, you must specify a region ID. The Alibaba Cloud CLI sends API calls to this region. For a list of available regions, see endpoints.

    Note

    When you use the Alibaba Cloud CLI, you can use the --region command-line option to specify a region for a command. This option overrides the default region set in your credentials configuration and environment variables. For more information, see Command-line options.

  4. Use the AccessKey of the RAM user to configure AK credentials. Name the profile AkProfile. For more information, see Configure and manage credentials.

Command examples

  1. Go to the ActionTrail API Explorer.

  2. In the API debugging interface, you can search for the API that you want to use in the search box on the left. In Parameter Configuration, fill in the parameters according to the API documentation, and click the CLI Example tab to the right of Parameter Configuration to generate a command example with parameters.

    • Click Run Commandimage to open Cloud Shell and quickly debug the command.

    • Click Copyimage to copy the CLI example to your clipboard. You can then paste and run it in your local shell tool.

      • When you paste a CLI example into your local shell tool for debugging, verify the parameter format. For more information about the Alibaba Cloud CLI command parameter format, see Understand command-line parameters.

      • The examples generated in the OpenAPI Explorer include the --region option by default. When you run the command locally, the Alibaba Cloud CLI ignores the region information in your default credentials and environment variables and uses the specified region instead. You can remove or keep this option as needed.

API calls

Command structure

The general command structure for the Alibaba Cloud CLI is as follows. For more information, see Generate and call commands.

aliyun <command> <subcommand> [options and parameters]

Common command-line options

Command-line options can modify a command's default behavior or provide additional functionality. Common options include:

  • --profile <profileName>: When you use the --profile option and specify a valid profile name profileName, the Alibaba Cloud CLI ignores the default credentials and environment variables and uses the specified profile.

  • --help: To get help for a command, add the --help option at the desired command level. For more information, see Get help information.

For more information, see Command-line options.

Examples

Example 1: You can use the --help option to list the ActionTrail APIs that are available in the Alibaba Cloud CLI. You can also view the supported APIs in API overview.

  1. Run the command.

    aliyun actiontrail --help
  2. Output:

    Usage:
      aliyun actiontrail <ApiName> --parameter1 value1 --parameter2 value2 ...
    Product: ActionTrail
    Version: 2020-07-06
    Available Api List:
      CreateDeliveryHistoryJob      Create a data backfill delivery job.
      CreateTrail                   Create an ActionTrail trail.
      DeleteDeliveryHistoryJob      Delete a data backfill delivery job.
      DeleteTrail                   Delete an ActionTrail trail.
      DescribeRegions               Query the Alibaba Cloud regions supported by ActionTrail.
      DescribeTrails                View the list of created trails.

Example 2: You can call the GetTrailStatus operation to query the status of a trail.

  1. Run the command.

    aliyun actiontrail GetTrailStatus --Name trail-test
  2. Output:

    {
      "RequestId": "8067369B-B923-4D26-85BC-61BF33922505",
      "StartLoggingTime": "2021-02-24T09:19:44Z",
      "LatestDeliveryError": "write sls failed, exception: the parent of sub user must be project owner, itemscount: 1",
      "StopLoggingTime": "2021-02-25T09:19:44Z",
      "IsLogging": true,
      "LatestDeliveryTime": "2021-02-26T09:19:44Z",
      "LatestDeliveryLogServiceError": "write sls failed, exception: the parent of sub user must be project owner, itemscount: 1",
      "LatestDeliveryLogServiceTime": "2021-02-26T09:19:44Z",
      "OssBucketStatus": true,
      "SlsLogStoreStatus": true
    }
    Note

    If an ActionTrail API call returns an error, review the error code and verify that the request parameters and their values are correct.

    You can also record the RequestID or SDK error message from the response and use the Alibaba Cloud OpenAPI Diagnostic Platform for self-service diagnostics.