詳解通過API管理或定制開發(fā)ECS實(shí)例
彈性管理 ECS 實(shí)例
獲取 RAM 子賬號 AK 密鑰
使用API管理ECS實(shí)例,您需要能訪問ECS資源的API密鑰(AccessKey ID 和 AccessKey Secret)。為了保證云服務(wù)的安全,您需要創(chuàng)建一個能訪問ECS資源的RAM用戶,獲取該用戶的AccessKey密鑰,并使用這個RAM用戶和API管理ECS實(shí)例。
以下是獲取RAM用戶AccessKey密鑰的操作步驟:
創(chuàng)建RAM用戶并獲取AccessKey密鑰。
直接給RAM用戶授權(quán),授予RAM用戶 管理云服務(wù)器服務(wù)(ECS)的權(quán)限。
安裝 ECS Python SDK
首先確保您已經(jīng)具備Python的Runtime,本文中使用的Python版本為2.7+。
pip install aliyun-python-sdk-ecs
如果提示您沒有權(quán)限,請切換sudo繼續(xù)執(zhí)行。
sudo pip install aliyun-python-sdk-ecs
本文使用的SDK版本為 2.1.2。
Hello Alibaba Cloud
創(chuàng)建文件 hello_ecs_api.py。為了使用SDK,首先實(shí)例化AcsClient對象,這里需要RAM用戶的AccessKey ID和AccessKey Secret。
AccessKey ID和AccessKey Secret是RAM用戶訪問阿里云ECS服務(wù)API的密鑰,具有該賬戶完全的權(quán)限,請妥善保管。
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')
完成實(shí)例化后可以進(jìn)行第一個應(yīng)用的開發(fā)。查詢當(dāng)前賬號支持的地域列表。具體的文檔參見 查詢可用地域列表。
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()
在命令行運(yùn)行 python hello_ecs_api.py 會得到當(dāng)前支持的 Region列表。類似的輸出如下:
[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']
查詢當(dāng)前的 Region 下的 ECS 實(shí)例列表
查詢實(shí)例列表和查詢 Region 列表非常類似,替換入?yún)ο鬄镈escribeInstancesRequest 即可,更多的查詢參數(shù)參考 查詢實(shí)例列表。
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
輸出結(jié)果為如下:
current region include instance [u'i-****', u'i-****'']
更多的API參考 ECS API 概覽,您可以嘗試作一個 查詢磁盤列表,將實(shí)例的參數(shù)替換為 DescribeDisksRequest。
完整代碼示例
以上操作完整的代碼示例如下所示。
# 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__': logging.info("Hello Aliyun OpenApi!") hello_aliyun_regions() list_instances()
相關(guān)文章
Python守護(hù)進(jìn)程和腳本單例運(yùn)行詳解
本篇文章主要介紹了Python守護(hù)進(jìn)程和腳本單例運(yùn)行,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01淺談Python類的__getitem__和__setitem__特殊方法
下面小編就為大家?guī)硪黄獪\談Python類的__getitem__和__setitem__特殊方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12python dataclass 快速創(chuàng)建數(shù)據(jù)類的方法
在Python中,dataclass是一種用于快速創(chuàng)建數(shù)據(jù)類的裝飾器和工具,本文實(shí)例代碼中我們定義了一個Person數(shù)據(jù)類,并使用fields()函數(shù)遍歷其字段,打印出每個字段的名稱、類型、默認(rèn)值和元數(shù)據(jù),對python dataclass 數(shù)據(jù)類相關(guān)知識感興趣的朋友一起看看吧2024-03-03Python requests接口測試實(shí)現(xiàn)代碼
這篇文章主要介紹了Python requests接口測試實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09淺談PyTorch中in-place operation的含義
這篇文章主要介紹了淺談PyTorch中in-place operation的含義,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06