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
Generating CSV, Excel files Using Python Shirisha Gaddi

In most of the cases, you need to export the data from your database to different formats. In this post I will show you how ...

Continue Reading...
Create excel file, Insert image, Draw Bar Graphs in excel files in python using xlsxwriter Chaitanya Kattineni

Xlsxwriter is a python module through which we can write data to Excel 2007+ XLSX file format. In this blog post we learn to write ...

Continue Reading...
How to implement Case Insensitive CSV DictReader in python Chaitanya Kattineni

In general use cases we upload the CSV files to the system to store huge amount of data by uploading single file. For example in ...

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