Created
May 1, 2012 03:40
-
-
Save seungjin/2564765 to your computer and use it in GitHub Desktop.
autoscaling with boto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto.ec2 | |
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 | |
AWS_ACCESS_KEY = '' | |
AWS_SECRET_KEY = '' | |
region = '' | |
elastic_load_balancer = { | |
'name' : 'my-load-balancer', | |
'health_check_target' : 'HTTP:80/index.html', | |
'connection_forwarding' : [(80,80,'http'),(443,443,'tcp')], | |
'timeout' : 3, | |
'interval' : 20 | |
} | |
autoscaling_group = { | |
'name' : 'my_autoscaling_group', | |
'min_size' : 1, | |
'max_size' : 2, | |
} | |
lc_name = 'my_launch_config' | |
as_ami = { | |
'id' : 'ami-baba68d3', | |
'access_key' : 'seungjin-aws-main', | |
'security_groups' : ['seungjin-basic'], | |
'instance_type' : 't1.micro', | |
'instance_monitoring' : True | |
} | |
conn_reg = boto.ec2.connect_to_region(region_name=region) | |
zones = conn_reg.get_all_zones() | |
zoneStrings = [] | |
for zone in zones: | |
zoneStrings.append(zone.name) | |
conn_elb = ELBConnection(AWS_ACCESS_KEY, AWS_SECRET_KEY) | |
conn_as = AutoScaleConnection(AWS_ACCESS_KEY, AWS_SECRET_KEY) | |
#For a complete list of options see http://boto.cloudhackers.com/ref/ec2.html#module-boto.ec2.elb.healthcheck | |
hc = HealthCheck('healthCheck', | |
interval=elastic_load_balancer['interval'], | |
target=elastic_load_balancer['health_check_target'], | |
timeout=elastic_load_balancer['timeout']) | |
lb = conn_elb.create_load_balancer( | |
elastic_load_balancer['name'], | |
zoneStrings, | |
elastic_load_balancer['connection_forwarding'] | |
) | |
lb.configure_health_check(hc) | |
print "Mape the CNAME of your website to %s" % (lb.dns_name) | |
lc = LaunchConfiguration( | |
name = lc_name, | |
image_id = as_ami['id'], | |
key_name = as_ami['access_key'], | |
security_groups = as_ami['security_groups'], | |
instance_type = as_ami['instance_type'], | |
instance_monitoring = as_ami['instance_monitoring']) | |
conn_as.create_launch_configuration(lc) | |
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'] | |
) | |
conn_as.create_auto_scaling_group(ag) | |
#http://readthedocs.org/docs/boto/en/2.3.0/ref/autoscale.html#module-boto.ec2.autoscale.policy | |
scalingUpPolicy = ScalingPolicy( | |
name = 'webserverScaleUpPolicy', | |
adjustment_type = 'ChangeInCapacity', | |
as_name = ag.name, | |
scaling_adjustment=2, | |
cooldown = 180 | |
) | |
scalingDownPolicy = ScalingPolicy( | |
name = 'webserverScaleDownPolicy', | |
adjustment_type = 'ChangeInCapacity', | |
as_name = ag.name, | |
scaling_adjustment = -1, | |
cooldown = 180 | |
) | |
conn_as.create_scaling_policy(scalingUpPolicy) | |
conn_as.create_scaling_policy(scalingDownPolicy) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment