Resource Center CLI integration example

更新时间:
复制 MD 格式

Use Alibaba Cloud CLI to call Resource Center OpenAPI operations. This example calls SearchResources to search for resources visible to the current account in Resource Center.

Key concepts

Alibaba Cloud CLI (Alibaba Cloud Command Line Interface) is a command-line tool built on OpenAPI that automates Resource Center management.

Step 1: Install Alibaba Cloud CLI

Install Alibaba Cloud CLI for your operating system:

Cloud Shell comes pre-installed with Alibaba Cloud CLI and auto-configures credentials. Debug commands directly in Cloud Shell. What is Cloud Shell?.

Step 2: Configure Alibaba Cloud CLI

Important
  • A primary account has full access to 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 call OpenAPI operations. For Resource Center permission policies, see System policies for ResourceManager and Custom policies for Resource Management.

  • Resource Center can only query resources that your account has permission to access. When using a RAM identity, you must grant the RAM user read-only permissions on target cloud resources in addition to Resource Center permissions. Grant a RAM user the permissions to use Resource Center.

Before using Alibaba Cloud CLI, configure credentials and a region ID. Alibaba Cloud CLI supports various credential types (Configuration examples). This example uses AccessKey credentials:

  1. Create a RAM user and create an AccessKey.

  2. Grant the RAM user the AliyunResourceCenterReadOnlyAccess policy for read-only access to Resource Center, plus policies for target cloud resources. For example, grant AliyunVPCReadOnlyAccess for Virtual Private Cloud (VPC) resources. Manage RAM user permissions.

  3. Obtain a region ID. Available Resource Center regions are listed in Endpoints.

    Note

    The --region option overrides the region in your default credentials and environment variables. Command-line options.

  4. Configure an AccessKey credential using the RAM user's AccessKey and the region ID. Name the profile AkProfile. Configuration examples.

Step 3: Generate a CLI command example

  1. In OpenAPI Explorer, go to the SearchResources debugging page.

    Configure parameters such as Filter, then click the CLI Example tab.

  2. Enter the request parameters in Parameter Configuration and click the CLI Example tab to view the generated CLI example.

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

    • Click the start buttonimage to open Cloud Shell and quickly debug the command.

    • Click the Copy buttonimage to copy the CLI example to the clipboard. Paste it into your local shell or use it in an automation script.

    Note

    When pasting the CLI example into your local shell, ensure the parameter format is correct. Parameter formats.

    OpenAPI Explorer adds --region to the generated example by default, which overrides your default profile and environment variables. You can keep or remove this option.

Step 4: Call Resource Center APIs

Example 1: List supported Resource Center APIs

Use --help to list all Resource Center API operations. API overview.

  1. Run the following command.

    aliyun resourcecenter --help
  2. Expected output:

    Product: ResourceCenter (Resource Center)
    Version: 2022-12-01
    Available Api List:
        AssociateDefaultFilter                Sets a default filter.
        CreateFilter                          Creates a filter.
        CreateSavedQuery                      Creates a custom query template.
        DeleteFilter                          Deletes a filter.
        DeleteSavedQuery                      Deletes a custom query template.
        DisableMultiAccountResourceCenter     Disables cross-account resource search.
        DisableResourceCenter                 Disables Resource Center.
        DisassociateDefaultFilter             Disassociates the default filter.
        EnableMultiAccountResourceCenter      Enables cross-account resource search.

Example 2: Search account resources

Call the SearchResources API of Resource Center to search for Virtual Private Cloud route table resources in the current account.

  1. Run the following command.

    aliyun resourcecenter SearchResources --Filter.1.Key ResourceType --Filter.1.Value.1 'ACS::VPC::RouteTable' --Filter.1.MatchType Equals
  2. Expected output:

    {
      "NextToken": "eyJzZWFyY2hBZnRlcnMiOlsiMTAwMTU2Nzk4MTU1OSJd****",
      "RequestId": "D696E6EF-3A6D-5770-801E-4982081FE4D0",
      "MaxResults": 10,
      "Resources": [
        {
          "AccountId": "151266687691****",
          "ResourceGroupId": "rg-acfmzawhxxc****",
          "ResourceId": "vtb-bp11lbh452fr8940s****",
          "ResourceName": "group1",
          "CreateTime": "2024-09-25T08:55:30Z",
          "ResourceType": "ACS::VPC::RouteTable",
          "RegionId": "ap-northeast-1"
        }
      ]
    }
    Note

    If a Resource Center API call returns an error, verify that the request parameters and values are valid.

    Record the RequestId or SDK error message and use the OpenAPI problem diagnosis page to troubleshoot.