CLI integration example

更新时间:
复制 MD 格式

Alibaba Cloud CLI is a general-purpose command-line tool built on OpenAPI. You can use Alibaba Cloud CLI to automate the management and maintenance of Web Application Firewall (WAF) 2.0. If you are new to Alibaba Cloud CLI, see What is Alibaba Cloud CLI? This topic uses the DescribeInstanceSpecInfo operation as an example to demonstrate how to use Alibaba Cloud CLI to call Web Application Firewall 2.0. The DescribeInstanceSpecInfo operation queries the specifications of a WAF instance.

Install Alibaba Cloud CLI

Before you use Alibaba Cloud CLI, you must install it. Alibaba Cloud CLI provides installers for Windows, Linux, and macOS. Choose the installer for your operating system.

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

Configure Alibaba Cloud CLI

Important

An Alibaba Cloud account has full access to all API operations, which poses a security risk. We strongly recommend that you create a RAM identity, grant permissions based on the principle of least privilege, and use that identity to call API operations.

Before you use Alibaba Cloud CLI, you must configure details such as your identity credentials and a region ID. Alibaba Cloud CLI supports multiple types of identity credentials. For more information, see Credential types. This topic uses an AccessKey pair as an example.

  1. Create a RAM user and an AccessKey pair for the user. For more information, see Create a RAM user and Create an AccessKey pair.

  2. Grant the required permissions to the RAM user. In this example, grant the RAM user read-only access to Web Application Firewall 2.0 by attaching the AliyunYundunWAFReadOnlyAccess policy. For more information about system policies, see System policies for Web Application Firewall.

  3. Obtain and record an available region ID. Alibaba Cloud CLI sends API requests to the region that you specify. For a list of available regions, see Service endpoints.

    Note

    You can use the --region option to specify a region for a command. This option overrides the default region configured in your credentials and environment variables. For more information, see Command-line options.

  4. Use the RAM user's AccessKey pair to configure your identity credentials, and save the configuration to a profile named AkProfile. For more information, see Configuration examples.

Generate a CLI command example

  1. Click DescribeInstanceSpecInfo to open OpenAPI Explorer.

  2. On the Parameters tab, set the request parameters. Then, click the CLI Example tab to view the generated command.

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

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

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

      Note
      • When you paste the command example into your local shell for debugging, ensure the parameters are formatted correctly. For more information about the parameter formats for Alibaba Cloud CLI, see Parameter formats.

      • By default, OpenAPI Explorer adds the --region option to the generated command. When you run the command locally, this option overrides the default region in your configuration. You can keep or remove this option as needed.

image

Call API operations

Example 1: Obtain the list of supported OpenAPI operations for Web Application Firewall 2.0 in Alibaba Cloud CLI

The following command uses the --help option to list the Web Application Firewall 2.0 API operations supported by Alibaba Cloud CLI. You can also view the supported API operations in API overview.

  1. Run the command.

    aliyun waf-openapi help
  2. View the output.

    image

Example 2: Query WAF instance specifications

The following example shows how to call the DescribeInstanceSpecInfo operation of Web Application Firewall 2.0 by using Alibaba Cloud CLI to query the specifications of a WAF instance.

  1. Run the command.

    aliyun waf-openapi DescribeInstanceSpecInfo \
      --region cn-hangzhou \
      --RegionId 'cn-hangzhou' \
      --InstanceId 'waf-cn-st2225l****'
  2. View the output.

    {
      "RequestId": "E906513E-F6B5-495E-98DC-7BA8****1D76",
      "InstanceId": "waf-cn-st2225l****",
      "ExpireTime": 1677168000000,
      "Version": "version_3",
      "InstanceSpecInfos": [
        {
          "Code": "103",
          "Value": "640"
        }
      ]
    }
    Note

    If a Web Application Firewall 2.0 API operation returns an error, check whether the request parameters and their values are valid based on the error message.

    You can also record the returned RequestID or SDK error information and use Alibaba Cloud OpenAPI Diagnostics to diagnose the issue.