Use the Alibaba Cloud SDK for Python to call the DescribeDBInstances operation and query ApsaraDB RDS instances.
Before you begin
Before you call an API operation, review the API overview to understand the required parameters and permissions.
Create a RAM user and grant permissions
Skip this section if you already have a RAM user with the required permissions.
-
Create a RAM user:
-
In the RAM console, navigate to the Users page and click Create User.
-
Set Logon Name to rds-openapi-operator, and set Access Method to Programmatic Access.
-
Click OK and save the AccessKey ID and AccessKey Secret of the new RAM user.
-
-
Grant permissions to the RAM user:
-
On the Users page, find the RAM user and click Add Permissions in the Actions column.
-
In the search box, enter
AliyunRDSand select the AliyunRDSReadOnlyAccess policy.NoteAliyunRDSReadOnlyAccess grants read-only access to ApsaraDB RDS instances. For full access, select AliyunRDSFullAccess. You can also create a custom policy.
-
Click OK.
-
Call the API
The following steps use Python as an example. For other languages, download the SDK from the ApsaraDB RDS SDK page.
Prepare the Python environment
Download and install Python 3. To verify an existing installation, run python --version.
Configure environment variables
Set the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
-
On Linux and macOS, replace
<access_key_id>and<access_key_secret>with your actual values and run the following commands:export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret> -
On Windows, create the
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRETenvironment variables with your AccessKey pair values, then restart the system.
Install dependencies
Run the following command to install the SDK:
pip install alibabacloud_rds20140815==5.0.1
Download the sample code
-
In OpenAPI Explorer, go to the DescribeDBInstances page.
-
On the Parameter Configuration tab, configure parameters. In this example, set RegionId to cn-beijing.
-
In the right-side panel, on the SDK Sample tab, select Python and click Download Project.
-
Extract the downloaded package and navigate to the alibabacloud_sample directory.
Run the 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
}
}