All Products
Document Center

Manage instances

Last Updated: Mar 07, 2018

In addition to using the ECS console for resource creation and daily management, you can also use API to manage and customize resources. API allows you to manage and configure ECS instances with greater flexibility.

Alibaba Cloud encapsulates API in an SDK to integrate ECS instance management into existing systems. This article describes how to manage ECS instances through API based on Python development. You can develop ECS instances easily even if you do not have Python development experience.

Get the AccessKey for a RAM user

An AccessKey (AccessKey ID and AccessKey Secret) is required when you want to use API to manage ECS instances. To keep your cloud service secure, you have to create a RAM user and generate an AccessKey for it, and authorize the RAM user to manage ECS resources only. Then, you can use the RAM user and its AccessKey to manage ECS resources by using API.

Follow these steps to get the AccessKey for a RAM user.

  1. Create a RAM user and get the AccessKey.
  2. Grant permissions to the RAM user directly. To manage ECS resources, you have to grant AliyunECSFullAccess to the RAM user.

Install the ECS Python SDK

Make sure that the Python runtime environment has been installed. This article uses Python 2.7+.

  1. pip install aliyun-python-SDK-ecs

If you do not have the permission, switch to sudo to continue.

  1. sudo pip install aliyun-python-SDK-ecs

The SDK version is 2.1.2.

Hello Alibaba Cloud

Create the file To use SDK, you have to use the AccessKey of the RAM user to instantialize an AcsClient object.

The AccessKey allows the RAM user to access Alibaba Cloud APIs and give you full access to the user. Keep them safe.

  1. from aliyunsdkcore import client
  2. from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
  3. from aliyunsdkecs.request.v20140526.DescribeRegionsRequest import DescribeRegionsRequest
  4. clt = client.AcsClient('Your Access Key Id', 'Your Access Key Secrect', 'cn-beijing')

You can develop your first application after the AcsClient object is instantiated. Query the list of regions that your account supports. For more information, see Query the list of available regions.

  1. def hello_aliyun_regions():
  2. request = DescribeRegionsRequest()
  3. response = _send_request(request)
  4. region_list = response.get('Regions').get('Region')
  5. assert response is not None
  6. assert region_list is not None
  7. result = map(_print_region_id, region_list)
  8."region list: %s", result)
  9. def _print_region_id(item):
  10. region_id = item.get("RegionId")
  11. return region_id
  12. def _send_request(request):
  13. request.set_accept_format('json')
  14. try:
  15. response_str = clt.do_action(request)
  17. response_detail = json.loads(response_str)
  18. return response_detail
  19. except Exception as e:
  20. logging.error(e)
  21. hello_aliyun_regions()

In the command line, run python to obtain a list of supported regions. The output is similar to the following.

  1. [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 the list of ECS instances in the current region

The process for querying the instance list is similar to the region list. You only need to replace the input parameter DescribeRegionsRequest with DescribeInstancesRequest. For a full list of query parameters, see Query an instance list.

  1. def list_instances():
  2. request = DescribeInstancesRequest()
  3. response = _send_request(request)
  4. if response is not None:
  5. instance_list = response.get('Instances').get('Instance')
  6. result = map(_print_instance_id, instance_list)
  7."current region include instance %s", result)
  8. def _print_instance_id(item):
  9. instance_id = item.get('InstanceId');
  10. return instance_id

The output is as follows.

  1. current region include instance [u'i-****', u'i-****'']

For a full list of APIs, see ECS API overview. If you want to query a list of disks, replace DescribeInstancesRequest with DescribeDisksRequest.

Complete code

The following is the complete code of the operations described in this document.

  1. # coding=utf-8
  2. # if the python sdk is not install using 'sudo pip install aliyun-python-sdk-ecs'
  3. # if the python sdk is install using 'sudo pip install --upgrade aliyun-python-sdk-ecs'
  4. # make sure the sdk version is 2.1.2, you can use command 'pip show aliyun-python-sdk-ecs' to check
  5. import json
  6. import logging
  7. from aliyunsdkcore import client
  8. from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
  9. from aliyunsdkecs.request.v20140526.DescribeRegionsRequest import DescribeRegionsRequest
  10. # configuration the log output formatter, if you want to save the output to file,
  11. # append ",filename='ecs_invoke.log'" after datefmt.
  12. logging.basicConfig(level=logging.INFO,
  13. format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
  14. datefmt='%a, %d %b %Y %H:%M:%S')
  15. clt = client.AcsClient('Your Access Key Id', 'Your Access Key Secrect', 'cn-beijing')
  16. # sample api to list aliyun open api.
  17. def hello_aliyun_regions():
  18. request = DescribeRegionsRequest()
  19. response = _send_request(request)
  20. if response is not None:
  21. region_list = response.get('Regions').get('Region')
  22. assert response is not None
  23. assert region_list is not None
  24. result = map(_print_region_id, region_list)
  25."region list: %s", result)
  26. # output the instance owned in current region.
  27. def list_instances():
  28. request = DescribeInstancesRequest()
  29. response = _send_request(request)
  30. if response is not None:
  31. instance_list = response.get('Instances').get('Instance')
  32. result = map(_print_instance_id, instance_list)
  33."current region include instance %s", result)
  34. def _print_instance_id(item):
  35. instance_id = item.get('InstanceId');
  36. return instance_id
  37. def _print_region_id(item):
  38. region_id = item.get("RegionId")
  39. return region_id
  40. # send open api request
  41. def _send_request(request):
  42. request.set_accept_format('json')
  43. try:
  44. response_str = clt.do_action(request)
  46. response_detail = json.loads(response_str)
  47. return response_detail
  48. except Exception as e:
  49. logging.error(e)
  50. if __name__ == '__main__':
  51."Hello Aliyun OpenApi!")
  52. hello_aliyun_regions()
  53. list_instances()

If you want to learn other API operations in ECS, see ECS API operation.