使用AWS的python库boto3调用ec2服务
生活随笔
收集整理的這篇文章主要介紹了
使用AWS的python库boto3调用ec2服务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.安裝和使用
系統:Ubuntu14
首先安裝兩個python包:
pip install boto3
pip install awscli
然后設置憑證文件,輸入,aws configure:
設置后,在~/.aws/目錄下會多出兩個配置文件,里面記錄了剛剛輸入的驗證數據:
之后就可以使用boto3這個python包來編寫自己的腳本了。
2.部分常用屬性和方法
# encoding=utf8import boto3def main():ec2 = boto3.resource('ec2') # 使用EC2服務instance = ec2.Instance('你的實例id') # 獲取一個EC2實例(一臺機器)state = instance.state # 獲取實例的當前狀態,返回是一個字典'''state說明:0 : pending 16 : running 32 : shutting-down 48 : terminated 64 : stopping 80 : stopped '''# 返回實例的一個或多個網絡接口信息attrs = instance.network_interfaces_attribute# 返回實例的公有ip,每次重啟后該ip會改變publicIp = instance.public_ip_address# 返回實例的私有ip,每次重啟后該ip不會改變privateIp = instance.private_ip_address# 停止一個實例,返回一個字典對象stop_dic = instance.stop()# 等待一個實例完成停止操作instance.wait_until_stopped()# 啟用一個實例,返回一個字典對象start_dic = instance.start()# 等待一個實例到它正常運行instance.wait_until_running()# 也可以選出正在運行的所有實例instances = ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])for instance in instances:print instance.id# 更多關于instance的屬性和方法可以參考:# http://boto3.readthedocs.io/en/latest/reference/services/ec2.html#instance# ec2相關文檔可以參考:# http://boto3.readthedocs.io/en/latest/reference/services/ec2.htmlif __name__ == '__main__':main()總結
以上是生活随笔為你收集整理的使用AWS的python库boto3调用ec2服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bilibili 哔哩哔哩 2018秋招
- 下一篇: python语言所使用的特殊含义符号_P