{"id":3700,"date":"2013-12-26T15:53:25","date_gmt":"2013-12-26T06:53:25","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=3700"},"modified":"2018-11-14T16:33:53","modified_gmt":"2018-11-14T07:33:53","slug":"python%e3%81%aeboto%e3%81%a7%e9%81%8a%e3%82%93%e3%81%a7%e3%81%bf%e3%82%8b","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2013\/12\/26\/python%e3%81%aeboto%e3%81%a7%e9%81%8a%e3%82%93%e3%81%a7%e3%81%bf%e3%82%8b\/","title":{"rendered":"Python\u306eboto\u3067\u904a\u3093\u3067\u307f\u308b"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u6cb3\u91ce\u3067\u3059\u3002<\/p>\n<p>\u4eca\u5e74\u306f\u3078\u3073\u5e74\u3067\u3057\u305f\u3002\u306a\u306e\u3067\u3001Python\u3092\u3084\u308a\u307e\u3057\u3087\u3046\u3068\u3044\u3063\u305f\u8a71\u304c\u5e74\u59cb\u306b\u8a71\u984c\u306b\u306a\u3063\u3066\u3044\u305f\u308a\u3057\u307e\u3057\u305f\u3002<\/p>\n<p><a href=\"http:\/\/anond.hatelabo.jp\/20130101082333\">http:\/\/anond.hatelabo.jp\/20130101082333<\/a><br \/>\n\u306f\u3066\u30d6\u3082\u73fe\u6642\u70b9\u3067934\u3082\u3042\u308a\u307e\u3059\u306d\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u3001\u3053\u306e\u8a18\u4e8b\u306e\u4e2d\u3067\u306f\u30012\u7cfb\u30683\u7cfb\u3069\u3063\u3061\u306b\u3059\u308b\u304b\uff1f\u3068\u3044\u3046\u3068\u3053\u308d\u304c\u6b6f\u5207\u308c\u304c\u60aa\u304b\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u30a6\u30a7\u30d6\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u524d\u63d0\u3068\u3059\u308b\u3068\u3001\u4e3b\u8981\u306a\u30a6\u30a7\u30d6\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306f\u3059\u3067\u306b3\u7cfb\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u307e\u3059\u3057\u30013\u7cfb\u3067\u826f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u79c1\u306f\u524d\u8077\u3067Python\u3092\u4f7f\u3063\u3066EC\u3092\u69cb\u7bc9\u3057\u305f\u308a\u3057\u3066\u305f\u3093\u3067\u3059\u304c\u3001\u8ee2\u8077\u5f8c\u307b\u3068\u3093\u3069\u89e6\u308b\u6a5f\u4f1a\u304c\u306a\u304b\u3063\u305f\u3057\u3001\u4eca\u56de\u306fboto\u3067\u904a\u3093\u3067\u307f\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002\u304e\u308a\u304e\u308a\u4eca\u5e74\u4e2d\u306bPython\u89e6\u3063\u3068\u3053\u3046\u3068\u3044\u3046\u611f\u3058\u3067\u3059\u3002<\/p>\n<h2>boto\u3068\u306f\uff1f<\/h2>\n<p>boto\u306fAWS\u3092\u64cd\u4f5c\u3059\u308b\u305f\u3081\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002<br \/>\naws-cli\u3067\u3082\u4f7f\u7528\u3055\u308c\u3066\u3044\u3066\u3001AWS\u3067\u306fPython\u7248\u306eSDK\u3068\u3057\u3066\u7d39\u4ecb\u3055\u308c\u3066\u3044\u307e\u3059\u3002<br \/>\n<a href=\"http:\/\/aws.amazon.com\/jp\/sdkforpython\/\">AWS SDK for Python | \u30a2\u30de\u30be\u30f3 \u30a6\u30a7\u30d6 \u30b5\u30fc\u30d3\u30b9\uff08AWS \u65e5\u672c\u8a9e\uff09<\/a><\/p>\n<h2>boto\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n<p>boto\u306f\u3001PyPI\u306e\u60c5\u5831\u3092\u898b\u305f\u611f\u3058\u3067\u306f3\u7cfb\u306b\u306f\u307e\u3060\u3061\u3083\u3093\u3068\u5bfe\u5fdc\u3057\u3066\u306a\u3044\u3088\u3046\u3067\u3059\u3002(aws-cli\u306fpython3\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u3069\u3046\u3084\u3089boto\u306e\u958b\u767a\u7248\u3092\u4f7f\u3063\u3066\u3044\u308b\u3088\u3046\u3067\u3059)<\/p>\n<p>\u79c1\u306e\u74b0\u5883\u306fcentos\u306a\u306e\u3067\u3001\u307e\u305a\u306f\u3001python\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u7ba1\u7406\u30c4\u30fc\u30eb\u3067\u3042\u308bpip\u3092yum\u3067\u5165\u3066\u3001pip\u7d4c\u7531\u3067boto\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<br \/>\n\u3064\u3044\u3067\u306b\u5bfe\u8a71\u7684\u306b\u64cd\u4f5c\u3057\u3084\u3059\u304f\u306a\u308b\u3001ipython\u3082\u5165\u308c\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">$ sudo yum install python-pip\r\n$ sudo pip install boto\r\n$ sudo pip install ipython<\/pre>\n<p>\u7c21\u5358\u3067\u3059\u306d\u3002<\/p>\n<h2>\u5b9f\u969b\u306b\u4f7f\u3063\u3066\u307f\u308b<\/h2>\n<p>\u65e9\u901fipython\u3092\u8d77\u52d5\u3057\u3066\u3001boto\u3092\u89e6\u3063\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"lang:sh decode:true\">$ ipython<\/pre>\n<p>\u8d77\u52d5\u3059\u308b\u3068\u30b3\u30f3\u30bd\u30fc\u30eb\u306bpython\u306e\u30b3\u30fc\u30c9\u304c\u5165\u529b\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3068\u308a\u3042\u3048\u305a\u52d5\u4f5c\u78ba\u8a8d\u3068\u3044\u3046\u3053\u3068\u3067\u3001EC2\u306e\u30ea\u30fc\u30b8\u30e7\u30f3\u4e00\u89a7\u3092\u8868\u793a\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"lang:python decode:true\">In [1]: import boto.ec2\r\n\r\nIn [2]: for region in boto.ec2.regions():\r\n   ...:     print(region.name)\r\n   ...:\r\nap-southeast-1\r\nap-southeast-2\r\nus-west-2\r\nus-east-1\r\nus-gov-west-1\r\nus-west-1\r\ncn-north-1\r\nsa-east-1\r\nap-northeast-1\r\neu-west-1\r\n\r\nIn [3]: exit<\/pre>\n<p>\u3061\u3083\u3093\u3068\u30ea\u30fc\u30b8\u30e7\u30f3\u306e\u540d\u79f0\u304c\u8868\u793a\u3055\u308c\u307e\u3057\u305f\u306d\u3002<\/p>\n<h2>boto\u306e\u8a8d\u8a3c\u306b\u3064\u3044\u3066<\/h2>\n<p>\u3053\u3053\u304b\u3089\u5148\u306e\u64cd\u4f5c\u306f\u8a8d\u8a3c\u304c\u5fc5\u8981\u306b\u306a\u308b\u306e\u3067\u3001IAM\u3067\u8a8d\u8a3c\u60c5\u5831\u3092\u53d6\u5f97\u3057\u3066\u304a\u304d\u307e\u3059\u3002<br \/>\n\u5fc5\u8981\u306b\u306a\u308b\u306e\u306f\u3001AccessKeyID\u3068SecretAccessKey\u3067\u3059\u3002<\/p>\n<p>boto\u306e\u8a8d\u8a3c\u65b9\u6cd5\u306b\u3064\u3044\u3066\u306f\u4ee5\u4e0b\u306b\u8a73\u7d30\u304c\u66f8\u3044\u3066\u3042\u308a\u307e\u3059\u3002<br \/>\n<a href=\"http:\/\/docs.pythonboto.org\/en\/latest\/boto_config_tut.html\">Boto Config \u2014 boto v2.21.1<\/a><\/p>\n<p>\u4eca\u56de\u306f\u74b0\u5883\u5909\u6570\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:python decode:true\">$ export AWS_ACCESS_KEY_ID=\r\n$ export AWS_SECRET_ACCESS_KEY=\r\n$ ipython<\/pre>\n<h2>EC2\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u4e00\u89a7\u306e\u53d6\u5f97\u30fb\u8d77\u52d5\u30fb\u505c\u6b62<\/h2>\n<p>\u3067\u306f\u3001EC2\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u4e00\u89a7\u3092\u53d6\u5f97\u3057\u3001\u7279\u5b9a\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u8d77\u52d5\u3068\u505c\u6b62\u3092\u884c\u3063\u3066\u307f\u307e\u3059\u3002<\/p>\n<h3>\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u4e00\u89a7\u306e\u53d6\u5f97<\/h3>\n<pre class=\"lang:python decode:true\">In [1]: from boto import ec2\r\n\r\nIn [2]: ec2conn = ec2.connect_to_region(\"ap-northeast-1\")\r\n\r\nIn [3]: instances = [i for r in ec2conn.get_all_instances() for i in r.instances]\r\n\r\nIn [4]: print(instances)\r\n[Instance:i-11111111, Instance:i-22222222, Instance:i-33333333, Instance:i-44444444, Instance:i-55555555, Instance:i-66666666]\r\n\r\nIn [5]: for instance in instances:\r\n   ...:     print(\"id: {0}, state: {1}, private_ip: {2}\".format(instance.id, instance.state, instance.private_ip_address))\r\n   ...:\r\nid: i-11111111, state: stopped, private_ip: 172.31.17.31\r\nid: i-22222222, state: stopped, private_ip: 172.31.31.130\r\nid: i-33333333, state: stopped, private_ip: 172.31.26.255\r\nid: i-44444444, state: stopped, private_ip: 172.31.25.106\r\nid: i-55555555, state: stopped, private_ip: 172.31.20.209\r\nid: i-66666666, state: stopped, private_ip: 172.31.28.231<\/pre>\n<p>boto.ec2.connect_to_regino\u3067\u30ea\u30fc\u30b8\u30e7\u30f3\u306b\u63a5\u7d9a\u3057\u307e\u3059\u3002<strong>boto.ec2.connection.EC2Connection<\/strong>\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u8fd4\u3063\u3066\u304d\u307e\u3059\u3002<\/p>\n<p>\u3053\u306eec2conn\u306b\u5bfe\u3057\u3066\u3001get_all_instances\u3092\u547c\u3073\u51fa\u3059\u3068\u3001\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u4e00\u89a7\u304c\u53d6\u5f97\u3067\u304d\u305d\u3046\u3067\u3059\u304c\u3001<strong>boto.ec2.instance.Reservation<\/strong>\u306e\u30ea\u30b9\u30c8\u304c\u8fd4\u3063\u3066\u304d\u307e\u3059\u3002<\/p>\n<p>Reservation\u306finstances\u3068\u3044\u3046\u30d7\u30ed\u30d1\u30c6\u30a3\u3067<strong>boto.ec2.instance.Instance<\/strong>\u306e\u30ea\u30b9\u30c8\u3092\u6301\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u306a\u306e\u3067\u30012\u91cd\u30eb\u30fc\u30d7\u3067EC2\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u30ea\u30b9\u30c8\u3092\u751f\u6210\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u6700\u5f8c\u306b\u3001\u53d6\u5f97\u3057\u305f\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u30ea\u30b9\u30c8\u304b\u3089\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9ID\u3068\u8d77\u52d5\u306e\u72b6\u614b\u3001\u30d7\u30e9\u30a4\u30d9\u30fc\u30c8\u30a2\u30c9\u30ec\u30b9\u3092\u8868\u793a\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<h3>\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u8d77\u52d5<\/h3>\n<p>\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u8d77\u52d5\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"lang:python decode:true\">In [7]: instance = instances[0]\r\n\r\nIn [8]: instance.state\r\nOut[8]: u'stopped'\r\n\r\nIn [9]: instance.start()\r\n\r\nIn [10]: instance.state\r\nOut[10]: u'pending'\r\n\r\nIn [11]: instance.update()\r\nOut[11]: u'running'<\/pre>\n<p>\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u8d77\u52d5\u3082\u7c21\u5358\u3067\u3059\u306d\u3002state\u3067\u8d77\u52d5\u306e\u72b6\u614b\u304c\u78ba\u8a8d\u3067\u304d\u307e\u3059\u304c\u3001update\u3092\u5b9f\u884c\u3057\u306a\u3044\u3068\u60c5\u5831\u304c\u66f4\u65b0\u3055\u308c\u307e\u305b\u3093\u3002<\/p>\n<h3>\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u505c\u6b62<\/h3>\n<pre class=\"lang:python decode:true\">In [13]: instance.state\r\nOut[13]: u'running'\r\n\r\nIn [14]: instance.stop()\r\n\r\nIn [15]: instance.state\r\nOut[15]: u'stopping'\r\n\r\nIn [17]: instance.update()\r\nOut[17]: u'stopped'<\/pre>\n<p>\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u505c\u6b62\u3082\u7c21\u5358\u3067\u3059\u306d\u3002<\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<p>\u4eca\u56de\u3084\u3063\u305f\u3088\u3046\u306a\u3053\u3068\u3067\u3042\u308c\u3070\u3001aws-cli\u3092\u4f7f\u3063\u305f\u65b9\u304c\u624b\u3063\u53d6\u308a\u65e9\u3044\u3068\u601d\u3046\u306e\u3067\u3059\u304c\u3001\u81ea\u52d5\u5316\u3057\u305f\u308a\u3068\u304b\u3001\u7d30\u304b\u306a\u5236\u5fa1\u3092\u884c\u3063\u305f\u308a\u3059\u308b\u5834\u5408\u306b\u4f7f\u3048\u308b\u306e\u304b\u306a\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001EC2\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u8d77\u52d5\u30fb\u505c\u6b62\u306b\u3064\u3044\u3066\u8a00\u3048\u3070\u3001\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u306a\u3069\u3092\u6e21\u305b\u306a\u304f\u3066\u3001\u81ea\u5206\u3067update\u3057\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u306e\u3067\u3001\u81ea\u52d5\u5316\u3057\u305f\u308a\u3059\u308b\u5834\u5408\u306b\u306f\u3001\u3082\u3046\u5c11\u3057\u5de5\u592b\u304c\u5fc5\u8981\u305d\u3046\u3067\u3059\u306d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u6cb3\u91ce\u3067\u3059\u3002<\/p>\n<p>\u4eca\u5e74\u306f\u3078\u3073\u5e74\u3067\u3057\u305f\u3002\u306a\u306e\u3067\u3001Python\u3092\u3084\u308a\u307e\u3057\u3087\u3046\u3068\u3044\u3063\u305f\u8a71\u304c\u5e74\u59cb\u306b\u8a71\u984c\u306b\u306a\u3063\u3066\u3044\u305f\u308a\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>http:\/\/anond.hatelabo.jp\/20130101082333<br \/>\n\u306f\u3066\u30d6\u3082\u73fe\u6642\u70b9\u3067934\u3082\u3042\u308a\u307e\u3059\u306d\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2013\/12\/26\/python%e3%81%aeboto%e3%81%a7%e9%81%8a%e3%82%93%e3%81%a7%e3%81%bf%e3%82%8b\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[119,120],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/3700"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=3700"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/3700\/revisions"}],"predecessor-version":[{"id":17893,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/3700\/revisions\/17893"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=3700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=3700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=3700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}