詳解通過(guò)API管理或定制開(kāi)發(fā)ECS實(shí)例
彈性管理 ECS 實(shí)例
獲取 RAM 子賬號(hào) AK 密鑰
使用API管理ECS實(shí)例,您需要能訪(fǎng)問(wèn)ECS資源的API密鑰(AccessKey ID 和 AccessKey Secret)。為了保證云服務(wù)的安全,您需要?jiǎng)?chuàng)建一個(gè)能訪(fǎng)問(wèn)ECS資源的RAM用戶(hù),獲取該用戶(hù)的AccessKey密鑰,并使用這個(gè)RAM用戶(hù)和API管理ECS實(shí)例。
以下是獲取RAM用戶(hù)AccessKey密鑰的操作步驟:
創(chuàng)建RAM用戶(hù)并獲取AccessKey密鑰。
直接給RAM用戶(hù)授權(quán),授予RAM用戶(hù) 管理云服務(wù)器服務(wù)(ECS)的權(quán)限。
安裝 ECS Python SDK
首先確保您已經(jīng)具備Python的Runtime,本文中使用的Python版本為2.7+。
pip install aliyun-python-sdk-ecs
如果提示您沒(méi)有權(quán)限,請(qǐng)切換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對(duì)象,這里需要RAM用戶(hù)的AccessKey ID和AccessKey Secret。
AccessKey ID和AccessKey Secret是RAM用戶(hù)訪(fǎng)問(wèn)阿里云ECS服務(wù)API的密鑰,具有該賬戶(hù)完全的權(quán)限,請(qǐng)妥善保管。
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)行第一個(gè)應(yīng)用的開(kāi)發(fā)。查詢(xún)當(dāng)前賬號(hào)支持的地域列表。具體的文檔參見(jiàn) 查詢(xún)可用地域列表。
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 會(huì)得到當(dāng)前支持的 Region列表。類(lèi)似的輸出如下:
[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']
查詢(xún)當(dāng)前的 Region 下的 ECS 實(shí)例列表
查詢(xún)實(shí)例列表和查詢(xún) Region 列表非常類(lèi)似,替換入?yún)?duì)象為DescribeInstancesRequest 即可,更多的查詢(xún)參數(shù)參考 查詢(xún)實(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 概覽,您可以嘗試作一個(gè) 查詢(xún)磁盤(pán)列表,將實(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)行,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01淺談Python類(lèi)的__getitem__和__setitem__特殊方法
下面小編就為大家?guī)?lái)一篇淺談Python類(lèi)的__getitem__和__setitem__特殊方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12python dataclass 快速創(chuàng)建數(shù)據(jù)類(lèi)的方法
在Python中,dataclass是一種用于快速創(chuàng)建數(shù)據(jù)類(lèi)的裝飾器和工具,本文實(shí)例代碼中我們定義了一個(gè)Person數(shù)據(jù)類(lèi),并使用fields()函數(shù)遍歷其字段,打印出每個(gè)字段的名稱(chēng)、類(lèi)型、默認(rèn)值和元數(shù)據(jù),對(duì)python dataclass 數(shù)據(jù)類(lèi)相關(guān)知識(shí)感興趣的朋友一起看看吧2024-03-03Python requests接口測(cè)試實(shí)現(xiàn)代碼
這篇文章主要介紹了Python requests接口測(cè)試實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09Python新手在作用域方面經(jīng)常容易碰到的問(wèn)題
這篇文章主要介紹了Python新手在作用域方面經(jīng)常容易碰到的問(wèn)題,全局變量和局部變量方面的知識(shí)在Python學(xué)習(xí)當(dāng)中是基礎(chǔ)中的基礎(chǔ),需要的朋友可以參考下2015-04-04淺談PyTorch中in-place operation的含義
這篇文章主要介紹了淺談PyTorch中in-place operation的含義,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06解決python圖像處理圖像賦值后變?yōu)榘咨膯?wèn)題
這篇文章主要介紹了解決python圖像處理圖像賦值后變?yōu)榘咨膯?wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06