You can manage an ECS instance in the ECS console or by using API operations. Alibaba Cloud provides SDKs in multiple languages to encapsulate APIs. This topic describes how to use an API operation to query an ECS instance in Python.

Background information

For more information about the sample code of querying an ECS instance, see Sample code. For more information about the explanation of the sample code, see the following topics:

Create and obtain an AccessKey pair of a RAM user

An AccessKey pair that consists of an AccessKey ID and an AccessKey secret is required if you want to use API operations to manage an ECS instance. To ensure the security of cloud services, you must create a RAM user, generate an AccessKey pair for the RAM user, and authorize the RAM user to manage ECS instances by using APIs.

Perform the following operations to create and obtain an AccessKey pair of a RAM user:

  1. Create a RAM user. For more information about the procedure, see Implement access control by using RAM.
  2. Create and obtain an AccessKey pair of the RAM user. For more information about the procedure, see Create an AccessKey.
  3. Authorize the RAM user to manage ECS instances. For more information about the procedure, see Implement access control by using RAM.

Install ECS SDK for Python

Ensure that you have prepared the runtime environment for Python. Python 2.7 or later is used in this topic.

Run the following command to install SDK for Python:
pip install aliyun-python-sdk-ecs
If you are prompted that you do not have the permission, use the following sudo command to continue the installation:
sudo pip install aliyun-python-sdk-ecs

SDK for Python 2.1.2 is used in this topic.

Sample code

The following content is the sample code. You can modify the code and change the values of parameters based on your actual needs.
#  coding=utf-8
# if the python sdk is not install using 'sudo pip install aliyun-python-sdk-ecs'
# if the python sdk is install using 'sudo pip install --upgrade aliyun-python-sdk-ecs'
# make sure the sdk version is greater than 2.1.2, you can use command 'pip show aliyun-python-sdk-ecs' to check
import json
import logging
from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
from aliyunsdkecs.request.v20140526.DescribeRegionsRequest import DescribeRegionsRequest
# configuration the log output formatter, if you want to save the output to file,
# append ",filename='ecs_invoke.log'" after datefmt.

logging.basicConfig(level=logging.INFO, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S')
clt = client.AcsClient('<accessKeyId>', '<accessSecret>', '<region-Id>')

# sample api to list aliyun open api.
def hello_aliyun_regions():
    request = DescribeRegionsRequest()
    response = _send_request(request)
    if response is not None:
        region_list = response.get('Regions').get('Region')
        assert response is not None
        assert region_list is not None
        result = map(_print_region_id, region_list)
        logging.info("region list: %s", result)

# output the instance owned in current region.
def list_instances():
    request = DescribeInstancesRequest()
    response = _send_request(request)
    if response is not None:
        instance_list = response.get('Instances').get('Instance')
        result = map(_print_instance_id, instance_list)
        logging.info("current region include instance %s", result)

def _print_instance_id(item):
    instance_id = item.get('InstanceId');
    return instance_id

def _print_region_id(item):
    region_id = item.get("RegionId")
    return region_id

# send open api request
def _send_request(request):
    request.set_accept_format('json')
    try:
        response_str = clt.do_action(request)
        logging.info(response_str)
        response_detail = json.loads(response_str)
        return response_detail
    except Exception as e:
        logging.error(e)

if __name__ == '__main__':
    logging.info("Hello Aliyun OpenApi!")
    hello_aliyun_regions()
    list_instances()

Query the list of regions supported by the current account

Create a file named hello_ecs_api.py. Before you use SDK for Python, use the Alibaba Cloud AccessKey ID and AccessKey secret of the RAM user to instantiate an AcsClient object.

Note The AccessKey ID and AccessKey secret allow the RAM user to access ECS API of Alibaba Cloud with full permissions. You must keep them confidential.
from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
from aliyunsdkecs.request.v20140526.DescribeRegionsRequest import DescribeRegionsRequest
clt = client.AcsClient('<accessKeyId>', '<accessSecret>', '<region-Id>')
Query the list of regions that are supported by your current account after the AcsClient object is instantiated. For more information, see DescribeRegions.
def hello_aliyun_regions():
    request = DescribeRegionsRequest()
    response = _send_request(request)
    region_list = response.get('Regions').get('Region')
    assert response is not None
    assert region_list is not None
    result = map(_print_region_id, region_list)
    logging.info("region list: %s", result)
def _print_region_id(item):
    region_id = item.get("RegionId")
    return region_id
def _send_request(request):
    request.set_accept_format('json')
    try:
        response_str = clt.do_action(request)
        logging.info(response_str)
        response_detail = json.loads(response_str)
        return response_detail
    except Exception as e:
        logging.error(e)
hello_aliyun_regions()
Run python hello_ecs_api.py on the command line to obtain the list of regions that are supported by the current account. A similar output is displayed as follows:
[u'cn-shenzhen', u'ap-southeast-1', u'cn-qingdao', u'cn-beijing', u'cn-shanghai', u'us-east-1', u'cn-hongkong', u'me-east-1', u'ap-southeast-2', u'cn-hangzhou', u'eu-central-1', u'ap-northeast-1', u'us-west-1']

Query ECS instances in a specific region

The process of querying the instance list is similar to that of querying the region list. You need to replace the input parameter with DescribeInstancesRequest. For more information, see DescribeInstances.

def list_instances():
    request = DescribeInstancesRequest()
    response = _send_request(request)
    if response is not None:
        instance_list = response.get('Instances').get('Instance')
        result = map(_print_instance_id, instance_list)
        logging.info("current region include instance %s", result)
def _print_instance_id(item):
    instance_id = item.get('InstanceId');
    return instance_id
A similar output is displayed as follows:
current region include instance [u'i-bp165p6xk2tmdhj0****', u'i-bp1a01zbqmsun5odo****'']

For more information about other API operations, see API overview. You can replace the input parameter with DescribeDisksRequest to query the disk list. For more information, see DescribeDisks.