Use Alibaba Cloud SDK for Python to call the DescribeDBInstances operation of ApsaraDB RDS and list your DB instances.
Prerequisites
Before you begin, make sure you have:
Python 3 installed. Run
python --versionto verify.An Alibaba Cloud account with an AccessKey ID and AccessKey secret
A RAM user with the
AliyunRDSReadOnlyAccesspolicy (or higher). See Set up a RAM user if you haven't done this yet.
Set up a RAM user
Skip this section if you already have a RAM user with the required permissions.
Log on to the RAM console, go to the Users page, and click Create User.
Set Logon Name to
rds-openapi-operatorand set Access Mode to Using permanent AccessKey to access.Click OK. Save the AccessKey ID and AccessKey secret that are displayed — you won't be able to retrieve them again.
On the Users page, find the RAM user you just created and click Add Permissions in the Actions column.
Search for
AliyunRDSand select AliyunRDSReadOnlyAccess.AliyunRDSReadOnlyAccessgrants permission to query RDS instances. SelectAliyunRDSFullAccessfor full management permissions, or create a custom policy. For more information, see Create custom policies.Click Grant permissions.
Call the API
Step 1: Review the API reference
Before calling an API operation, review its parameters and required permissions in the ApsaraDB RDS API reference.
Step 2: Configure environment variables
Store your credentials in environment variables to keep your AccessKey secret out of source code and version control.
Linux or macOS:
export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>Windows (Command Prompt):
setx ALIBABA_CLOUD_ACCESS_KEY_ID "<access_key_id>"
setx ALIBABA_CLOUD_ACCESS_KEY_SECRET "<access_key_secret>"After running setx, restart your terminal for the changes to take effect.
Step 3: Install the SDK
pip install alibabacloud_rds20140815==5.0.1Step 4: Download and run the sample code
Go to DescribeDBInstances on OpenAPI Explorer.
On the Parameters tab, set RegionId to
cn-beijing.On the SDK Sample Code tab, select Python and click Download Project.
Extract the package and navigate to the
alibabacloud_sampledirectory.Run the sample:
python sample.pyIf the call succeeds, the response looks similar to:
{ "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 } }
What's next
Explore other ApsaraDB RDS API operations at OpenAPI Portal.
Use the same SDK workflow to call ApsaraDB RDS API operations in other programming languages.