Alibaba Cloud CLI integration examples

更新时间:
复制 MD 格式

Alibaba Cloud CLI is a command-line tool built on OpenAPI. You can use it to manage ApsaraDB RDS instances from a shell. Learn how to install and configure the CLI and call ApsaraDB RDS APIs.

Prerequisites

New to Alibaba Cloud CLI? Start with What is Alibaba Cloud CLI?.

Install Alibaba Cloud CLI

Install Alibaba Cloud CLI for your operating system:

You can also debug CLI commands in Cloud Shell. What is Cloud Shell?

Configure Alibaba Cloud CLI

Important

Your Alibaba Cloud account has full API access, which poses a security risk. Create and use a RAM user instead. Attach AliyunRDSReadOnlyAccess for read-only access or AliyunRDSFullAccess for full control over ApsaraDB RDS.

Configure your identity credentials, default region ID, and other settings before using the CLI. The CLI supports multiple credential types (Configure and manage identity credentials). This example uses AccessKey credentials.

  1. Create a RAM user and grant the required service permissions (Manage RAM user permissions).

    Note

    ApsaraDB RDS permission policies: Use RAM to implement access control.

  2. Create an AccessKey pair for the RAM user. Record the AccessKey ID and AccessKey secret for credential configuration.

  3. Choose a region ID. The CLI sends API calls to the specified region. Available regions: Endpoints.

    Note

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

  4. Configure AccessKey credentials under a profile named AkProfile using the RAM user's AccessKey pair. Configure and manage identity credentials.

Generate a sample CLI command

  1. Go to the ApsaraDB RDS API reference in OpenAPI Explorer.

  2. Search for the API in the left-side search box. On the Parameters tab, enter the required values. Next to the Parameters tab, click CLI Example to generate a sample command.

    • Click the image icon to open Cloud Shell and debug the command.

    • Click the image icon to copy the CLI example to the clipboard. You can then paste it into your local shell to run it.

      • When pasting a CLI example into your local shell, verify the parameter format. Understand command parameters.

      • OpenAPI Explorer adds a --region option to sample commands by default, which overrides the region in your credentials and environment variables. Keep or remove it as needed.

Call cloud service APIs

Command structure

CLI commands follow this structure (Generate and call a command):

aliyun <command> <subcommand> [options and parameters]

Common command-line options

Use command-line options to modify command behavior or add functionality.

  • --profile<profileName>: Uses the specified profile, overriding default credentials and environment variables.

  • --help: Displays help information for a command. Get help.

Command-line options.

Examples

Example 1: List available ApsaraDB RDS APIs with --help. You can also check the API overview.

  1. Run the command.

    aliyun rds --help
  2. View the command output.

    Command output

    Alibaba Cloud Command Line Interface Version 3.0.216
    Usage:
      aliyun rds <ApiName> --parameter1 value1 --parameter2 value2 ...
    Product: Rds (ApsaraDB for RDS)
    Version: 2014-08-15
    Available API List:
      ActivateMigrationTargetInstance             Switches workloads over from the source PostgreSQL instance to the destination ApsaraDB RDS for PostgreSQL instance.
      AddTagsToResource                           Adds tags to an instance.
      AllocateInstancePublicConnection            Applies for a public endpoint for an ApsaraDB RDS instance
      AllocateReadWriteSplittingConnection        Applies for a read-only routing endpoint for an instance.
      AttachWhitelistTemplateToInstance           Associates a whitelist template with an instance.
      CalculateDBInstanceWeight                   Queries system-assigned read weights.
    ...

Example 2: Call the DescribeDBInstanceAttribute API to query instance details.

  1. Run the command.

    aliyun rds DescribeDBInstanceAttribute --region cn-hangzhou --DBInstanceId 'pgm-bp16k272p478****'
  2. View the command output.

    Command output

    {
            "Items": {
                    "DBInstanceAttribute": [
                            {
                                    "AccountMaxQuantity": 99999,
                                    "AccountType": "Mix",
                                    "AdvancedFeatures": "LinkedServer,DistributeTransaction",
                                    "AutoUpgradeMinorVersion": "Auto",
                                    "AvailabilityValue": "100.0%",
                                    "BabelfishConfig": {},
                                    "CanTempUpgrade": false,
                                    "Category": "HighAvailability",
                                    "ColdDataEnabled": false,
                                    "ConnectionMode": "Standard",
                                    "ConnectionString": "pgm-bp16k272p478****.pg.rds.aliyuncs.com",
                                    "ConsoleVersion": "2",
                                    "CreationTime": "2024-08-06T06:18:48Z",
                                    "CurrentKernelVersion": "rds_postgres_1400_20240530",
                                    "DBClusterNodes": {
                                            "DBClusterNode": []
                                    },
                                    "DBInstanceCPU": "2",
                                    "DBInstanceClass": "pg.n4.2c.2m",
                                    "DBInstanceClassType": "x",
                                    "DBInstanceDescription": "DB-test",
                                    "DBInstanceDiskUsed": 3520069632,
                                    "DBInstanceId": "pgm-bp16k272p478****",
                                    "DBInstanceMemory": 8192,
                                    "DBInstanceNetType": "Intranet",
                                    "DBInstanceStatus": "Running",
                                    "DBInstanceStorage": 100,
                                    "DBInstanceStorageType": "cloud_essd",
                                    "DBInstanceType": "Primary",
                                    "DBMaxQuantity": 99999,
                                    "DedicatedHostGroupId": "",
                                    "DeletionProtection": false,
                                    "DispenseMode": "MultiAVZDispenseMode",
                                    "Engine": "PostgreSQL",
                                    "EngineVersion": "14.0",
                                    "ExpireTime": "",
                                    "Extra": {
                                            "DBInstanceIds": {
                                                    "DBInstanceId": []
                                            }
                                    },
                                    "IPType": "IPv4",
                                    "InsId": 1,
                                    "InstanceNetworkType": "VPC",
                                    "InstructionSetArch": "x86",
                                    "IoAccelerationEnabled": "0",
                                    "LatestKernelVersion": "rds_postgres_1400_20240530",
                                    "LockMode": "Unlock",
                                    "MaintainTime": "18:00Z-22:00Z",
                                    "MasterZone": "cn-hangzhou-j",
                                    "MaxConnections": 800,
                                    "MaxIOMBPS": 0,
                                    "MaxIOPS": 6800,
                                    "OriginConfiguration": "{\"KindCode\":\"18\"}",
                                    "PayType": "Postpaid",
                                    "Port": "5432",
                                    "ProxyType": 2,
                                    "ReadOnlyDBInstanceIds": {
                                            "ReadOnlyDBInstanceId": []
                                    },
                                    "RegionId": "cn-hangzhou",
                                    "ResourceGroupId": "rg-acfmz7u4zzrngoa",
                                    "SecurityIPList": "0.0.0.0/0,172.16.0.0/12",
                                    "SecurityIPMode": "normal",
                                    "ServerlessConfig": {},
                                    "SlaveZones": {
                                            "SlaveZone": [
                                                    {
                                                            "ZoneId": "cn-hangzhou-k"
                                                    }
                                            ]
                                    },
                                    "SuperPermissionMode": "",
                                    "SupportCreateSuperAccount": "Yes",
                                    "SupportUpgradeAccountType": "Yes",
                                    "Tips": "ok",
                                    "TipsLevel": 1,
                                    "VSwitchId": "vsw-bp1sxxsodv28ey5dl****",
                                    "VpcCloudInstanceId": "pgm-bp16k272p478****-20240806141842",
                                    "VpcId": "vpc-bp1ov7as4yvz4kxei****",
                                    "ZoneId": "cn-hangzhou-j",
                                    "kindCode": "18"
                            }
                    ]
            },
            "RequestId": "14B99E32-4ECD-5B8E-A9C8-6738C8C95910"
    }
    
    Note

    If an API call fails, check the error code to verify your request parameters.

    Use the request ID or error message with Alibaba Cloud OpenAPI Diagnostics to diagnose the issue.