All Products
Search
Document Center

ApsaraDB RDS:Use SDK for Python

Last Updated:Dec 18, 2024

This topic describes how to use Alibaba Cloud SDK for Python to call the DescribeDBInstances operation of ApsaraDB RDS to query instances.

View API documentation

Before you call an API operation, we recommend that you read the documentation about the API operation to learn about the parameters and permissions that are required to call the API operation. For more information, see List of operations by function.

Create a Resource Access Management (RAM) user and grant permissions to the RAM user

If you have a RAM user and the required permissions are granted to the RAM user, skip this procedure.

  1. Create a RAM user.

    1. Log on to the RAM console, go to the Users page, and then click Create User.

    2. Set Logon Name to rds-openapi-operator and select Using permanent AccessKey to access for Access Mode.

    3. Click OK. After the RAM user is created, save the AccessKey ID and AccessKey secret of the user.

  2. Grant permissions to the RAM user.

    1. Go to the Users page, find the RAM user that you created, and then click Add Permissions in the Actions column.

    2. Search for AliyunRDS in the text box and select the AliyunRDSReadOnlyAccess policy.

      Note

      The AliyunRDSReadOnlyAccess policy grants the RAM user permissions to query RDS instances. You can also select the AliyunRDSFullAccess policy to grant the RAM user full management permissions on RDS instances or select a custom policy. For more information, see Create custom policies.

    3. Click Grant permissions to complete the authorization.

Call an API operation

This topic describes how to use Alibaba Cloud SDK for Python to call API operations. You can use Alibaba Cloud SDK in other programming languages in the same way. For more information, visit OpenAPI Portal.

Install Python

Download and install Python 3. If you have installed Python, you can run the python --version command to view the Python version.

Configure environment variables

This section describes how to configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables in your operating system.

  • If you use a Linux or macOS operating system, replace <access_key_id> and <access_key_secret> in the following commands with your AccessKey ID and AccessKey secret. Then, run the commands in sequence on your device.

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • If you use a Windows operating system, create a file to add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables and set the variables to your AccessKey ID and AccessKey secret. Then, restart the Windows operating system.

Install dependencies

Run the following command on your device to install dependencies:

pip install alibabacloud_rds20140815==5.0.1

Download the sample code

  1. Access DescribeDBInstances.

  2. On the Parameters tab, configure the required parameters. In this example, you must set RegionId to cn-beijing.

  3. On the SDK Sample Code tab on the right, select Python for Languages and click Download Project to download the sample code package.

  4. Decompress the package on your device and go to the alibabacloud_sample directory.

Run the sample code

Run the following command:

python sample.py

Sample response:

{
    "headers": {
        "date": "Tue, 18 Jun 2024 10:12:14 GMT",
        "content-type": "application/json;charset=utf-8",
        "content-length": "1018",
        "connection": "keep-alive",
        "keep-alive": "timeout=25",
        "access-control-allow-origin": "*",
        "access-control-expose-headers": "*",
        "x-acs-request-id": "3BBF9A9B-E74C-5EF5-99BB-4F65B4B67499",
        "x-acs-trace-id": "dcfce27991f7d63647cc3c31ae3daa65",
        "etag": "1se2pACCKd0kse87Rqtbprg8"
    },
    "statusCode": 200,
    "body": {
        "Items": {
            "DBInstance": [
                {
                    "ConnectionMode": "Standard",
                    "ConnectionString": "rm-2zea24972vgw2****.mysql.rds.aliyuncs.com",
                    "CreateTime": "2024-06-18T10:09:56Z",
                    "DBInstanceClass": "mysql.n1e.small.1",
                    "DBInstanceId": "rm-2zea24972vgw2****",
                    "DBInstanceMemory": 1024,
                    "DBInstanceNetType": "Intranet",
                    "DBInstanceStatus": "Creating",
                    "DBInstanceStorageType": "general_essd",
                    "DBInstanceType": "Primary",
                    "DeletionProtection": false,
                    "Engine": "MySQL",
                    "EngineVersion": "8.0",
                    "ExpireTime": "2024-07-18T16:00:00Z",
                    "InstanceNetworkType": "VPC",
                    "LockMode": "Unlock",
                    "MutriORsignle": false,
                    "PayType": "Prepaid",
                    "ReadOnlyDBInstanceIds": {
                        "ReadOnlyDBInstanceId": []
                    },
                    "RegionId": "cn-beijing",
                    "ResourceGroupId": "rg-acfmz7u4zzr****",
                    "TipsLevel": 0,
                    "VSwitchId": "vsw-2zeyawzswr2lno0ys****",
                    "VpcCloudInstanceId": "rm-2zea24972vgw2****-20240618180950",
                    "VpcId": "vpc-2zeev9fov0chw8hjt****",
                    "ZoneId": "cn-beijing-l"
                }
            ]
        },
        "NextToken": "o7PHCFqQhehg8NUW9EJ7Yw",
        "PageNumber": 1,
        "PageRecordCount": 1,
        "RequestId": "3BBF9A9B-E74C-5EF5-99BB-4F65B4B67499",
        "TotalRecordCount": 1
    }
}