Getting Started with Boto ( python Interface for AWS )

25 / Jan / 2015 by Vikash Jha 4 comments

Introduction

 
Boto is a python package  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  : Installation
Step 2 : Configuration
Step 3 : Creating Connection
Step 4 : Examples

Installation

 
We need to install boto library in order to use it.  We can use pip to install boto.

[js] $ sudo pip install boto
[/js]
 

Configuration

 
In order to work with boto library we need to configure Boto Credentials which require AWS Access Keys and Secret Keys. We’ll need to create one IAM user with a policy attached with proper Resource permission as required.

Create a ~/.boto file with below syntax:

[js]
[Credentials]
aws_access_key_id = YOURACCESSKEY
aws_secret_access_key = YOURSECRETKEY

[/js]

Once the boto is configured, we’ll start writing python scripts.

Creating Connection

 
Before calling any AWS resources we need to create connections first.

[js]
import boto
from boto import ec2
connection = ec2.connect_to_region(‘region_name’)

[/js]

We can also create connection using aws keys directly if ~/.boto is not configured

[js]
import boto
from boto import ec2
connection=ec2.connect_to_region(‘region_name’,aws_access_key_id=”,aws_secret_access_key=”
[/js]

Examples

 

1. Script which will list all the Instances and associated IP Address:

[js]
import boto
from boto import ec2
connection=ec2.connect_to_region("region_name")
reservations=connection.get_all_instances();

for reservation in reservations:
for instances in reservation.instances:
print "%s \t \t %s" % (instances.tags[‘Name’], instances.ip_address)

[/js]
 

2. Script to create snapshots of all the Volumes and Tag them with Instance Name:

[js]

import boto
import sys
from boto import ec2
connection=ec2.connect_to_region(‘region_name’)

try:
volumes=connection.get_all_volumes()

# This function is used to tag the Volumes with the Instance Name
def tag_volume(vol):
instance_id=vol.attach_data.instance_id
instance_tag=connection.get_all_tags({‘resource-id’:instance_id})
for tag in instance_tag:
vol.add_tag(‘Name’,tag.value)

for volume in volumes:
connection.create_snapshot(volume.id,tag_volume(volume))

except:
print ‘Some Error occurred :’
print sys.exc_info()
[/js]
 

3. Script to list all the Instances having port 22 open for all IP [ 0.0.0.0/0 ]

[js]
import sys
import boto
from boto import ec2
connection=ec2.connect_to_region("region-name")
sg=connection.get_all_security_groups()

def getTag(instanceId):

reservations=connection.get_all_instances(filters={‘instance_id’:instanceId})
for res in reservations:
for instance in res.instances:
return instance.tags[‘Name’]

try:

for securityGroup in sg:
for rule in securityGroup.rules:
global instanceId;
if rule.to_port == ’22’ and ‘0.0.0.0/0’ in str(rule.grants):
for instanceid in securityGroup.instances():
instanceId=str(instanceid)
print "Port 22 open for all IP:"
print " SecurityGroupName: %s –> Instance Name: %s" %(securityGroup.name, getTag(instanceId.split(‘:’)[1]))

except :
print ‘Some Error occurred : ‘
print sys.exc_info()

[/js]

Leave a comment if you have any questions regarding this article.

FOUND THIS USEFUL? SHARE IT

Tag -

boto python

comments (4)

  1. Narayana Swamy

    trying to tag all EC2 by using Lambda below is code to list as of now.. and error …
    import boto
    from boto import ec2
    connection=ec2.connect_to_region(“region_name”)
    reservations=connection.get_all_instances();
    for reservation in reservations:
    for instances in reservation.instances:
    print “%s \t \t %s” % (instances.tags[‘Name’], instances.ip_address)

    #### ERROR #####
    Function Logs:
    START RequestId: 3ab8f2ab-6d3a-11e8-b3f2-4fde2564ad7b Version: $LATEST
    Syntax error in module ‘lambda_function’: invalid syntax (lambda_function.py, line 7)

    Reply
  2. Ravikumuar

    print “%s \t \t %s” % (instances.tags[‘Name’], instances.ip_address), is there any way to print instance event ?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *