Reading Time : ~ .

Okay, this is day two and i want to put fabric to develop some thing meaningful. Lets start with amazon EC2. I want to develop an interactive script where i get to choose ec2 region, ec2 flavour etc. being blessed with python we have boto, a swiss army knife for amazon web services. i assume you have all set (aws account, security group, key pair, access key id, secret access key) so this is not going to be fabric but i want this on the way as we need to create server then do stuff with faric.

from fabric.api import *
from fabric.colors import green as _green, yellow as _yellow
import boto
import boto.ec2
import time

def create_ec2():
	ubuntu_images = {	"ap-northeast-1":"ami-bfc0afbe",
	ec2_region = ""
	ec2_key = "your Access Key ID"
	ec2_secret = "Secret Access Key"
	ec2_key_pair = "key pair name you created in the region you will select in this program"
	ec2_security = ("default",)
	ec2_instancetype = "m1.small"
	regions = boto.ec2.regions()

	for r in regions:
		r = str(r)
		if r[r.index(':')+1:] =="us-gov-west-1":

		print str(i) + " : " + r[r.index(':')+1:]
		i += 1

	i = raw_input("your choice : ")
	ec2_region = str(regions[int(i)])
	ec2_region = ec2_region[ec2_region.index(':')+1:]

	conn = boto.ec2.connect_to_region(ec2_region, aws_access_key_id=ec2_key, aws_secret_access_key=ec2_secret)
	print(_green("connected to : "  + ec2_region))
	print ubuntu_images[ec2_region]
	reservation = conn.run_instances(ubuntu_images[ec2_region],
						key_name = ec2_key_pair,
						security_groups =  ec2_security,
						instance_type = ec2_instancetype
	instance = reservation.instances[0]
	while instance.state == u'pending':
		print(_yellow("Instance state: %s" % instance.state))
	print(_green("Instance state: %s" % instance.state))

	print(_green("Instance created succeffully!"))

	print(_green("Allocating new elastic ip..."))
	ip = conn.allocate_address()
	print(_green("Attaching elastic ip to instance..."))

	print(_green("Public dns: %s" % instance.public_dns_name))

	print "Instance created and attached to public ip : " + ip.public_ip

now the out put is...

hotice@ashwin-ws:~$ fab create_ec2
0 : ap-southeast-1
1 : ap-southeast-2
2 : us-west-2
3 : us-east-1
4 : us-west-1
5 : sa-east-1
6 : ap-northeast-1
7 : eu-west-1
your choice : 3
connected to : us-east-1
Instance state: pending
Instance state: pending
Instance state: pending
Instance state: pending
Instance state: pending
Instance state: pending
Instance state: running
Instance created succeffully!
Allocating new elastic ip...
Attaching elastic ip to instance...
Public dns: ec2-54-196-108-1.compute-1.amazonaws.com
Instance created and attached to public ip :

okay... this is not fabric, this is just python boto script. as i said earlier, i will do the fabric stuff in next of this series...

    By Posted On

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Working with python collections part 1 Rakesh babu Podishetty

Python Collections - named tuple is to access by the names specified and deque is to append and pop the elements from both sides of ...

Continue Reading...
Building and Parsing XML Document using Python Swetha Naretla

Creating XML document with required elements, Then Parsing it using Python to generate a serialized form of its contents.

Continue Reading...
Python Coding Techniques and Programming Practices Anjaneyulu Batta

Coding techniques and programming practices are one of the features of a professional programmer. While writing code to solve a problem programmer should make simple ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties