FABRIC – LEARNING PART 2

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",
						"ap-southeast-1":"ami-92aef9c0",
						"ap-southeast-2":"ami-57f16f6d",
						"eu-west-1":"ami-dea653a9",
						"sa-east-1":"ami-4da10150",
						"us-east-1":"ami-951524fc",
						"us-west-1":"ami-b0784af5",
						"us-west-2":"ami-36d6b006",
					}
	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()

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

		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))
		time.sleep(2)
		instance.update()
	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..."))
	ip.associate(instance_id=instance.id)

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

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

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
ami-951524fc
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 : 54.204.13.213

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
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
How to access development server publicly using Localtunnel Ashwin Kumar

We do need to expose our local server to hit call-back URLs while programming with other APIs. There is a tool called local tunnel from ...

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...
Programming with python: Decorators Ashwin Kumar

Python decorators supports aspect-oriented programming. It is used to add or modify code in functions or classes. Using decorators will provide security, tracing, looking ..etc ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties