Getting Started with Boto ( python Interface for AWS )

25 / Jan / 2015 by Vikash Jha 3 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.

 $ sudo pip install boto 

 

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:

[Credentials]
aws_access_key_id = YOURACCESSKEY
aws_secret_access_key = YOURSECRETKEY

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

Creating Connection

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

import boto
from boto import ec2
connection = ec2.connect_to_region('region_name')

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

import boto
from boto import ec2
connection=ec2.connect_to_region('region_name',aws_access_key_id='',aws_secret_access_key=''

Examples

 

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

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) 

 

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


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()

 

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

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()

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

Tag -

boto python
FOUND THIS USEFUL? SHARE IT

comments (3)

  1. Ravikumuar

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

    Reply

Leave a comment -