{"id":1035,"date":"2017-01-22T13:10:40","date_gmt":"2017-01-22T19:10:40","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=1035"},"modified":"2017-01-22T13:12:36","modified_gmt":"2017-01-22T19:12:36","slug":"aws-api-and-python-boto","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/aws-api-and-python-boto\/","title":{"rendered":"AWS API and Python Boto"},"content":{"rendered":"
Quick note on connection to EC2 to list instances.<\/p>\n
- Ensure IAM User permissions. In my case I tried EC2FullAccess.
\n- Ensure you have your access and secret key handy.
\n- This example just cycle through regions and list any instances.<\/p>\n
\r\nimport argparse\r\nimport boto.ec2\r\n\r\naccess_key = ''\r\nsecret_key = ''\r\n\r\ndef get_ec2_instances(region):\r\n ec2_conn = boto.ec2.connect_to_region(region,\r\n aws_access_key_id=access_key,\r\n aws_secret_access_key=secret_key)\r\n reservations = ec2_conn.get_all_reservations()\r\n for reservation in reservations: \r\n print region+':',reservation.instances\r\n\r\n for vol in ec2_conn.get_all_volumes():\r\n print region+':',vol.id\r\n\r\ndef main():\r\n regions = ['us-east-1','us-west-1','us-west-2','eu-west-1','sa-east-1',\r\n 'ap-southeast-1','ap-southeast-2','ap-northeast-1']\r\n parser = argparse.ArgumentParser()\r\n parser.add_argument('access_key', help='Access Key');\r\n parser.add_argument('secret_key', help='Secret Key');\r\n args = parser.parse_args()\r\n global access_key\r\n global secret_key\r\n access_key = args.access_key\r\n secret_key = args.secret_key\r\n \r\n for region in regions: get_ec2_instances(region)\r\n\r\nif __name__ =='__main__':main()\r\n<\/pre>\nExample:<\/p>\n
$ python list.py myaccess_key mysecret_key
\nus-east-1: [Instance:i-1aac5699]
\nus-east-1: vol-d121290e<\/p>\n","protected":false},"excerpt":{"rendered":"Quick note on connection to EC2 to list instances. – Ensure IAM User permissions. In my case I tried EC2FullAccess.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73,13],"tags":[],"class_list":["post-1035","post","type-post","status-publish","format-standard","hentry","category-aws","category-python"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=1035"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1035\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=1035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=1035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=1035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}