Elastic management of cloud server ECS using OpenApi

Introduction: Alibaba Cloud's cloud server ECS not only provides a console for daily management and resource creation, but also provides OpenApi for resource management and custom development. With OpenApi, you can manage and configure cloud servers more flexibly. Alibaba Cloud provides an SDK to wrap OpenApi, allowing you to integrate cloud server management into your existing system.
Alibaba Cloud's cloud server ECS not only provides a console for daily management and resource creation, but also provides OpenApi for resource management and custom development. With OpenApi, you can manage and configure cloud servers more flexibly.
Alibaba Cloud provides an SDK to wrap OpenApi, allowing you to integrate cloud server management into your existing system. This article uses Python development to illustrate how OpenApi manages cloud servers. Even if you do not have Python development experience, you can easily develop cloud services through this article . For the development and management of other languages, you can communicate via message.

【Elastic management of cloud server ECS using OpenApi】Install ECS Python SDK


First make sure you already have the Python Runtime, the Python version used in this article is 2.7+.
pip install aliyun-python-sdk-ecs
If prompted that you do not have permission, switch sudo to continue.
sudo pip install aliyun-python-sdk-ecs
The sdk version used in this article is 2.1.2 . If you are using an older version of the sdk, it is recommended that you update it.
Hello Aliyun ECS
We first create a file hello_ecs_api.py . In order to use the SDK, first instantiate the AcsClient object, here you need to enter your Alibaba Cloud Accesskey and Accesskey Secrect, you can get it through https://ak-console.aliyun.com/ own AK.
The Access Key ID and Access Key Secret are the keys for accessing Alibaba Cloud APIs, and they have full permissions for the account. Please keep them safe. .
from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
from aliyunsdkecs.request.v20140526.DescribeRegionsRequest import DescribeRegionsRequest


clt = client.AcsClient( 'Your Access Key Id' , 'Your Access Key Secrect' , 'cn-beijing' )
After the instantiation is complete, you can start developing your first application. Do a simple query to check the list of regions currently supported by your account. For specific documentation, see Querying the Available Regions List .

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()
Running python hello_ecs_api.py on the command line will get the current list of supported regions. Similar output is 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' ]

【Elastic management of cloud server ECS using OpenApi】Query the list of ECS instances in the current Region


query instance list is very similar to the query region list. You can replace the input parameter with DescribeInstancesRequest . For more query parameters, please refer to the query instance list.
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
The output is as follows
current region include instance [ u'i-****' , u'i-****'']
more API reference ECS API overview , try to do a query disk list . Replace the instance's parameter with DescribeDisksRequest .
Next step
After completing the above tasks our next step will include new tasks. The following content will be continuously updated, so stay tuned:
•API resource creation
•Resource Management - TAG Grouping
•API renewal and setting up automatic renewal
•API resource release and setting automatic release time
All the codes are as follows
# 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 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( 'Your Access Key Id' , 'Your Access Key Secrect' , 'cn-beijing' )

# 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__' :
( "Hello Aliyun OpenApi ! " )
hello_aliyun_regions()
list_instances()

Copyright statement: The content of this article is contributed by Alibaba Cloud real-name registered users. The copyright belongs to the original author. The Alibaba Cloud developer community does not own the copyright and does not assume the corresponding legal responsibility. For specific rules, please refer to the " Alibaba Cloud Developer Community User Service Agreement " and " Alibaba Cloud Developer Community Intellectual Property Protection Guidelines ". If you find any content suspected of plagiarism in this community, fill out the infringement complaint form to report it. Once verified, this community will delete the allegedly infringing content immediately.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00