All Products
Search
Document Center

Auto Scaling:Use Alibaba Cloud CLI to call Auto Scaling API

Last Updated:Nov 21, 2024

This topic describes how to use Alibaba Cloud Command Line Interface (CLI) to call Auto Scaling API. In this topic, the DescribeScalingActivities operation is used as an example. You can call this operation to query the details of a specific scaling activity.

Background information

Alibaba Cloud CLI is a general-purpose command-line tool that is developed based on APIs. You can use Alibaba Cloud CLI to automatically manage and maintain Auto Scaling resources. For more information, see What is Alibaba Cloud CLI?

Step 1: Install Alibaba Cloud CLI

Before you can use Alibaba Cloud CLI, you must install it. You can install Alibaba Cloud CLI in the Windows, Linux, and macOS operating systems. Select an installation package of Alibaba Cloud CLI based on the operating system of your device. For more information, see the following topics:

  • Windows operating system: Windows

  • Linux operating system: Linux

  • macOS operating system: macOS

Alibaba Cloud CLI is pre-installed in Cloud Shell. Cloud Shell automatically configures an identity credential. You can also use Cloud Shell to debug the commands that you want to run in Alibaba Cloud CLI. For more information, see What is Cloud Shell?

Step 2: Configure Alibaba Cloud CLI

Important

An Alibaba Cloud account has the permissions to manage and access the APIs of all Alibaba Cloud services. If you use an Alibaba Cloud account to call API operations, security risks may arise. We recommend that you create a Resource Access Management (RAM) user, grant the required permissions to the RAM user based on the principle of least privilege, and then use the RAM user to call API operations. 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 information such as identity credentials and region IDs in Alibaba Cloud CLI. Alibaba Cloud CLI supports various types of identity credentials. For more information, see Credential types. Alibaba Cloud CLI allows you to use the AccessKey information of a RAM user to configure identity credentials. To do so, perform the following steps:

  1. Create a RAM user and then create an AccessKey pair for the RAM user to configure identity credentials. 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, attach the AliyunESSReadOnlyAccess policy to the RAM user. The policy grants the RAM user the read-only permissions on Auto Scaling. For more information, see Grant permissions to a RAM user.

  3. Obtain the ID of the region in which you want to configure identity credentials. Alibaba Cloud CLI uses the region ID that you specify to initiate API calls. For more information about the regions supported by Auto Scaling, see Endpoints.

    Note

    When you use Alibaba Cloud CLI, you can use the --region option to run a command in a specific region. If you use the option, Alibaba Cloud CLI ignores the region information in the default credential configurations and environment variable settings. For more information, see Command line options for API calls.

  4. Use the AccessKey pair of the RAM user to configure AccessKey credentials in the profile named AkProfile. For more information, see Configuration examples.

Step 3: Generate a CLI command

  1. Go to the DescribeScalingActivities in OpenAPI Explorer.

    image

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

  3. Copy the CLI command or debug the CLI command in Cloud Shell.

    • Click the image icon to start Cloud Shell and debug the CLI command in Cloud Shell.

    • Click the image icon to copy the sample command to the clipboard. Then, paste the sample command into your local shell to run the sample command or edit an automation command line script.

    Note

    When you paste the CLI command into your on-premises shell for debugging, take note of the parameter formats. 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 shell, Alibaba Cloud CLI ignores the region information in the default identity credential configurations and environment variable settings, and preferentially runs the command in the specified region. You can delete or retain the option based on your business requirements.

Step 4: Call Auto Scaling API

Note
  • Alibaba Cloud CLI integrates earlier Auto Scaling API operations. We recommend that you use Auto Scaling API operations of version 2022-02-22. To do so, configure the --force and --version options when you call an Auto Scaling API operation. For more information, see Forcibly call an API operation.

  • When a CLI command is generated in OpenAPI Explorer for an Auto Scaling API operation of version 2022-02-22, the --force and --version options are automatically appended to the CLI command. You do not need to manually perform this operation.

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

In the following example, the DescribeScalingActivities operation of Auto Scaling is called to view the details of a specific scaling activity. For more information, see DescribeScalingActivities.

  1. Run the following command:

    aliyun ess DescribeScalingActivities --RegionId 'cn-hangzhou' --ScalingGroupId 'asg-bp18p2yfxow2dloq****' --version 2022-02-22 --method POST --force
  2. View the command output.

    {
      "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 is returned after you call an API operation of Auto Scaling, check whether the input parameters and values are valid based on the error code.

    You can also perform self-service diagnostics based on the returned request ID or SDK error information on the OpenAPI problem diagnosis page.