{"id":16981,"date":"2015-01-25T20:57:16","date_gmt":"2015-01-25T15:27:16","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=16981"},"modified":"2015-01-25T21:00:02","modified_gmt":"2015-01-25T15:30:02","slug":"getting-started-with-boto-python-interface-for-aws","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/getting-started-with-boto-python-interface-for-aws\/","title":{"rendered":"Getting Started with Boto ( python Interface for AWS )"},"content":{"rendered":"<h3 style=\"text-align: justify;color: #ff9900\">Introduction<\/h3>\n<p>&nbsp;<br \/>\nBoto is a python package\u00a0 which provides an interface for AWS. With boto library, we can call the AWS resources using python script.<\/p>\n<p>This article walks you through the step by step guide for using boto library for calling AWS resources.<\/p>\n<p>Step 1 \u00a0: Installation<br \/>\nStep 2 : Configuration<br \/>\nStep 3 : Creating Connection<br \/>\nStep 4 : Examples<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">Installation<\/h3>\n<p>&nbsp;<br \/>\nWe need to install boto library in order to use it.\u00a0 We can use pip to install boto.<\/p>\n<p>[js] $ sudo pip install boto<br \/>\n[\/js]<br \/>\n&nbsp;<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">Configuration<\/h3>\n<p>&nbsp;<br \/>\nIn order to work with boto library we need to configure Boto Credentials which require AWS Access Keys and Secret Keys. We&#8217;ll need to create one IAM user with a policy attached with proper Resource permission as required.<\/p>\n<p>Create a ~\/.boto file with below syntax:<\/p>\n<p>[js]<br \/>\n[Credentials]<br \/>\naws_access_key_id = YOURACCESSKEY<br \/>\naws_secret_access_key = YOURSECRETKEY<\/p>\n<p>[\/js]<\/p>\n<p>Once the boto is configured, we&#8217;ll start writing python scripts.<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">Creating Connection<\/h3>\n<p>&nbsp;<br \/>\nBefore calling any AWS resources we need to create connections first.<\/p>\n<p>[js]<br \/>\nimport boto<br \/>\nfrom boto import ec2<br \/>\nconnection = ec2.connect_to_region(&#8216;region_name&#8217;)<\/p>\n<p>[\/js]<\/p>\n<p>We can also create connection using aws keys directly if ~\/.boto is not configured<\/p>\n<p>[js]<br \/>\nimport boto<br \/>\nfrom boto import ec2<br \/>\nconnection=ec2.connect_to_region(&#8216;region_name&#8217;,aws_access_key_id=&#8221;,aws_secret_access_key=&#8221;<br \/>\n[\/js]<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">Examples<\/h3>\n<p>&nbsp;<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">1. Script which will list all the Instances and associated IP Address:<\/h3>\n<p>[js]<br \/>\nimport boto<br \/>\nfrom boto import ec2<br \/>\nconnection=ec2.connect_to_region(&quot;region_name&quot;)<br \/>\nreservations=connection.get_all_instances();<\/p>\n<p>for reservation in reservations:<br \/>\n for instances in reservation.instances:<br \/>\n print &quot;%s \\t \\t %s&quot; % (instances.tags[&#8216;Name&#8217;], instances.ip_address) <\/p>\n<p>[\/js]<br \/>\n&nbsp;<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">2. Script to create snapshots of all the Volumes and Tag them with Instance Name:<\/h3>\n<p>[js]<\/p>\n<p>import boto<br \/>\nimport sys<br \/>\nfrom boto import ec2<br \/>\nconnection=ec2.connect_to_region(&#8216;region_name&#8217;)<\/p>\n<p>try:<br \/>\n\tvolumes=connection.get_all_volumes()<\/p>\n<p>\t# This function is used to tag the Volumes with the Instance Name<br \/>\n\tdef tag_volume(vol):<br \/>\n\t\tinstance_id=vol.attach_data.instance_id<br \/>\n\t\tinstance_tag=connection.get_all_tags({&#8216;resource-id&#8217;:instance_id})<br \/>\n\t\tfor tag in instance_tag:<br \/>\n\t\t\tvol.add_tag(&#8216;Name&#8217;,tag.value)<\/p>\n<p>\tfor volume in volumes:<br \/>\n\t\tconnection.create_snapshot(volume.id,tag_volume(volume))<\/p>\n<p>except:<br \/>\n\tprint &#8216;Some Error occurred :&#8217;<br \/>\n        print sys.exc_info()<br \/>\n[\/js]<br \/>\n&nbsp;<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">3. Script to list all the Instances having port 22 open for all IP [ 0.0.0.0\/0 ]<\/h3>\n<p>[js]<br \/>\nimport sys<br \/>\nimport boto<br \/>\nfrom boto import ec2<br \/>\nconnection=ec2.connect_to_region(&quot;region-name&quot;)<br \/>\nsg=connection.get_all_security_groups()<\/p>\n<p>def getTag(instanceId):<\/p>\n<p>\treservations=connection.get_all_instances(filters={&#8216;instance_id&#8217;:instanceId})<br \/>\n\tfor res in reservations:<br \/>\n\t\tfor instance in res.instances:<br \/>\n\t\t\treturn instance.tags[&#8216;Name&#8217;]<\/p>\n<p>try:<\/p>\n<p>\tfor securityGroup in sg:<br \/>\n\t   for rule in securityGroup.rules:<br \/>\n\t\t   global instanceId;<br \/>\n       \t\t   if rule.to_port == &#8217;22&#8217;  and &#8216;0.0.0.0\/0&#8217; in str(rule.grants):<br \/>\n                           for instanceid in securityGroup.instances():<br \/>\n                                 instanceId=str(instanceid)<br \/>\n                                 print &quot;Port 22 open for all IP:&quot;<br \/>\n             \t\t\t print &quot; SecurityGroupName: %s &#8211;&gt; Instance Name: %s&quot; %(securityGroup.name,  getTag(instanceId.split(&#8216;:&#8217;)[1]))<\/p>\n<p>except :<br \/>\n    print &#8216;Some Error occurred : &#8216;<br \/>\n    print sys.exc_info()<\/p>\n<p>[\/js]<\/p>\n<p>Leave a comment if you have any questions regarding this article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction &nbsp; Boto is a python package\u00a0 which provides an interface for AWS. With boto library, we can call the AWS resources using python script. This article walks you through the step by step guide for using boto library for calling AWS resources. Step 1 \u00a0: Installation Step 2 : Configuration Step 3 : Creating [&hellip;]<\/p>\n","protected":false},"author":100,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":16},"categories":[1174],"tags":[1611,1358],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/16981"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/100"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=16981"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/16981\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=16981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=16981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=16981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}