Call Auto Scaling OpenAPI with Alibaba Cloud CLI

Updated at:

This topic explains how to use Alibaba Cloud CLI to call the Auto Scaling OpenAPI, using the Auto Scaling (ESS) DescribeScalingActivities operation as an example to query the details of a specific scaling activity.

Concepts

Alibaba Cloud CLI (Alibaba Cloud Command Line Interface) is a general-purpose command-line tool built on OpenAPI. You can use Alibaba Cloud CLI to automate the management of Auto Scaling. For more information, see What is Alibaba Cloud CLI?.

Step 1: Install Alibaba Cloud CLI

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

Cloud Shell comes pre-installed with Alibaba Cloud CLI and automatically configures your credentials, so no manual setup is required. You can debug Alibaba Cloud CLI commands in Cloud Shell. For more information, see What is Cloud Shell?.

Step 2: Configure Alibaba Cloud CLI

Important

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

Before you use Alibaba Cloud CLI, you must configure your credentials and region ID. Alibaba Cloud CLI supports various credential types. For more information, see Credential types. The following steps describe how to configure AK-type credentials by using the AccessKey of a RAM user:

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

  2. Grant permissions to the RAM user. This example requires you to grant read-only permissions for Auto Scaling by using the AliyunESSReadOnlyAccess policy. For more information, see Grant permissions to a RAM user.

  3. Obtain an available region ID for credential configuration. Alibaba Cloud CLI uses the region that you specify to initiate OpenAPI calls. For a list of regions where Auto Scaling is available, see Endpoints.

    Note

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

  4. Use the AccessKey of the RAM user to configure an AK-type credential profile and name the profile AkProfile. For more information, see Configuration examples.

Step 3: Generate CLI command example

  1. In OpenAPI Explorer, go to the debug page of DescribeScalingActivities.

    image

  2. On the Parameter Configuration tab, enter the request parameters. Click the CLI Example tab to view the generated CLI example.

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

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

    • Click the Copyimage button to copy the CLI example to the clipboard. You can paste it into your local shell tool to run it or use it to edit an automation script.

    Note

    If you copy the CLI example to a local shell tool for debugging, make sure that the parameter format is correct. For more information about the parameter formats for Alibaba Cloud CLI commands, see Parameter formats.

    By default, OpenAPI Explorer adds the --region option to the generated example. When you copy the command and run it locally, Alibaba Cloud CLI uses the region specified in the option and ignores the region in your default credential profile and environment variables. You can remove or keep this option as needed.

Step 4: Call Auto Scaling OpenAPI

Note
  • Alibaba Cloud CLI includes an older version of the Auto Scaling OpenAPI. We recommend that you use the --force and --version options to ensure you call the 2022-02-22 version of the Auto Scaling OpenAPI. For more information, see Forcibly call an API operation.

  • When OpenAPI Explorer generates a command example for the 2022-02-22 version of the Auto Scaling OpenAPI, it automatically adds the --force and --version options. You do not need to add them manually.

  • For more information about available API operations, see List of operations by function.

The following example shows how to use Alibaba Cloud CLI to call the DescribeScalingActivities operation of Auto Scaling to view the details of a specific scaling activity. For more information about this operation, see DescribeScalingActivities.

  1. Run the command.

    aliyun ess DescribeScalingActivities --RegionId 'cn-hangzhou' --ScalingGroupId 'asg-bp18p2yfxow2dloq****' --version 2022-02-22 --method POST --force
  2. The following output is returned.

    {
      "RequestId": "CC107349-57B7-4405-B1BF-9BF5AF7F****",
      "PageNumber": 1,
      "PageSize": 10,
      "TotalCount": 1,
      "ScalingActivities": [
        {
          "Progress": 100,
          "ScalingInstanceNumber": 1,
          "AttachedCapacity": "0",
          "TotalCapacity": "2",
          "ScalingGroupId": "asg-bp18p2yfxow2dloq****",
          "AutoCreatedCapacity": "2",
          "EndTime": "2020-09-10T09:54Z",
          "StartTime": "2020-09-10T09:54Z",
          "Description": "Add \\\"1\\\" ECS instance",
          "StatusCode": "Successful",
          "Cause": "A user requests to execute scaling rule \\\"asr-bp12tcnol686y1ik****\\\", changing the Total Capacity from \\\"1\\\" to \\\"2\\\".",
          "ScalingActivityId": "asa-bp161xudmuxdzofe****",
          "StatusMessage": "\\\"1\\\" ECS instances are added",
          "CreatedCapacity": 1,
          "DestroyedCapacity": 1,
          "StartedCapacity": 1,
          "StoppedCapacity": 1,
          "CreatedInstances": [
            "[\"i-abc1\",\"i-abc2\"]"
          ],
          "DestroyedInstances": [
            "[\n      \"i-abc1\",\n      \"i-abc2\"\n]"
          ],
          "StartedInstances": [
            "[\n      \"i-abc1\",\n      \"i-abc2\"\n]"
          ],
          "StoppedInstances": [
            "[\n      \"i-abc1\",\n      \"i-abc2\"\n]"
          ],
          "ErrorCode": "OperationDenied.NoStock",
          "ErrorMessage": "The specified ECS resource is out of stock in this region. Please try again later.",
          "Detail": "\"new ECS instances \"i-j6c8ilerw, i-j6c8iler4mx\" are created.\"",
          "TriggerSourceType": "Api",
          "TriggerSourceId": "2346366580*****",
          "LifecycleHookContext": {
            "DisableLifecycleHook": false,
            "IgnoredLifecycleHookIds": [
              "ash-bp14zolna43z266bq***\n"
            ]
          },
          "ActivityMetadata": "{\\\"goatscaler.io/managed\\\":\\\"true\\\"}",
          "InstanceRefreshTaskId": "ir-asdf12adsxg*****"
        }
      ]
    }
    Note

    If an error occurs when you call an Auto Scaling OpenAPI, check your request parameters and values against the returned error code.

    You can record the RequestID or SDK error message from the response and use OpenAPI Troubleshoot to diagnose the issue.