Call tag OpenAPI operations from the Alibaba Cloud CLI. The tag ListTagResources operation is used as an example.
Background
Alibaba Cloud CLI is a general-purpose command-line tool built on OpenAPI. You can use it to automate the management and maintenance of tags. For more information, see What is Alibaba Cloud CLI?.
Step 1: Install the CLI
Before you use the Alibaba Cloud CLI, install it. Alibaba Cloud CLI provides installers for Windows, Linux, and macOS.
-
Windows: Install the CLI on Windows.
-
macOS: Install the CLI on macOS.
Cloud Shell comes pre-installed with the Alibaba Cloud CLI and automatically configures your identity credentials, so you do not need to configure it manually. You can debug Alibaba Cloud CLI commands in Cloud Shell. For more information, see What is Cloud Shell?.
Step 2: Configure the CLI
An Alibaba Cloud account has access to all OpenAPI operations, which poses a high security risk. We recommend that you create a RAM identity, grant permissions based on the principle of least privilege, and then use the RAM identity to call OpenAPI operations. For information about the permission policies supported by tag, see System policies for tags and Custom policies for tags.
Before you use the Alibaba Cloud CLI, configure your identity credentials and a region ID. The Alibaba Cloud CLI supports various credential types. For more information, see Configure and manage credentials. This topic uses AccessKey credentials as an example.
-
Create a RAM user and an AccessKey pair to configure your identity credentials. For more information, see Create a RAM user and Create an AccessKey.
-
Grant permissions to the RAM user. For this example, grant the
AliyunTAGReadOnlyAccesssystem policy to the RAM user for read-only access to tags. For more information, see Manage RAM user permissions. -
Obtain an available region ID. The Alibaba Cloud CLI sends OpenAPI calls to the region that you specify. For a list of available regions for tag, see service endpoint.
NoteWhen you use the Alibaba Cloud CLI, you can add the
--regionoption to a command to specify the region for that specific call. This option overrides the region specified in your default credential configuration and any environment variables. For more information, see Command-line options. -
Use the AccessKey of the RAM user and the region ID to configure AccessKey credentials. Name the profile
AkProfile. For more information, see Configure and manage credentials.
Step 3: Generate a sample command
-
In OpenAPI Explorer, go to the ListTagResources debugging page.
-
On the Parameter Configuration tab, enter the request parameters. Click the CLI Example tab to view the sample command.
-
Copy the sample command or run it directly in Cloud Shell:
-
Click the Run Command button
to open Cloud Shell and run the command. -
Click the Copy button
to copy the sample command. You can then paste it into a local shell or use it in an automation script.
NoteWhen you copy a sample command to a local shell for debugging, check the parameter format. For more information about parameter formats for Alibaba Cloud CLI commands, see Parameter format.
OpenAPI Explorer adds the
--regionoption to the sample command by default. When you run the command locally, this region takes precedence over the region in your credential configuration and environment variables. You can keep or remove this option as needed. -
Step 4: Call tag OpenAPI
Example 1: List supported tag operations
Use the --help option to list tag operations supported by the Alibaba Cloud CLI. For more information, see API reference.
-
Run the following command.
aliyun tag --help -
The expected output is as follows:
Product: Tag (tag) Version: 2018-08-28 Available Api List: AttachPolicy Attaches a tag policy to a target node. CheckCreatedByEnabled Checks whether the created-by tag feature is enabled. CloseCreatedBy Disables the created-by tag feature. CreatePolicy Creates a tag policy. CreateTags Creates preset tags. DeletePolicy Deletes a tag policy. DeleteTag Deletes a preset tag. DescribeRegions Queries regions that support tags. DetachPolicy Detaches a specified tag policy.
Example 2: Query resource tags
The following example calls the tag ListTagResources command to query tags attached to resources across different cloud services.
-
Run the following command.
aliyun tag ListTagResources --RegionId 'cn-hangzhou' -
The expected output is as follows:
{ "NextToken": "caeba0bbb2be03f84eb48b699f0a****", "RequestId": "014738E0-3C7F-47D8-8FB9-469500C6F387", "TagResources": [ { "ResourceARN": "arn:acs:ecs:cn-hangzhou:123456789****:instance/i-bp15hr53jws84akg****", "Tags": [ { "Key": "k1", "Value": "v1", "Category": "Custom" } ] } ] }NoteIf an error occurs when you call a tag OpenAPI operation, check the error code to verify that the request parameters and values are valid.
You can also record the RequestID or SDK error message from the response and use OpenAPI problem diagnosis to troubleshoot the issue.