Python SDK call example-DMS
This example walks you through calling the DMS ListInstances operation using the Alibaba Cloud SDK for Python to retrieve registered database instances.
Prerequisites
Before you begin, make sure you have:
-
An Alibaba Cloud account with a RAM user that has DMS permissions
-
Python 3 installed. Run
python --versionto check. Download from Python 3 if needed -
The AccessKey ID and AccessKey secret of the RAM user
Set up a RAM user
Skip this section if you already have a RAM user with DMS permissions.
Create a RAM user
-
Go to the Users page of the RAM console and click Create User.
-
Set Logon Name to
dms-openapi-operator. Under Access Mode, select Using permanent AccessKey to access. -
Click OK. Save the AccessKey ID and AccessKey secret displayed on the page — they are shown only once.
Grant DMS permissions
-
On the Users page, find the RAM user you created and click Add Permissions in the Actions column.
-
Search for
Aliyundmsand select a policy from the results:Policy Permissions AliyunDMSFullAccess Full access to DMS AliyunDMSReadOnlyAccess Read-only access to DMS -
Click Grant Permission.
Set up your environment
Set up a virtual environment (recommended)
Using a virtual environment prevents dependency conflicts with other Python packages on your system.
python -m venv .venv
source .venv/bin/activate # Linux or macOS
# On Windows: .venv\Scripts\activate
Configure environment variables
Store your credentials as environment variables to avoid hardcoding them in your code.
Linux or macOS — replace the placeholders and run:
export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
Windows — create a file that sets the following environment variables, then restart your system:
| Variable | Value |
|---|---|
ALIBABA_CLOUD_ACCESS_KEY_ID |
Your AccessKey ID |
ALIBABA_CLOUD_ACCESS_KEY_SECRET |
Your AccessKey secret |
Install dependencies
pip install alibabacloud_credentials
pip install alibabacloud_dms_enterprise20181101
pip install alibabacloud_tea_console
Call the ListInstances operation
Download the sample code
-
Open ListInstances in OpenAPI Explorer.ListInstances
-
On the Parameters tab, configure request parameters. For this example, leave all parameters blank.
-
On the SDK Sample Code tab, select Python and click Download Project.
-
Decompress the package and go to the
alibabacloud_sampledirectory.
Run the code
python sample.py
If the call succeeds, the response looks similar to this:
{
"headers": {
"date": "Wed, 19 Jul 2023 03:11:46 GMT",
"content-type": "application/json;charset=utf-8",
"x-acs-request-id": "D960D80C-01CD-5FA4-AB12-7FA90BE0D35D"
},
"statusCode": 200,
"body": {
"InstanceList": {
"Instance": [
{
"DataLinkName": "dbl_mysql_7",
"DatabasePassword": "******",
"DatabaseUser": "root",
"DbaId": "54****",
"DbaNickName": "db_doc",
"DdlOnline": 0,
"EcsRegion": "cn-hangzhou",
"EnvType": "product",
"ExportTimeout": 86400,
"Host": "172.20.XXX.XXX",
"InstanceAlias": "zjq-test",
"InstanceId": "220****",
"InstanceSource": "ECS_OWN",
"InstanceType": "mysql",
"OwnerIdList": {
"OwnerIds": ["54****"]
},
"OwnerNameList": {
"OwnerNames": ["db_doc"]
},
"Port": 3306,
"QueryTimeout": 7200,
"SafeRuleId": "829***",
"SellSitd": false,
"StandardGroup": {
"GroupMode": "NONE_CONTROL",
"GroupName": "Flexible Management"
},
"State": "NORMAL",
"UseDsql": "1"
},
{
"DataLinkName": "dbl_sls_1",
"DatabasePassword": "******",
"DatabaseUser": "LTA****",
"DbaId": "54****",
"DbaNickName": "db_doc",
"DdlOnline": 0,
"EcsRegion": "cn-hangzhou",
"EnvType": "product",
"ExportTimeout": 86400,
"Host": "cn-hangzhou.log.aliyuncs.com",
"InstanceAlias": "DMSTEST",
"InstanceId": "22****",
"InstanceSource": "FILE",
"InstanceType": "sls",
"OwnerIdList": {
"OwnerIds": ["54****"]
},
"OwnerNameList": {
"OwnerNames": ["db_doc"]
},
"Port": 0,
"QueryTimeout": 7200,
"SafeRuleId": "82****",
"SellSitd": false,
"Sid": "dmstest",
"StandardGroup": {
"GroupMode": "NONE_CONTROL",
"GroupName": "Flexible Management"
},
"State": "NORMAL",
"UseDsql": "1"
}
]
},
"RequestId": "D960D80C-01CD-5FA4-AB12-7FA90BE0D35D",
"Success": true,
"TotalCount": 2
}
}
The TotalCount field shows the number of instances returned. Each instance object includes connection details (Host, Port, InstanceType), environment type, and the security group it belongs to.
If the call fails, check the x-acs-request-id in the response headers. Include this ID when contacting support.
What's next
-
Explore the full list of DMS API operations in the API reference.
-
Use OpenAPI Explorer to generate sample code in other languages, including Java, Go, and Node.js.