Alibaba Cloud ECS SDK (V1.0)

4.24.82 · maintenance · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the V1.0 ECS client using environment variables for credentials and fetch a list of ECS instances. It uses `DescribeInstancesRequest` as a common API call. Remember to replace placeholder values or set environment variables.

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}")

view raw JSON →