#!/usr/bin/python -tt
import sys
import boto
import boto.ec2
import boto.ec2.zone
import boto.ec2.elb
import boto.ec2.autoscale
import boto.ec2.cloudwatch
#from boto.ec2.elb import loadbalancer
from boto.ec2.connection import EC2Connection
from boto.ec2 import connect_to_region
from boto.ec2.elb import ELBConnection
from boto.ec2.elb import HealthCheck
from boto.ec2.autoscale import AutoScaleConnection
from boto.ec2.autoscale import LaunchConfiguration
from boto.ec2.autoscale import AutoScalingGroup
from boto.ec2.autoscale import ScalingPolicy
from boto.ec2.cloudwatch import MetricAlarm
region = "eu-west-1" #The region I am connecting to
zoneStrings = 'eu-west-1a','eu-west-1b' #availability zones of the VPC
#storage of data related to each section is performed through python dictionaries
elastic_load_balancer = {
'name': 'portland-lb',#The name of your load balancer
'health_check_target': 'HTTP:8080/',#Location to perform health checks
'ports': [(80, 8080, 'http'), (443, 8443, 'tcp')],#[Load Balancer Port, EC2 Instance Port, Protocol]
'timeout': 5, #Number of seconds to wait for a response from a ping
'interval': 30, #Number of seconds between health checks
'vpc_subnets' : ['subnet-09519f50', 'subnet-9fa12dfa'],
'security_groups': ['sg-be77e8db']
}
lc_name = 'portland-lc'
autoscaling_group = {
'name': 'portland-autoscaling-group',#descriptive name for your auto scaling group
'min_size': 1,#Minimum number of instances that should be running at all times
'max_size': 2, #Maximum number of instances that should be running at all times
'desired_capacity' : 1,
'vpc_subnets' : ['subnet-09519f50', 'subnet-9fa12dfa']
}
portland_ami = {
'id': 'ami-0f870678', #The AMI ID of the instance your Auto Scaling group will launch
'access_key': 'demo_keys', #The key the EC2 instance will be configured with
'security_groups': ['sg-be77e8db'], #The security group(s) your instances will belong to
'instance_type': 't2.micro', #The size of instance that will be launched
'instance_monitoring': True, #Indicated whether the instances will be launched with detailed monitoring enabled for CloudWatch.
'associate_public_ip': ['subnet-09519f50', 'subnet-9fa12dfa']
}
elb_conn = boto.ec2.elb.connect_to_region('eu-west-1')
hc = HealthCheck(access_point=elastic_load_balancer['name'],#'healthCheck',
interval=elastic_load_balancer['interval'],
target=elastic_load_balancer['health_check_target'],
timeout=elastic_load_balancer['timeout'])
lb = elb_conn.create_load_balancer(elastic_load_balancer['name'],
#zoneStrings,
zones=None,
subnets=elastic_load_balancer['vpc_subnets'],
security_groups=elastic_load_balancer['security_groups'],
scheme='internet-facing',
complex_listeners=None)
#elastic_load_balancer['ports']
lb.configure_health_check(hc)
print "Map the CNAME of your website to: %s" % (lb.dns_name)
print "Load balancer successfully created! Please check aws for more information."
#Launch Configuration and Auto Scaling Group Creation
asg_conn = boto.ec2.autoscale.connect_to_region('eu-west-1')
lc = LaunchConfiguration(name=lc_name, image_id=portland_ami['id'],
key_name=portland_ami['access_key'],
security_groups=portland_ami['security_groups'],
instance_type=portland_ami['instance_type'],
instance_monitoring=portland_ami['instance_monitoring'],
associate_public_ip_address=portland_ami['associate_public_ip'])
asg_conn.create_launch_configuration(lc)
print "launch configuration created"
ag = AutoScalingGroup(group_name=autoscaling_group['name'], load_balancers=[elastic_load_balancer['name']],
availability_zones=zoneStrings,
launch_config=lc, min_size=autoscaling_group['min_size'], max_size=autoscaling_group['max_size'],
vpc_zone_identifier=autoscaling_group['vpc_subnets'])
asg_conn.create_auto_scaling_group(ag)
print "Auto-Scaling Group created"
#Creation of Scaling Policies
scalingUpPolicy = ScalingPolicy(name='webserverScaleUpPolicy',
adjustment_type='ChangeInCapacity',
scaling_adjustment=2,
cooldown=180)
scalingDownPolicy = ScalingPolicy(name='webserverScaleDownPolicy',
adjustment_type='ChangeInCapacity',
scaling_adjustment=-1,
cooldown=180)
asg_conn.create_scaling_policy(scalingUpPolicy)
print "Scale up policy created"
asg_conn.create_scaling_policy(scalingDownPolicy)
print "Scale down policy created"
#Policy Refresh
scalingUpPolicy = asg_conn.get_all_policies(as_group=ag.name, policy_names=['webserverScaleUpPolicy'])[0]
scalingDownPolicy = asg_conn.get_all_policies(as_group=ag.name, policy_names=['webserverScaleDownPolicy'])[0]