Alibaba Cloud ECS SDK (V1.0)
aliyun-python-sdk-ecs is the Elastic Compute Service (ECS) module of the Alibaba Cloud Python SDK V1.0. This SDK provides an interface to interact with Alibaba Cloud ECS resources. While actively maintained with frequent updates, the V1.0 SDK is in a basic security maintenance phase, and Alibaba Cloud officially recommends migrating to the V2.0 SDK (`alibabacloud-python-sdk`) for new development and improved features.
Common errors
-
ModuleNotFoundError: No module named 'aliyunsdkcore'
cause The core SDK (`aliyun-python-sdk-core`) is a mandatory dependency and has not been installed.fixInstall the core SDK: `pip install aliyun-python-sdk-core`. -
Command 'python setup.py egg_info' failed with error code 1 in /path/to/package
cause This often indicates an outdated `pip` or Python version, or missing build-time dependencies required for package installation.fixUpgrade `pip` and `setuptools`: `pip install --upgrade pip setuptools`. Ensure your Python version meets the SDK requirements (Python 3.7+ for recent `aliyunsdkcore`). -
The current status of the resource does not support this operation.
cause You are attempting to perform an action (e.g., start, stop, release an instance) on an ECS instance that is not in the required state for that operation.fixBefore performing state-changing operations, query the instance's current status (e.g., using `DescribeInstancesRequest`) and wait for it to reach the correct state. For example, an instance must be 'Stopped' to be 'Started', and 'Running' to be 'Stopped'. -
SDK.HttpError: The endpoint is not found.
cause This error typically means the region ID provided during `AcsClient` initialization is invalid or the SDK version is too old to recognize the endpoint for the specified region.fixVerify the `region_id` string format (e.g., 'cn-hangzhou'). Ensure `aliyunsdkcore` is updated to the latest version: `pip install --upgrade aliyun-python-sdk-core`.
Warnings
- breaking The Alibaba Cloud Python SDK V1.0, which includes `aliyun-python-sdk-ecs`, is officially deprecated and in a basic security maintenance phase. Users are strongly recommended to migrate to the V2.0 SDK (`alibabacloud-python-sdk`) for new projects and actively maintained features.
- gotcha As of `aliyun-python-sdk-core` version 2.16.0, the minimum supported Python version is 3.7. Attempting to use this SDK with older Python versions (e.g., Python 2.7, 3.6) will lead to compatibility issues or installation failures.
- gotcha Many ECS operations (e.g., StartInstance, StopInstance, ReleaseInstance) are asynchronous and require the ECS instance to be in a specific state before the operation can succeed. Failing to check the instance status can lead to API errors.
- gotcha When allocating an EIP (`AllocateEipAddressRequest`), you might encounter an API error if the `InternetChargeType` parameter is not explicitly set, even though it might seem optional in some contexts.
Install
-
pip install aliyun-python-sdk-core aliyun-python-sdk-ecs
Imports
- AcsClient
from aliyunsdkcore.client import AcsClient
- DescribeInstancesRequest
from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
- RunInstancesRequest
from aliyunsdkecs.request.RunInstancesRequest import RunInstancesRequest
from aliyunsdkecs.request.v20140526.RunInstancesRequest import RunInstancesRequest
Quickstart
import os
from aliyunsdkcore.client import AcsClient
from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
# Configure credentials and region using environment variables
access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', 'YOUR_ACCESS_KEY_ID')
access_key_secret = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', 'YOUR_ACCESS_KEY_SECRET')
region_id = os.environ.get('ALIBABA_CLOUD_REGION_ID', 'cn-hangzhou')
if not access_key_id or not access_key_secret:
raise ValueError("Alibaba Cloud Access Key ID and Secret must be set in environment variables or hardcoded for testing.")
try:
# Initialize the AcsClient
client = AcsClient(access_key_id, access_key_secret, region_id)
# Create a request object for DescribeInstances
request = DescribeInstancesRequest()
request.set_PageSize(10)
# Send the request and get the response
response = client.do_action_with_exception(request)
# Process the response (response is a byte string, decode it)
import json
response_data = json.loads(response.decode('utf-8'))
print(f"Successfully queried instances in region {region_id}:")
for instance in response_data.get('Instances', {}).get('Instance', []):
print(f" Instance ID: {instance['InstanceId']}, Status: {instance['Status']}")
except Exception as e:
print(f"An error occurred: {e}")