1. Start an instance in EC2
EC2(Elastic Compute Cloud) it is a virtual server that provides resizable
computing capacity on cloud
FLOW:
1) Make connection
2) Make reservation
3) Check reservation status
import boto.ec2
from time import sleep
#ACESS_key,SECRET_KEY etc are user defined
#ACCESS_KEY,SECRET_KEY AND REGION are common for all the services
ACCESS_KEY='<Access key>'
SECRET_KEY="<Secret key>"
REGION='us-east-1'
#connect to EC2
#common to all codes
conn=boto.ec2.connect_to_region(REGION,aws_access_key_id=ACCESS_KEY,aws_s
ecret_access_key=SECRET_KEY)
#Basically connect to region function needs two argument, a region and a
#dictionary. the above function is similar to
#boto.ec2.connect_to_region(REGION, {aws_access_key_id:ACCESS_KEY,
aws_secret_access_key=SECRET_KEY} )
#AMI_ID is the image id of the image which we want to launch
#EC2_KEY_HANDLE is the key pair which we want to use to launch the
instance
#SECURITY_GROUP is the security group which we want to use to launch the
instance
#AMI_ID, INSTANCE_TYPE, EC2_KEY_HANDLE AND SECURITY GROUP VARIES FOR EACH
CODE
AMI_ID='<AMI_ID>'
INSTANCE_TYPE='t2.micro'
EC2_KEY_HANDLE='mykey'
SECURITY_GROUP='mygroup'
,#LAUNCHING INSTANCE
reservation=conn.run_instances(AMI_ID, key_name=EC2_KEY_HANDLE,
instance_type=INSTANCE_TYPE, security_groups=[SECURITY_GROUP])
instance=reservation.instances[0] #choosing 0th from list of instances
print ("Instance Launched")
#WAITING FOR INSTANCE TO BECOME ACTIVE
state=instance.update()
while state=='pending':
print ("Instance is pending")
sleep(5)
state=instance.update()
if(state=='running'):
print ("Instance is running")
print("Instance ID: ",instance.id)
print("Instance Public DNS: ",instance.public_dns_name)
print("launch time: ",instance.launch_time)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Terminating a EC2 instance
import boto.ec2
from time import sleep
ACCESS_KEY='<ACCESS_KEY>'
SECRET_KEY="<Seceret key>"
REGION='us-east-1'
AMI_ID='<AMI_ID>'
INSTANCE_TYPE='t2.micro'
EC2_KEY_HANDLE='mykey'
SECURITY_GROUP='mygroup'
#connect to EC2
, conn=boto.ec2.connect_to_region(REGION,aws_access_key_id=ACCESS_KEY,aws_s
ecret_access_key=SECRET_KEY)
#getting all reservations with all its instances
reservations=conn.get_all_instances()
#iterating through all the instances
instance_rs=reservations[0].instances
instance=instance_rs[0]
instanceid=instance.id
#stopping the instance
conn.stop_instances(instance_ids=[instanceid])
#waiting for the instance to stop
state=instance.update()
while state=='stopping':
print ("Instance is stopping")
sleep(5)
state=instance.update()
print ("Instance is stopped")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2. AutoScaling
Autoscaling is a AWS service which is used to launch or terminate EC2
instances based on user defined policies(Scaling up / Scaling down)
import boto.ec2.autoscale
from boto.ec2.autoscale import *
import boto.ec2.cloudwatch
from time import sleep
ACCESS_KEY ="<Access key>"
SECRET_KEY="<Secret key>"
REGION='us-east-1'
FLOW:
4) Connect to region with autoscale;
EC2(Elastic Compute Cloud) it is a virtual server that provides resizable
computing capacity on cloud
FLOW:
1) Make connection
2) Make reservation
3) Check reservation status
import boto.ec2
from time import sleep
#ACESS_key,SECRET_KEY etc are user defined
#ACCESS_KEY,SECRET_KEY AND REGION are common for all the services
ACCESS_KEY='<Access key>'
SECRET_KEY="<Secret key>"
REGION='us-east-1'
#connect to EC2
#common to all codes
conn=boto.ec2.connect_to_region(REGION,aws_access_key_id=ACCESS_KEY,aws_s
ecret_access_key=SECRET_KEY)
#Basically connect to region function needs two argument, a region and a
#dictionary. the above function is similar to
#boto.ec2.connect_to_region(REGION, {aws_access_key_id:ACCESS_KEY,
aws_secret_access_key=SECRET_KEY} )
#AMI_ID is the image id of the image which we want to launch
#EC2_KEY_HANDLE is the key pair which we want to use to launch the
instance
#SECURITY_GROUP is the security group which we want to use to launch the
instance
#AMI_ID, INSTANCE_TYPE, EC2_KEY_HANDLE AND SECURITY GROUP VARIES FOR EACH
CODE
AMI_ID='<AMI_ID>'
INSTANCE_TYPE='t2.micro'
EC2_KEY_HANDLE='mykey'
SECURITY_GROUP='mygroup'
,#LAUNCHING INSTANCE
reservation=conn.run_instances(AMI_ID, key_name=EC2_KEY_HANDLE,
instance_type=INSTANCE_TYPE, security_groups=[SECURITY_GROUP])
instance=reservation.instances[0] #choosing 0th from list of instances
print ("Instance Launched")
#WAITING FOR INSTANCE TO BECOME ACTIVE
state=instance.update()
while state=='pending':
print ("Instance is pending")
sleep(5)
state=instance.update()
if(state=='running'):
print ("Instance is running")
print("Instance ID: ",instance.id)
print("Instance Public DNS: ",instance.public_dns_name)
print("launch time: ",instance.launch_time)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Terminating a EC2 instance
import boto.ec2
from time import sleep
ACCESS_KEY='<ACCESS_KEY>'
SECRET_KEY="<Seceret key>"
REGION='us-east-1'
AMI_ID='<AMI_ID>'
INSTANCE_TYPE='t2.micro'
EC2_KEY_HANDLE='mykey'
SECURITY_GROUP='mygroup'
#connect to EC2
, conn=boto.ec2.connect_to_region(REGION,aws_access_key_id=ACCESS_KEY,aws_s
ecret_access_key=SECRET_KEY)
#getting all reservations with all its instances
reservations=conn.get_all_instances()
#iterating through all the instances
instance_rs=reservations[0].instances
instance=instance_rs[0]
instanceid=instance.id
#stopping the instance
conn.stop_instances(instance_ids=[instanceid])
#waiting for the instance to stop
state=instance.update()
while state=='stopping':
print ("Instance is stopping")
sleep(5)
state=instance.update()
print ("Instance is stopped")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2. AutoScaling
Autoscaling is a AWS service which is used to launch or terminate EC2
instances based on user defined policies(Scaling up / Scaling down)
import boto.ec2.autoscale
from boto.ec2.autoscale import *
import boto.ec2.cloudwatch
from time import sleep
ACCESS_KEY ="<Access key>"
SECRET_KEY="<Secret key>"
REGION='us-east-1'
FLOW:
4) Connect to region with autoscale;